Skip to content

Commit

Permalink
Merge pull request #539 from JakeStanger/feat/ipc-list
Browse files Browse the repository at this point in the history
feat(ipc): ironvar list command
  • Loading branch information
JakeStanger authored Apr 13, 2024
2 parents 7e7b303 + cfaba87 commit 88840b8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
16 changes: 15 additions & 1 deletion docs/Controlling Ironbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/ipc/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions src/ipc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/ironvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand All @@ -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>>,
Expand All @@ -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()
}

Expand Down

0 comments on commit 88840b8

Please sign in to comment.