Skip to content

Commit

Permalink
use the same version of termion as tui's dependency for `tuirs-te…
Browse files Browse the repository at this point in the history
…rmion` feature
  • Loading branch information
rhysd committed Aug 8, 2024
1 parent 8d2c201 commit 3865fd5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions examples/tuirs_editor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Use `crossterm` v0.25 for `tui` backend.
use crossterm_025 as crossterm;

use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
Expand Down
1 change: 1 addition & 0 deletions examples/tuirs_minimal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Use `crossterm` v0.25 for `tui` backend.
use crossterm_025 as crossterm;

use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
Expand Down
8 changes: 6 additions & 2 deletions examples/tuirs_termion.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -18,8 +21,9 @@ enum Event {
}

fn main() -> Result<(), Box<dyn Error>> {
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)?;

Expand Down
31 changes: 30 additions & 1 deletion src/input/termion.rs
Original file line number Diff line number Diff line change
@@ -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<Event> for Input {
/// Convert [`termion::event::Event`] into [`Input`].
Expand All @@ -22,6 +22,7 @@ impl From<KeyEvent> 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
Expand All @@ -42,6 +43,14 @@ impl From<KeyEvent> 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),
Expand All @@ -67,6 +76,26 @@ impl From<KeyEvent> 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,
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3865fd5

Please sign in to comment.