Skip to content

Commit

Permalink
Merge pull request #595 from JakeStanger/refactor/cli-change
Browse files Browse the repository at this point in the history
Refactor/cli change
  • Loading branch information
JakeStanger authored Jun 1, 2024
2 parents 6b8e874 + 9dd7112 commit c28de8d
Show file tree
Hide file tree
Showing 14 changed files with 565 additions and 393 deletions.
178 changes: 133 additions & 45 deletions docs/Controlling Ironbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,38 @@ It also includes a command line interface, which can be used for interacting wit
# CLI

This is shipped as part of the `ironbar` binary. To view commands, you can use `ironbar --help`.
You can also view help per-command, for example using `ironbar set --help`.
You can also view help per sub-command or command, for example using `ironbar var --help` or `ironbar var set --help`.

Responses are handled by writing their type to stdout, followed by any value starting on the next line.
Error responses are written to stderr in the same format.
The CLI supports plaintext and JSON output. Plaintext will:

- Print `ok` for empty success responses
- Print the returned body for success responses
- Print `error` to followed by the error on the next line for error responses. This is printed to `stderr`.

Example:

```shell
$ ironbar set subject world
$ ironbar var set subject world
ok

$ ironbar get subject
ok
$ ironbar var get subject
world

$ ironbar var get foo
error
Variable not found
```

All error responses will cause the CLI to exit code 3.

# IPC

The server listens on a Unix socket.
This can usually be found at `/run/user/$UID/ironbar-ipc.sock`.
The path is printed on startup, and can usually be found at `/run/user/$UID/ironbar-ipc.sock`.

Commands and responses are sent as JSON objects.

Commands and responses are sent as JSON objects, denoted by their `type` key.
Commands will have a `command` key, and a `subcommand` key when part of a sub-command.

The message buffer is currently limited to `1024` bytes.
Particularly large messages will be truncated or cause an error.
Expand All @@ -47,7 +57,7 @@ Responds with `ok`.

```json
{
"type": "ping"
"command": "ping"
}
```

Expand All @@ -59,7 +69,7 @@ Responds with `ok`.

```json
{
"type": "inspect"
"command": "inspect"
}
```

Expand All @@ -73,38 +83,57 @@ Responds with `ok`.

```json
{
"type": "reload"
"command": "reload"
}
```

### `load_css`

Loads an additional CSS stylesheet, with hot-reloading enabled.

Responds with `ok` if the stylesheet exists, otherwise `error`.

```json
{
"command": "load_css",
"path": "/path/to/style.css"
}
```

### `get`
### `var`

Subcommand for controlling Ironvars.

#### `get`

Gets an [ironvar](ironvars) value.

Responds with `ok_value` if the value exists, otherwise `error`.

```json
{
"type": "get",
"command": "var",
"subcommand": "get",
"key": "foo"
}
```

### `set`
#### `set`

Sets an [ironvar](ironvars) value.

Responds with `ok`.

```json
{
"type": "set",
"command": "var",
"subcommand": "set",
"key": "foo",
"value": "bar"
}
```

### list
#### `list`

Gets a list of all [ironvar](ironvars) values.

Expand All @@ -114,89 +143,148 @@ Each key/value pair is on its own `\n` separated newline. The key and value are

```json
{
"type": "list"
"command": "var",
"subcommand": "list"
}
```

### `load_css`
### `bar`

Loads an additional CSS stylesheet, with hot-reloading enabled.
#### `show`

Responds with `ok` if the stylesheet exists, otherwise `error`.
Forces a bar to be shown, regardless of the current visibility state.

```json
{
"type": "load_css",
"path": "/path/to/style.css"
"command": "bar",
"subcommand": "show",
"name": "bar-123"
}
```

#### `hide`

Forces a bar to be hidden, regardless of the current visibility state.

```json
{
"command": "bar",
"subcommand": "hide",
"name": "bar-123"
}
```

### `set_visible`
#### `set_visible`

Sets a bar's visibility.
Sets a bar's visibility to one of shown/hidden.

Responds with `ok` if the bar exists, otherwise `error`.

```json
{
"type": "set_visible",
"bar_name": "bar-123",
"command": "bar",
"subcommand": "set_visible",
"name": "bar-123",
"visible": true
}
```

### `get_visible`
#### `toggle_visible`

Toggles the current visibility state of a bar between shown and hidden.

```json
{
"command": "bar",
"subcommand": "toggle_visible",
"name": "bar-123"
}
```

#### `get_visible`

Gets a bar's visibility.

Responds with `ok_value` and the visibility (`true`/`false`) if the bar exists, otherwise `error`.

