Skip to content

Commit

Permalink
Simplify API further using trait
Browse files Browse the repository at this point in the history
BREAKING: crossterm's to_input_request now takes a reference to the
event.
  • Loading branch information
sayanarijit committed Oct 4, 2022
1 parent 7df1ab6 commit 2019d94
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tui-input"
version = "0.5.1"
version = "0.6.0"
edition = "2021"
authors = ["Arijit Basu <hi@arijitbasu.in>"]
description = "TUI input library supporting multiple backends"
Expand All @@ -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]]
Expand Down
6 changes: 2 additions & 4 deletions examples/crossterm_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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(),
Expand Down
6 changes: 2 additions & 4 deletions examples/termion_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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()?;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/tui-rs-input/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions examples/tui-rs-input/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -95,8 +96,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, 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));
}
},
}
Expand Down
18 changes: 14 additions & 4 deletions src/backend/crossterm.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -9,7 +9,7 @@ use crossterm::{
use std::io::Write;

/// Converts crossterm event into input requests.
pub fn to_input_request(evt: CrosstermEvent) -> Option<InputRequest> {
pub fn to_input_request(evt: &CrosstermEvent) -> Option<InputRequest> {
use InputRequest::*;
use KeyCode::*;
match evt {
Expand All @@ -18,7 +18,7 @@ pub fn to_input_request(evt: CrosstermEvent) -> Option<InputRequest> {
modifiers,
kind: _,
state: _,
}) => match (code, modifiers) {
}) => match (*code, *modifiers) {
(Backspace, KeyModifiers::NONE) | (Char('h'), KeyModifiers::CONTROL) => {
Some(DeletePrevChar)
}
Expand Down Expand Up @@ -101,6 +101,16 @@ pub fn write<W: Write>(
Ok(())
}

pub trait EventHandler {
fn handle_event(&mut self, evt: &CrosstermEvent) -> Option<StateChanged>;
}

impl EventHandler for Input {
fn handle_event(&mut self, evt: &CrosstermEvent) -> Option<StateChanged> {
to_input_request(evt).and_then(|req| self.handle(req))
}
}

#[cfg(test)]
mod tests {
use crossterm::event::{KeyEventKind, KeyEventState};
Expand All @@ -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());
}
Expand Down
12 changes: 12 additions & 0 deletions src/backend/termion.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -64,6 +66,16 @@ pub fn write<W: Write>(
Ok(())
}

pub trait EventHandler {
fn handle_event(&mut self, evt: &Event) -> Option<StateChanged>;
}

impl EventHandler for Input {
fn handle_event(&mut self, evt: &Event) -> Option<StateChanged> {
to_input_request(evt).and_then(|req| self.handle(req))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 2019d94

Please sign in to comment.