diff options
| author | Jonas Maier <> | 2026-05-03 09:15:04 +0200 |
|---|---|---|
| committer | Jonas Maier <> | 2026-05-03 09:15:04 +0200 |
| commit | d5953e52f0df8ca6727e71fa07f147467a7369c1 (patch) | |
| tree | cffbb58207d2aa81fc6d765b005b1225914fb268 /src/ansi | |
| parent | a9bf864158e67353f57047cdc4d6b0e325d73eae (diff) | |
| download | pish-d5953e52f0df8ca6727e71fa07f147467a7369c1.tar.gz | |
complete keybind overhaul, not working, afraid to loose it
Diffstat (limited to 'src/ansi')
| -rw-r--r-- | src/ansi/mod.rs | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/src/ansi/mod.rs b/src/ansi/mod.rs index 4fc550b..0b43ede 100644 --- a/src/ansi/mod.rs +++ b/src/ansi/mod.rs @@ -114,26 +114,33 @@ fn read_escape(debug: bool) -> KeyboardInput { } } -pub fn read(debug: bool) -> KeyboardInput { - use KeyboardInput::*; - - let Some(x) = read1() else { - return KeyboardInput::Eof; - }; - - match x { - 1 => CtrlA, - 2 => CtrlB, - 3 => CtrlC, - 4 => CtrlD, - 5 => CtrlE, - 8 | 127 => DeleteLeft, - 12 => CtrlL, - 18 => CtrlR, - 27 => read_escape(debug), - b'\t' | b'\r' => Key(x), - x if !x.is_ascii_control() => Key(x), - x => todo!("unimplemented control code: {x}"), +pub fn read(debug: bool) -> Option<KbInput<'static>> { + let trie = EscapeTrie::from(ti()); + let trie = Box::leak(Box::new(trie)); // TODO don't leak memory, this is only temporary + let mut reader = EscapingStdinReader::new(trie); + loop { + let Some(b) = read1() else { + break None; + }; + match reader.process_byte(b) { + ByteProcessingResult::Done(kb) => { + if debug { + if let KbInput::Escape(e) = &kb { + for k in e.keys.iter() { + print!("{k} ") + } + } + println!("{}\r", kb.as_bytes().escape_ascii()); + } + if let KbInput::InvalidEscape(x) = &kb { + if x.len() == 1 { + break Some(KbInput::Key([x[0]])); + } + } + break Some(kb); + } + ByteProcessingResult::Continue(r) => reader = r, + } } } @@ -183,7 +190,8 @@ enum EscapeTrie { More(BTreeMap<u8, EscapeTrie>), } -enum KbInput<'a> { +#[derive(Debug)] +pub enum KbInput<'a> { Key([u8; 1]), Escape(Escape<'a>), InvalidEscape(Vec<u8>), @@ -199,9 +207,10 @@ impl<'a> KbInput<'a> { } } -struct Escape<'a> { - keys: &'a [&'a str], - value: Vec<u8>, +#[derive(Debug)] +pub struct Escape<'a> { + pub keys: &'a [&'a str], + pub value: Vec<u8>, } use terminfo_lean::parse::Terminfo; @@ -280,12 +289,12 @@ fn trie_from_words(words: Vec<(&'static str, &[u8])>) -> EscapeTrie { } } -impl From<&'static Terminfo<'static>> for EscapeTrie { - fn from(ti: &'static Terminfo<'static>) -> Self { +impl From<&Terminfo<'static>> for EscapeTrie { + fn from(ti: &Terminfo<'static>) -> Self { let w: Vec<(&'static str, &'static [u8])> = ti .strings .iter() - .filter(|(_, v)| !is_parametrized(v)) + .filter(|(k, v)| k.starts_with("k") && !is_parametrized(v)) .map(|(k, v)| (*k, *v)) .collect(); trie_from_words(w) |
