wip: let's figure out how edn keymaps will work

This commit is contained in:
🪞👃🪞 2025-01-12 13:01:15 +01:00
parent 4ab08e48e5
commit 794d4210c6
33 changed files with 161 additions and 85 deletions

View file

@ -0,0 +1,46 @@
use crate::*;
impl EdnInput for TuiIn {
fn matches (&self, token: &str) -> bool {
false
}
fn get_event <S: AsRef<str>> (item: &EdnItem<S>) -> Option<Event> {
match item { EdnItem::Sym(s) => parse_key_spec(s.as_ref().to_string(), KeyModifiers::NONE), _ => None }
}
}
fn parse_key_spec (spec: String, mut mods: KeyModifiers) -> Option<Event> {
Some(Event::Key(match spec.as_str() {
":enter" | ":return" => KeyEvent::new(KeyCode::Enter, mods),
":delete" | ":del" => KeyEvent::new(KeyCode::Enter, mods),
":comma" => KeyEvent::new(KeyCode::Char(','), mods),
":period" => KeyEvent::new(KeyCode::Char('.'), mods),
":plus" => KeyEvent::new(KeyCode::Char('+'), mods),
":underscore" => KeyEvent::new(KeyCode::Char('_'), mods),
":up" => KeyEvent::new(KeyCode::Up, mods),
":down" => KeyEvent::new(KeyCode::Down, mods),
":left" => KeyEvent::new(KeyCode::Left, mods),
":right" => KeyEvent::new(KeyCode::Right, mods),
_ => {
let chars = spec.chars().collect::<Vec<_>>();
match chars.as_slice() {
[':', c] => KeyEvent::new(KeyCode::Char(*c), mods),
[':', c @ ..] => {
let mut splits = c.split(|c|*c=='-');
while let Some(split) = splits.next() {
match split {
['c','t','r','l'] => mods |= KeyModifiers::CONTROL,
['a','l','t'] => mods |= KeyModifiers::ALT,
['s','h','i','f','t'] => mods |= KeyModifiers::SHIFT,
_ => return parse_key_spec(split.iter().collect(), mods)
}
}
panic!("invalid key event {spec:?}")
}
_ => panic!("invalid key event {spec:?}")
}
}
}))
}