feat(ipc): ironvar list command

This commit is contained in:
Jake Stanger 2024-04-13 23:08:45 +01:00
parent 7e7b3039eb
commit cfaba87f2f
No known key found for this signature in database
GPG Key ID: C51FC8F9CB0BEA61
4 changed files with 38 additions and 3 deletions

View File

@ -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.

View File

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

View File

@ -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::<Vec<_>>();
values.sort();
let value = values.join("\n");
Response::OkValue { value }
}
Command::LoadCss { path } => {
if path.exists() {
load_css(path);

View File

@ -46,6 +46,10 @@ impl VariableManager {
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.
/// 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>> {
@ -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<String>,
tx: broadcast::Sender<Option<String>>,
_rx: broadcast::Receiver<Option<String>>,
@ -82,7 +86,7 @@ impl IronVar {
/// Gets the current variable value.
/// Prefer to subscribe to changes where possible.
fn get(&self) -> Option<String> {
pub fn get(&self) -> Option<String> {
self.value.clone()
}