From cfaba87f2fe470667eea4eca0504f6e8651c90f3 Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sat, 13 Apr 2024 23:08:45 +0100 Subject: [PATCH] feat(ipc): ironvar list command --- docs/Controlling Ironbar.md | 16 +++++++++++++++- src/ipc/commands.rs | 3 +++ src/ipc/server.rs | 14 ++++++++++++++ src/ironvar.rs | 8 ++++++-- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/docs/Controlling Ironbar.md b/docs/Controlling Ironbar.md index f8d8877a..f8f5eeb3 100644 --- a/docs/Controlling Ironbar.md +++ b/docs/Controlling Ironbar.md @@ -79,7 +79,7 @@ Responds with `ok`. ### `get` -Gets an [ironvar](ironvars) value. +Gets an [ironvar](ironvars) value. 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` Loads an additional CSS stylesheet, with hot-reloading enabled. diff --git a/src/ipc/commands.rs b/src/ipc/commands.rs index 372ef926..a080ff8a 100644 --- a/src/ipc/commands.rs +++ b/src/ipc/commands.rs @@ -32,6 +32,9 @@ pub enum Command { key: Box, }, + /// Gets the current value of all `ironvar`s. + List, + /// Load an additional CSS stylesheet. /// The sheet is automatically hot-reloaded. LoadCss { diff --git a/src/ipc/server.rs b/src/ipc/server.rs index aa74a610..521545b4 100644 --- a/src/ipc/server.rs +++ b/src/ipc/server.rs @@ -153,6 +153,20 @@ impl Ipc { 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::>(); + + values.sort(); + let value = values.join("\n"); + + Response::OkValue { value } + } Command::LoadCss { path } => { if path.exists() { load_css(path); diff --git a/src/ironvar.rs b/src/ironvar.rs index 5b1db213..e99a3b04 100644 --- a/src/ironvar.rs +++ b/src/ironvar.rs @@ -46,6 +46,10 @@ impl VariableManager { self.variables.get(key).and_then(IronVar::get) } + pub fn get_all(&self) -> &HashMap, IronVar> { + &self.variables + } + /// Subscribes to an `ironvar`, creating it if it does not exist. /// Any time the var is set, its value is sent on the channel. pub fn subscribe(&mut self, key: Box) -> broadcast::Receiver> { @@ -66,7 +70,7 @@ impl VariableManager { /// Ironbar dynamic variable representation. /// Interact with them through the `VARIABLE_MANAGER` `VariableManager` singleton. #[derive(Debug)] -struct IronVar { +pub struct IronVar { value: Option, tx: broadcast::Sender>, _rx: broadcast::Receiver>, @@ -82,7 +86,7 @@ impl IronVar { /// Gets the current variable value. /// Prefer to subscribe to changes where possible. - fn get(&self) -> Option { + pub fn get(&self) -> Option { self.value.clone() }