diff --git a/Cargo.toml b/Cargo.toml index 193ecbf..d9c2645 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tui-input" -version = "0.5.1" +version = "0.6.0" edition = "2021" authors = ["Arijit Basu "] description = "TUI input library supporting multiple backends" @@ -17,7 +17,7 @@ default = ["crossterm"] [dependencies] crossterm = { version = "0.25.0", optional = true } -serde = { version = "1.0.144", optional = true, features = ["derive"] } +serde = { version = "1.0.145", optional = true, features = ["derive"] } termion = { version = "1.5.6", optional = true } [[example]] diff --git a/examples/crossterm_input.rs b/examples/crossterm_input.rs index e1b787c..7862c44 100644 --- a/examples/crossterm_input.rs +++ b/examples/crossterm_input.rs @@ -9,6 +9,7 @@ use crossterm::{ }; use std::io::{stdout, Write}; use tui_input::backend::crossterm as backend; +use tui_input::backend::crossterm::EventHandler; use tui_input::Input; fn main() -> Result<()> { @@ -30,10 +31,7 @@ fn main() -> Result<()> { break; } _ => { - if backend::to_input_request(event) - .and_then(|r| input.handle(r)) - .is_some() - { + if input.handle_event(&event).is_some() { backend::write( &mut stdout, input.value(), diff --git a/examples/termion_input.rs b/examples/termion_input.rs index 8e6a9d2..fb8486e 100644 --- a/examples/termion_input.rs +++ b/examples/termion_input.rs @@ -5,6 +5,7 @@ use termion::input::TermRead; use termion::raw::IntoRawMode; use termion::screen::AlternateScreen; use tui_input::backend::termion as backend; +use tui_input::backend::termion::EventHandler; use tui_input::Input; fn main() -> Result<()> { @@ -24,10 +25,7 @@ fn main() -> Result<()> { break; } - if backend::to_input_request(&evt) - .and_then(|req| input.handle(req)) - .is_some() - { + if input.handle_event(&evt).is_some() { backend::write(&mut stdout, input.value(), input.cursor(), (0, 0), 15)?; stdout.flush()?; } diff --git a/examples/tui-rs-input/Cargo.toml b/examples/tui-rs-input/Cargo.toml index e860770..0df043d 100644 --- a/examples/tui-rs-input/Cargo.toml +++ b/examples/tui-rs-input/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -crossterm = "0.23.2" -tui = { version = "0.18.0", features = ["crossterm"] } +crossterm = "0.25.0" +tui = { version = "0.19.0", features = ["crossterm"] } tui-input = { path = "../../" } -unicode-width = "0.1.9" +unicode-width = "0.1.10" diff --git a/examples/tui-rs-input/src/main.rs b/examples/tui-rs-input/src/main.rs index b5dfccb..28f96ab 100644 --- a/examples/tui-rs-input/src/main.rs +++ b/examples/tui-rs-input/src/main.rs @@ -16,6 +16,7 @@ use tui::{ Frame, Terminal, }; use tui_input::backend::crossterm as input_backend; +use tui_input::backend::crossterm::EventHandler; use tui_input::Input; enum InputMode { @@ -95,8 +96,7 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( app.input_mode = InputMode::Normal; } _ => { - input_backend::to_input_request(Event::Key(key)) - .and_then(|req| app.input.handle(req)); + app.input.handle_event(&Event::Key(key)); } }, } diff --git a/src/backend/crossterm.rs b/src/backend/crossterm.rs index ab5750b..166717d 100644 --- a/src/backend/crossterm.rs +++ b/src/backend/crossterm.rs @@ -1,4 +1,4 @@ -use crate::InputRequest; +use crate::{Input, InputRequest, StateChanged}; use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEvent, KeyModifiers}; use crossterm::{ cursor::MoveTo, @@ -9,7 +9,7 @@ use crossterm::{ use std::io::Write; /// Converts crossterm event into input requests. -pub fn to_input_request(evt: CrosstermEvent) -> Option { +pub fn to_input_request(evt: &CrosstermEvent) -> Option { use InputRequest::*; use KeyCode::*; match evt { @@ -18,7 +18,7 @@ pub fn to_input_request(evt: CrosstermEvent) -> Option { modifiers, kind: _, state: _, - }) => match (code, modifiers) { + }) => match (*code, *modifiers) { (Backspace, KeyModifiers::NONE) | (Char('h'), KeyModifiers::CONTROL) => { Some(DeletePrevChar) } @@ -101,6 +101,16 @@ pub fn write( Ok(()) } +pub trait EventHandler { + fn handle_event(&mut self, evt: &CrosstermEvent) -> Option; +} + +impl EventHandler for Input { + fn handle_event(&mut self, evt: &CrosstermEvent) -> Option { + to_input_request(evt).and_then(|req| self.handle(req)) + } +} + #[cfg(test)] mod tests { use crossterm::event::{KeyEventKind, KeyEventState}; @@ -116,7 +126,7 @@ mod tests { state: KeyEventState::NONE, }); - let req = to_input_request(evt); + let req = to_input_request(&evt); assert!(req.is_none()); } diff --git a/src/backend/termion.rs b/src/backend/termion.rs index fbe53c1..689b98c 100644 --- a/src/backend/termion.rs +++ b/src/backend/termion.rs @@ -1,4 +1,6 @@ use crate::input::InputRequest; +use crate::Input; +use crate::StateChanged; use std::io::{Result, Write}; use termion::cursor::Goto; use termion::event::{Event, Key}; @@ -64,6 +66,16 @@ pub fn write( Ok(()) } +pub trait EventHandler { + fn handle_event(&mut self, evt: &Event) -> Option; +} + +impl EventHandler for Input { + fn handle_event(&mut self, evt: &Event) -> Option { + to_input_request(evt).and_then(|req| self.handle(req)) + } +} + #[cfg(test)] mod tests { use super::*;