diff --git a/Cargo.toml b/Cargo.toml index cc97441..e21cc6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ no-backend = ["ratatui"] # Features to use tui-rs tuirs = ["dep:tui"] tuirs-crossterm = ["tuirs", "dep:crossterm-025", "tui/crossterm"] -tuirs-termion = ["tuirs", "dep:termion", "tui/termion"] +tuirs-termion = ["tuirs", "dep:termion-15", "tui/termion"] tuirs-no-backend = ["tuirs"] # Other optional features search = ["dep:regex"] @@ -40,6 +40,7 @@ crossterm-025 = { package = "crossterm", version = "0.25", optional = true } ratatui = { version = "0.28", default-features = false, optional = true } regex = { version = "1", optional = true } termion = { version = "4.0", optional = true } +termion-15 = { package = "termion", version = "1.5", optional = true } termwiz = { version = "0.22.0", optional = true } tui = { version = "0.19", default-features = false, optional = true } unicode-width = "0.1.11" diff --git a/README.md b/README.md index 1253f23..e640174 100644 --- a/README.md +++ b/README.md @@ -192,8 +192,9 @@ Note that [ratatui][] support and [tui-rs][] support are exclusive. When you use [ratatui][] support by `default-features = false`. In addition to above dependencies, you also need to install [crossterm][] or [termion][] or [termwiz][] to initialize -your application and to receive key inputs. Note that version of [crossterm][] crate is different between [ratatui][] -and [tui-rs][]. Please select the correct version. +your application and to receive key inputs. Note that version of [crossterm][] crate and [termion][] crate are different +between [ratatui][] and [tui-rs][]. Please select the same dependency version. For example, [tui-rs][] depends on +[crossterm][] v0.2.5 or [termion][] v1.5 where both crates are older than [ratatui][]'s dependencies. ## Minimal Usage diff --git a/examples/tuirs_editor.rs b/examples/tuirs_editor.rs index f871ffb..2dfc881 100644 --- a/examples/tuirs_editor.rs +++ b/examples/tuirs_editor.rs @@ -1,3 +1,4 @@ +// Use `crossterm` v0.25 for `tui` backend. use crossterm_025 as crossterm; use crossterm::event::{DisableMouseCapture, EnableMouseCapture}; diff --git a/examples/tuirs_minimal.rs b/examples/tuirs_minimal.rs index 743863c..5e5cc27 100644 --- a/examples/tuirs_minimal.rs +++ b/examples/tuirs_minimal.rs @@ -1,3 +1,4 @@ +// Use `crossterm` v0.25 for `tui` backend. use crossterm_025 as crossterm; use crossterm::event::{DisableMouseCapture, EnableMouseCapture}; diff --git a/examples/tuirs_termion.rs b/examples/tuirs_termion.rs index 7429b3e..e398e3d 100644 --- a/examples/tuirs_termion.rs +++ b/examples/tuirs_termion.rs @@ -1,3 +1,6 @@ +// Use `termion` v1.5 for `tui` backend. +use termion_15 as termion; + use std::error::Error; use std::io; use std::sync::mpsc; @@ -6,7 +9,7 @@ use std::time::Duration; use termion::event::Event as TermEvent; use termion::input::{MouseTerminal, TermRead}; use termion::raw::IntoRawMode; -use termion::screen::IntoAlternateScreen; +use termion::screen::AlternateScreen; use tui::backend::TermionBackend; use tui::widgets::{Block, Borders}; use tui::Terminal; @@ -18,8 +21,9 @@ enum Event { } fn main() -> Result<(), Box> { - let stdout = io::stdout().into_raw_mode()?.into_alternate_screen()?; + let stdout = io::stdout().into_raw_mode()?; let stdout = MouseTerminal::from(stdout); + let stdout = AlternateScreen::from(stdout); let backend = TermionBackend::new(stdout); let mut term = Terminal::new(backend)?; diff --git a/src/input/termion.rs b/src/input/termion.rs index 2fabeb1..26f5f02 100644 --- a/src/input/termion.rs +++ b/src/input/termion.rs @@ -1,5 +1,5 @@ use super::{Input, Key}; -use termion::event::{Event, Key as KeyEvent, MouseButton, MouseEvent}; +use crate::termion::event::{Event, Key as KeyEvent, MouseButton, MouseEvent}; impl From for Input { /// Convert [`termion::event::Event`] into [`Input`]. @@ -22,6 +22,7 @@ impl From for Input { /// So the `shift` field of the returned `Input` instance is always `false` except for combinations with arrow keys. /// For example, `termion::event::Key::Char('A')` is converted to `Input { key: Key::Char('A'), shift: false, .. }`. fn from(key: KeyEvent) -> Self { + #[cfg(feature = "termion")] let (ctrl, alt, shift) = match key { KeyEvent::Ctrl(_) | KeyEvent::CtrlUp @@ -42,6 +43,14 @@ impl From for Input { _ => (false, false, false), }; + #[cfg(feature = "tuirs-termion")] + let (ctrl, alt, shift) = match key { + KeyEvent::Ctrl(_) => (true, false, false), + KeyEvent::Alt(_) => (false, true, false), + _ => (false, false, false), + }; + + #[cfg(feature = "termion")] let key = match key { KeyEvent::Char('\n' | '\r') => Key::Enter, KeyEvent::Char(c) | KeyEvent::Ctrl(c) | KeyEvent::Alt(c) => Key::Char(c), @@ -67,6 +76,26 @@ impl From for Input { _ => Key::Null, }; + #[cfg(feature = "tuirs-termion")] + let key = match key { + KeyEvent::Char('\n' | '\r') => Key::Enter, + KeyEvent::Char(c) | KeyEvent::Ctrl(c) | KeyEvent::Alt(c) => Key::Char(c), + KeyEvent::Backspace => Key::Backspace, + KeyEvent::Left => Key::Left, + KeyEvent::Right => Key::Right, + KeyEvent::Up => Key::Up, + KeyEvent::Down => Key::Down, + KeyEvent::Home => Key::Home, + KeyEvent::End => Key::End, + KeyEvent::PageUp => Key::PageUp, + KeyEvent::PageDown => Key::PageDown, + KeyEvent::BackTab => Key::Tab, + KeyEvent::Delete => Key::Delete, + KeyEvent::Esc => Key::Esc, + KeyEvent::F(x) => Key::F(x), + _ => Key::Null, + }; + Input { key, ctrl, diff --git a/src/lib.rs b/src/lib.rs index e37d3f0..b78aa1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,11 @@ use crossterm; #[cfg(feature = "tuirs-crossterm")] use crossterm_025 as crossterm; +#[cfg(feature = "termion")] +use termion; +#[cfg(feature = "tuirs-termion")] +use termion_15 as termion; + pub use cursor::CursorMove; pub use input::{Input, Key}; pub use scroll::Scrolling;