From b00061d4e64ac6b9b3f473f86347099f76cad7ed Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 12 Jun 2022 15:43:04 +0900 Subject: [PATCH] add support for termion --- Cargo.toml | 1 + src/input.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7d493b..233aebd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ default = ["crossterm"] [dependencies] crossterm = { version = "0.23", optional = true } +termion = { version = "1.5", optional = true } tui = "0.18" [[example]] diff --git a/src/input.rs b/src/input.rs index 0a63459..88152e4 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,5 +1,7 @@ #[cfg(feature = "crossterm")] -use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; +use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers}; +#[cfg(feature = "termion")] +use termion::event::{Event as TerimonEvent, Key as TermionKey}; #[derive(Clone, Copy, Debug)] pub enum Key { @@ -37,9 +39,9 @@ impl Default for Input { } #[cfg(feature = "crossterm")] -impl From for Input { - fn from(event: Event) -> Self { - if let Event::Key(key) = event { +impl From for Input { + fn from(event: CrosstermEvent) -> Self { + if let CrosstermEvent::Key(key) = event { Self::from(key) } else { Self::default() @@ -71,3 +73,49 @@ impl From for Input { Self { key, ctrl, alt } } } + +#[cfg(feature = "termion")] +impl From for Input { + fn from(event: TerimonEvent) -> Self { + if let TerimonEvent::Key(key) = event { + Self::from(key) + } else { + Self::default() + } + } +} + +#[cfg(feature = "termion")] +impl From for Input { + fn from(key: TermionKey) -> Self { + use TermionKey::*; + + let mut ctrl = false; + let mut alt = false; + let key = match key { + Char(c) => Key::Char(c), + Ctrl(c) => { + ctrl = true; + Key::Char(c) + } + Alt(c) => { + alt = true; + Key::Char(c) + } + Backspace => Key::Backspace, + Left => Key::Left, + Right => Key::Right, + Up => Key::Up, + Down => Key::Down, + Home => Key::Home, + End => Key::End, + PageUp => Key::PageUp, + PageDown => Key::PageDown, + BackTab => Key::Tab, + Delete => Key::Delete, + _ => Key::Null, + }; + + Input { key, ctrl, alt } + } +}