```json
{
"type": "get_visible",
"bar_name": "bar-123"
"command": "bar",
"subcommand": "get_visible",
"name": "bar-123"
}
```

### `toggle_popup`
#### `show_popup`

Toggles the open/closed state for a module's popup.
Sets a module's popup open, regardless of its current state.
Since each bar only has a single popup, any open popup on the bar is closed.

Responds with `ok` if the popup exists, otherwise `error`.
Responds with `ok` if the bar and widget exist, otherwise `error`.

```json
{
"type": "toggle_popup",
"bar_name": "bar-123",
"name": "clock"
"command": "bar",
"subcommand": "show_popup",
"name": "bar-123",
"widget_name": "clock"
}
```

### `open_popup`
#### `hide_popup`

Sets a module's popup open, regardless of its current state.
Sets the popup on a bar closed, regardless of which module it is open for.

Responds with `ok` if the bar and widget exist, otherwise `error`.

```json
{
"command": "bar",
"subcommand": "hide_popup",
"bar_name": "bar-123"
}
```

#### `set_popup_visible`

Sets a popup's visibility to one of shown/hidden.

Responds with `ok` if the bar and widget exist, otherwise `error`.

```json
{
"command": "bar",
"subcommand": "set_popup_visible",
"name": "bar-123",
"widget_name": "clock",
"visible": true
}
```

#### `toggle_popup`

Toggles the open/closed state for a module's popup.
Since each bar only has a single popup, any open popup on the bar is closed.

Responds with `ok` if the popup exists, otherwise `error`.
Responds with `ok` if the bar and widget exist, otherwise `error`.

```json
{
"type": "open_popup",
"command": "bar",
"subcommand": "toggle_popup",
"bar_name": "bar-123",
"name": "clock"
"widget_name": "clock"
}
```

### `close_popup`

Sets the popup on a bar closed, regardless of which module it is open for.
#### `get_popup_visible`

Responds with `ok` if the popup exists, otherwise `error`.
Gets the popup's current visibility state.

```json
{
"type": "close_popup",
"command": "bar",
"subcommand": "get_popup_visible",
"bar_name": "bar-123"
}
```
Expand Down
9 changes: 9 additions & 0 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,15 @@ impl Bar {
Inner::Loaded { popup } => popup.clone(),
}
}

pub fn visible(&self) -> bool {
self.window.is_visible()
}

/// Sets the window visibility status
pub fn set_visible(&self, visible: bool) {
self.window.set_visible(visible)
}
}

/// Creates a `gtk::Box` container to place widgets inside.
Expand Down
42 changes: 36 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::error::ExitCode;
use crate::ipc::commands::Command;
use crate::ipc::responses::Response;
use clap::Parser;
use clap::{Parser, ValueEnum};
use serde::{Deserialize, Serialize};
use std::process::exit;

#[derive(Parser, Debug, Serialize, Deserialize)]
#[command(version)]
Expand All @@ -15,16 +17,44 @@ pub struct Args {
#[arg(long("print-schema"))]
pub print_schema: bool,

/// Print debug information to stderr
/// TODO: Make bar follow this too
#[arg(long)]
pub debug: bool,

/// Format to output the response as.
#[arg(short, long)]
pub format: Option<Format>,

/// `bar_id` argument passed by `swaybar_command`.
/// Not used.
#[arg(short('b'), hide(true))]
sway_bar_id: Option<String>,
}

pub fn handle_response(response: Response) {
match response {
Response::Ok => println!("ok"),
Response::OkValue { value } => println!("ok\n{value}"),
Response::Err { message } => eprintln!("error\n{}", message.unwrap_or_default()),
#[derive(Debug, Serialize, Deserialize, Default, ValueEnum, Clone, Copy)]
pub enum Format {
#[default]
Plain,
Json,
}

pub fn handle_response(response: Response, format: Format) {
let is_err = matches!(response, Response::Err { .. });

match format {
Format::Plain => match response {
Response::Ok => println!("ok"),
Response::OkValue { value } => println!("{value}"),
Response::Err { message } => eprintln!("error\n{}", message.unwrap_or_default()),
},
Format::Json => println!(
"{}",
serde_json::to_string(&response).expect("to be valid json")
),
}

if is_err {
exit(ExitCode::IpcResponseError as i32)
}
}
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub enum ExitCode {
GtkDisplay = 1,
CreateBars = 2,
IpcResponseError = 3,
}

pub const ERR_MUTEX_LOCK: &str = "Failed to get lock on Mutex";
Expand Down
Loading

0 comments on commit c28de8d

Please sign in to comment.