Skip to content

Commit

Permalink
feat(whkd): allow single vkey hotkeys
Browse files Browse the repository at this point in the history
This commit updates the whkdrc grammar and parser to allow a single vkey
to be used as a hotkey, for example "f11 : komorebic toggle-maximize".
  • Loading branch information
LGUG2Z committed Jan 4, 2025
1 parent 04b7330 commit f983a64
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[package]
name = "whkd"
version = "0.2.4"
version = "0.2.5"
description = "A simple hotkey daemon for Windows"
categories = ["hotkey-daemon", "windows"]
repository = "https://github.com/LGUG2Z/whkd"
license = "MIT"
edition = "2021"
Expand Down
17 changes: 12 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,19 @@ impl TryFrom<&HotkeyBinding> for HkmData {
type Error = WHKError;

fn try_from(value: &HotkeyBinding) -> Result<Self, Self::Error> {
let (trigger, mods) = value.keys.split_last().unwrap();
let mut mod_keys = vec![];
let vkey = VKey::from_keyname(trigger)?;
for m in mods {
mod_keys.push(ModKey::from_keyname(m)?);
}

let (mod_keys, vkey) = if value.keys.len() == 1 {
(vec![], VKey::from_keyname(&value.keys[0])?)
} else {
let (trigger, mods) = value.keys.split_last().unwrap();
let vkey = VKey::from_keyname(trigger)?;
for m in mods {
mod_keys.push(ModKey::from_keyname(m)?);
}

(mod_keys, vkey)
};

Ok(Self {
mod_keys,
Expand Down
24 changes: 23 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn parser() -> impl Parser<char, Whkdrc, Error = Simple<char>> {
let hotkeys = choice((text::ident(), text::int(10)))
.padded()
.separated_by(just("+"))
.at_least(2)
.at_least(1)
.collect::<Vec<String>>();

let delimiter = just(":").padded();
Expand Down Expand Up @@ -227,4 +227,26 @@ alt + 1 : komorebic focus-workspace 0 # digits are fine in the hotkeys section

assert_eq!(output.unwrap(), expected);
}

#[test]
fn test_binding_without_modkeys() {
let src = r#"
# sample comment
.shell pwsh # can be one of cmd | pwsh | powershell
f11 : echo "hello f11""#;

let output = parser().parse(src);
let expected = Whkdrc {
shell: Shell::Pwsh,
app_bindings: vec![],
bindings: vec![HotkeyBinding {
keys: vec![String::from("f11")],
command: String::from("echo \"hello f11\""),
process_name: None,
}],
};

assert_eq!(output.unwrap(), expected);
}
}

0 comments on commit f983a64

Please sign in to comment.