1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 19:34:24 +02:00

Merge pull request #539 from JakeStanger/feat/ipc-list

feat(ipc): ironvar list command
This commit is contained in:
Jake Stanger 2024-04-13 23:33:20 +01:00 committed by GitHub
commit 88840b8391
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 3 deletions

View file

@ -79,7 +79,7 @@ Responds with `ok`.
### `get` ### `get`
Gets an [ironvar](ironvars) value. Gets an [ironvar](ironvars) value.
Responds with `ok_value` if the value exists, otherwise `error`. Responds with `ok_value` if the value exists, otherwise `error`.
@ -104,6 +104,20 @@ Responds with `ok`.
} }
``` ```
### list
Gets a list of all [ironvar](ironvars) values.
Responds with `ok_value`.
Each key/value pair is on its own `\n` separated newline. The key and value are separated by a colon and space `: `.
```json
{
"type": "list"
}
```
### `load_css` ### `load_css`
Loads an additional CSS stylesheet, with hot-reloading enabled. Loads an additional CSS stylesheet, with hot-reloading enabled.

View file

@ -32,6 +32,9 @@ pub enum Command {
key: Box<str>, key: Box<str>,
}, },
/// Gets the current value of all `ironvar`s.
List,
/// Load an additional CSS stylesheet. /// Load an additional CSS stylesheet.
/// The sheet is automatically hot-reloaded. /// The sheet is automatically hot-reloaded.
LoadCss { LoadCss {

View file

@ -153,6 +153,20 @@ impl Ipc {
None => Response::error("Variable not found"), None => Response::error("Variable not found"),
} }
} }
Command::List => {
let variable_manager = Ironbar::variable_manager();
let mut values = read_lock!(variable_manager)
.get_all()
.iter()
.map(|(k, v)| format!("{k}: {}", v.get().unwrap_or_default()))
.collect::<Vec<_>>();
values.sort();
let value = values.join("\n");
Response::OkValue { value }
}
Command::LoadCss { path } => { Command::LoadCss { path } => {
if path.exists() { if path.exists() {
load_css(path); load_css(path);

View file

@ -46,6 +46,10 @@ impl VariableManager {
self.variables.get(key).and_then(IronVar::get) self.variables.get(key).and_then(IronVar::get)
} }
pub fn get_all(&self) -> &HashMap<Box<str>, IronVar> {
&self.variables
}
/// Subscribes to an `ironvar`, creating it if it does not exist. /// Subscribes to an `ironvar`, creating it if it does not exist.
/// Any time the var is set, its value is sent on the channel. /// Any time the var is set, its value is sent on the channel.
pub fn subscribe(&mut self, key: Box<str>) -> broadcast::Receiver<Option<String>> { pub fn subscribe(&mut self, key: Box<str>) -> broadcast::Receiver<Option<String>> {
@ -66,7 +70,7 @@ impl VariableManager {
/// Ironbar dynamic variable representation. /// Ironbar dynamic variable representation.
/// Interact with them through the `VARIABLE_MANAGER` `VariableManager` singleton. /// Interact with them through the `VARIABLE_MANAGER` `VariableManager` singleton.
#[derive(Debug)] #[derive(Debug)]
struct IronVar { pub struct IronVar {
value: Option<String>, value: Option<String>,
tx: broadcast::Sender<Option<String>>, tx: broadcast::Sender<Option<String>>,
_rx: broadcast::Receiver<Option<String>>, _rx: broadcast::Receiver<Option<String>>,
@ -82,7 +86,7 @@ impl IronVar {
/// Gets the current variable value. /// Gets the current variable value.
/// Prefer to subscribe to changes where possible. /// Prefer to subscribe to changes where possible.
fn get(&self) -> Option<String> { pub fn get(&self) -> Option<String> {
self.value.clone() self.value.clone()
} }