Skip to content

Commit

Permalink
feat(cmd): parse numeric delay (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo authored May 25, 2024
1 parent c965cef commit 2a94c9c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1587,8 +1587,8 @@ use the shell as the first parameter, e.g. `bash` or `powershell.exe`.

There is a variant of `cmd`: `cmd-output-keys`. This variant reads the output
of the executed program and reads it as an S-expression, similarly to the
<<macro, macro action>>. However — unlike macro — only keys, chords, and
chorded lists are supported. Delays and other actions are not supported.
<<macro, macro action>>. However — unlike macro — only delays, keys, chords, and
chorded lists are supported. Other actions are not supported.

[source]
----
Expand Down
36 changes: 23 additions & 13 deletions src/kanata/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ pub(super) fn run_cmd_in_thread(cmd_and_args: Vec<String>) -> std::thread::JoinH
})
}

pub(super) type Item = (KeyAction, OsCode);
pub(super) type Item = KeyAction;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(super) enum KeyAction {
Press,
Release,
Press(OsCode),
Release(OsCode),
Delay(u16),
}
use kanata_keyberon::key_code::KeyCode;
use KeyAction::*;
Expand All @@ -71,11 +72,20 @@ fn parse_items<'a>(exprs: &'a [SExpr], items: &mut Vec<Item>) -> &'a [SExpr] {
match &exprs[0] {
SExpr::Atom(osc) => match str_to_oscode(&osc.t) {
Some(osc) => {
items.push((Press, osc));
items.push((Release, osc));
items.push(Press(osc));
items.push(Release(osc));
&exprs[1..]
}
None => try_parse_chord(&osc.t, exprs, items),
None => {
use std::str::FromStr;
match u16::from_str(&osc.t) {
Ok(delay) => {
items.push(Delay(delay));
&exprs[1..]
}
Err(_) => try_parse_chord(&osc.t, exprs, items),
}
}
},
SExpr::List(sexprs) => {
let mut remainder = sexprs.t.as_slice();
Expand Down Expand Up @@ -111,13 +121,13 @@ fn try_parse_chorded_key(mods: &[KeyCode], osc: &str, chord: &str, items: &mut V
match str_to_oscode(osc) {
Some(osc) => {
for mod_kc in mods.iter().copied() {
items.push((Press, mod_kc.into()));
items.push(Press(mod_kc.into()));
}
items.push((Press, osc));
items.push(Press(osc));
for mod_kc in mods.iter().copied() {
items.push((Release, mod_kc.into()));
items.push(Release(mod_kc.into()));
}
items.push((Release, osc));
items.push(Release(osc));
}
None => {
log::warn!("{LP} found chord {chord} with invalid key: {osc}");
Expand All @@ -144,14 +154,14 @@ fn try_parse_chorded_list<'a>(
}
SExpr::List(subexprs) => {
for mod_kc in mods.iter().copied() {
items.push((Press, mod_kc.into()));
items.push(Press(mod_kc.into()));
}
let mut remainder = subexprs.t.as_slice();
while !remainder.is_empty() {
remainder = parse_items(remainder, items);
}
for mod_kc in mods.iter().copied() {
items.push((Release, mod_kc.into()));
items.push(Release(mod_kc.into()));
}
&exprs[1..]
}
Expand Down
16 changes: 13 additions & 3 deletions src/kanata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,10 +1273,20 @@ impl Kanata {
CustomAction::CmdOutputKeys(_cmd) => {
#[cfg(feature = "cmd")]
{
for (key_action, osc) in keys_for_cmd_output(_cmd) {
let cmd = _cmd.clone();
// Maybe improvement in the future:
// A delay here, as in KeyAction::Delay, will pause the entire
// state machine loop. That is _probably_ OK, but ideally this
// would be done in a separate thread or somehow
for key_action in keys_for_cmd_output(&cmd) {
match key_action {
KeyAction::Press => press_key(&mut self.kbd_out, osc)?,
KeyAction::Release => release_key(&mut self.kbd_out, osc)?,
KeyAction::Press(osc) => press_key(&mut self.kbd_out, osc)?,
KeyAction::Release(osc) => {
release_key(&mut self.kbd_out, osc)?
}
KeyAction::Delay(delay) => std::thread::sleep(
std::time::Duration::from_millis(u64::from(delay)),
),
}
}
}
Expand Down

0 comments on commit 2a94c9c

Please sign in to comment.