diff options
| author | Aram Drevekenin <aram@poor.dev> | 2022-03-23 09:08:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-23 09:08:35 +0100 |
| commit | 7141779153743cb98771c6582a1feff32ec2e494 (patch) | |
| tree | bf30b37263858d5e166caa226ffadbb1e53c8c0f /zellij-utils/src/input/mod.rs | |
| parent | chore(docs): update changelog with rounded corners (diff) | |
| download | sl0ppij-7141779153743cb98771c6582a1feff32ec2e494.tar.gz sl0ppij-7141779153743cb98771c6582a1feff32ec2e494.tar.xz sl0ppij-7141779153743cb98771c6582a1feff32ec2e494.zip | |
chore(deps): move from termion to termwiz for STDIN handling (#1249)
* fix(deps): switch from termion to termwiz for STDIN parsing
* style(fmt): clippy
* style(fmt): moar clippy
* style(fmt): rustfmt
* fix(tests): e2e mouse press
* style(fmt): rustfmt
* bring back polling
* fmt fmt fmt
* fix some e2e flakiness
Diffstat (limited to 'zellij-utils/src/input/mod.rs')
| -rw-r--r-- | zellij-utils/src/input/mod.rs | 72 |
1 files changed, 48 insertions, 24 deletions
diff --git a/zellij-utils/src/input/mod.rs b/zellij-utils/src/input/mod.rs index 289e52ca..869c513a 100644 --- a/zellij-utils/src/input/mod.rs +++ b/zellij-utils/src/input/mod.rs @@ -11,7 +11,7 @@ pub mod plugins; pub mod theme; use crate::envs; -use termion::input::TermRead; +use termwiz::input::{InputEvent, InputParser, KeyCode, KeyEvent, Modifiers}; use zellij_tile::{ data::{InputMode, Key, ModeInfo, PluginCapabilities}, prelude::Style, @@ -83,33 +83,57 @@ pub fn get_mode_info(mode: InputMode, style: Style, capabilities: PluginCapabili } pub fn parse_keys(input_bytes: &[u8]) -> Vec<Key> { - input_bytes.keys().flatten().map(cast_termion_key).collect() + let mut ret = vec![]; + let mut input_parser = InputParser::new(); // this is the termwiz InputParser + let maybe_more = false; + let parse_input_event = |input_event: InputEvent| { + if let InputEvent::Key(key_event) = input_event { + ret.push(cast_termwiz_key(key_event, input_bytes)); + } + }; + input_parser.parse(input_bytes, parse_input_event, maybe_more); + ret } // FIXME: This is an absolutely cursed function that should be destroyed as soon // as an alternative that doesn't touch zellij-tile can be developed... -pub fn cast_termion_key(event: termion::event::Key) -> Key { - match event { - termion::event::Key::Backspace => Key::Backspace, - termion::event::Key::Left => Key::Left, - termion::event::Key::Right => Key::Right, - termion::event::Key::Up => Key::Up, - termion::event::Key::Down => Key::Down, - termion::event::Key::Home => Key::Home, - termion::event::Key::End => Key::End, - termion::event::Key::PageUp => Key::PageUp, - termion::event::Key::PageDown => Key::PageDown, - termion::event::Key::BackTab => Key::BackTab, - termion::event::Key::Delete => Key::Delete, - termion::event::Key::Insert => Key::Insert, - termion::event::Key::F(n) => Key::F(n), - termion::event::Key::Char(c) => Key::Char(c), - termion::event::Key::Alt(c) => Key::Alt(c), - termion::event::Key::Ctrl(c) => Key::Ctrl(c), - termion::event::Key::Null => Key::Null, - termion::event::Key::Esc => Key::Esc, - _ => { - unimplemented!("Encountered an unknown key!") +pub fn cast_termwiz_key(event: KeyEvent, raw_bytes: &[u8]) -> Key { + let modifiers = event.modifiers; + + // *** THIS IS WHERE WE SHOULD WORK AROUND ISSUES WITH TERMWIZ *** + if raw_bytes == [8] { + return Key::Ctrl('h'); + }; + + match event.key { + KeyCode::Char(c) => { + if modifiers.contains(Modifiers::CTRL) { + Key::Ctrl(c.to_lowercase().next().unwrap_or_default()) + } else if modifiers.contains(Modifiers::ALT) { + Key::Alt(c.to_lowercase().next().unwrap_or_default()) + } else { + Key::Char(c) + } } + KeyCode::Backspace => Key::Backspace, + KeyCode::LeftArrow => Key::Left, + KeyCode::ApplicationLeftArrow => Key::Left, + KeyCode::RightArrow => Key::Right, + KeyCode::ApplicationRightArrow => Key::Right, + KeyCode::UpArrow => Key::Up, + KeyCode::ApplicationUpArrow => Key::Up, + KeyCode::DownArrow => Key::Down, + KeyCode::ApplicationDownArrow => Key::Down, + KeyCode::Home => Key::Home, + KeyCode::End => Key::End, + KeyCode::PageUp => Key::PageUp, + KeyCode::PageDown => Key::PageDown, + KeyCode::Tab => Key::BackTab, // TODO: ??? + KeyCode::Delete => Key::Delete, + KeyCode::Insert => Key::Insert, + KeyCode::Function(n) => Key::F(n), + KeyCode::Escape => Key::Esc, + KeyCode::Enter => Key::Char('\n'), + _ => Key::Esc, // there are other keys we can implement here, but we might need additional terminal support to implement them, not just exhausting this enum } } |
