Skip to content

Commit

Permalink
Add config to keep UI open after command is executed (#9)
Browse files Browse the repository at this point in the history
This lets you execute one-shot commands repeatedly without the UI closing (giving you functionality similar to the hydra emacs package).
  • Loading branch information
noxpardalis authored Jan 2, 2025
1 parent 13fd97d commit 7f767aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Default config file: `$XDG_CONFIG_HOME/wlr-which-key/config.yaml` or `~/.config/

Keybindings may be single characters (e.g. `a`, `B`) or [xkb key labels](https://github.com/xkbcommon/libxkbcommon/blob/master/include/xkbcommon/xkbcommon-keysyms.h) (without the `XKB_KEY_` prefix, e.g. `Return`, `Insert`). Ctrl and Alt modifiers are supported (like `Ctrl+Return` or `Ctrl+Alt+a`).

When executed a command will normally end the `wlr_which_key` process. If you want certain commands to keep the UI open after they execute then
configure those specific commands with (`keep_open: true`).

Example config:

```yaml
Expand Down Expand Up @@ -56,7 +59,7 @@ menu:
submenu:
"d": { desc: Dark, cmd: dark-theme on }
"l": { desc: Light, cmd: dark-theme off }
"t": { desc: Toggle, cmd: dark-theme toggle }
"t": { desc: Toggle, cmd: dark-theme toggle, keep_open: true }
"l":
desc: Laptop Screen
submenu:
Expand Down
12 changes: 10 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ pub struct Config {
#[derive(Deserialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum Entry {
Cmd { cmd: String, desc: String },
Recursive { submenu: Entries, desc: String },
Cmd {
cmd: String,
desc: String,
#[serde(default)]
keep_open: bool,
},
Recursive {
submenu: Entries,
desc: String,
},
}

impl Config {
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl KeyboardHandler for State {
self.exit = true;
conn.break_dispatch_loop();
}
menu::Action::Exec(cmd) => {
menu::Action::Exec { cmd, keep_open } => {
let mut proc = Command::new("sh");
proc.args(["-c", &cmd]);
proc.stdin(Stdio::null());
Expand All @@ -327,7 +327,9 @@ impl KeyboardHandler for State {
});
}
proc.spawn().unwrap().wait().unwrap();
self.exit = true;
if !keep_open {
self.exit = true;
}
}
menu::Action::Submenu(page) => {
self.menu.set_page(page);
Expand Down
13 changes: 10 additions & 3 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct MenuItem {
#[derive(Clone)]
pub enum Action {
Quit,
Exec(String),
Exec { cmd: String, keep_open: bool },
Submenu(usize),
}

Expand Down Expand Up @@ -74,8 +74,15 @@ impl Menu {

for (key, entry) in &entries.0 {
let item = match entry {
config::Entry::Cmd { cmd, desc } => MenuItem {
action: Action::Exec(cmd.into()),
config::Entry::Cmd {
cmd,
desc,
keep_open,
} => MenuItem {
action: Action::Exec {
cmd: cmd.into(),
keep_open: *keep_open,
},
key_comp: ComputedText::new(&key.repr, context, &config.font),
val_comp: ComputedText::new(desc, context, &config.font),
key: key.clone(),
Expand Down

0 comments on commit 7f767aa

Please sign in to comment.