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 | |
| 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')
| -rw-r--r-- | zellij-utils/src/input/mod.rs | 72 | ||||
| -rw-r--r-- | zellij-utils/src/input/mouse.rs | 88 |
2 files changed, 109 insertions, 51 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 } } diff --git a/zellij-utils/src/input/mouse.rs b/zellij-utils/src/input/mouse.rs index d5dc4330..0a63ac2d 100644 --- a/zellij-utils/src/input/mouse.rs +++ b/zellij-utils/src/input/mouse.rs @@ -19,21 +19,67 @@ pub enum MouseEvent { Hold(Position), } -impl From<termion::event::MouseEvent> for MouseEvent { - fn from(event: termion::event::MouseEvent) -> Self { - match event { - termion::event::MouseEvent::Press(button, x, y) => Self::Press( - MouseButton::from(button), - Position::new((y.saturating_sub(1)) as i32, x.saturating_sub(1)), - ), - termion::event::MouseEvent::Release(x, y) => Self::Release(Position::new( - (y.saturating_sub(1)) as i32, - x.saturating_sub(1), - )), - termion::event::MouseEvent::Hold(x, y) => Self::Hold(Position::new( - (y.saturating_sub(1)) as i32, - x.saturating_sub(1), - )), +impl From<termwiz::input::MouseEvent> for MouseEvent { + fn from(event: termwiz::input::MouseEvent) -> Self { + #[allow(clippy::if_same_then_else)] + if event + .mouse_buttons + .contains(termwiz::input::MouseButtons::LEFT) + { + MouseEvent::Press( + MouseButton::Left, + Position::new(event.y.saturating_sub(1) as i32, event.x.saturating_sub(1)), + ) + } else if event + .mouse_buttons + .contains(termwiz::input::MouseButtons::RIGHT) + { + MouseEvent::Press( + MouseButton::Right, + Position::new(event.y.saturating_sub(1) as i32, event.x.saturating_sub(1)), + ) + } else if event + .mouse_buttons + .contains(termwiz::input::MouseButtons::MIDDLE) + { + MouseEvent::Press( + MouseButton::Middle, + Position::new(event.y.saturating_sub(1) as i32, event.x.saturating_sub(1)), + ) + } else if event + .mouse_buttons + .contains(termwiz::input::MouseButtons::VERT_WHEEL) + { + if event + .mouse_buttons + .contains(termwiz::input::MouseButtons::WHEEL_POSITIVE) + { + MouseEvent::Press( + MouseButton::WheelUp, + Position::new(event.y.saturating_sub(1) as i32, event.x.saturating_sub(1)), + ) + } else { + MouseEvent::Press( + MouseButton::WheelDown, + Position::new(event.y.saturating_sub(1) as i32, event.x.saturating_sub(1)), + ) + } + } else if event + .mouse_buttons + .contains(termwiz::input::MouseButtons::NONE) + { + // release + MouseEvent::Release(Position::new( + event.y.saturating_sub(1) as i32, + event.x.saturating_sub(1), + )) + } else { + // this is an unsupported event, we just do this in order to send "something", but if + // something happens here, we might want to add more specific support + MouseEvent::Release(Position::new( + event.y.saturating_sub(1) as i32, + event.x.saturating_sub(1), + )) } } } @@ -55,15 +101,3 @@ pub enum MouseButton { /// This event is typically only used with Mouse::Press. WheelDown, } - -impl From<termion::event::MouseButton> for MouseButton { - fn from(button: termion::event::MouseButton) -> Self { - match button { - termion::event::MouseButton::Left => Self::Left, - termion::event::MouseButton::Right => Self::Right, - termion::event::MouseButton::Middle => Self::Middle, - termion::event::MouseButton::WheelUp => Self::WheelUp, - termion::event::MouseButton::WheelDown => Self::WheelDown, - } - } -} |
