Skip to content

Commit

Permalink
add example for usage with termwiz crate
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 20, 2023
1 parent 892b449 commit 575ed15
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 22 deletions.
67 changes: 67 additions & 0 deletions examples/termwiz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use ratatui::backend::TermwizBackend;
use ratatui::widgets::{Block, Borders};
use ratatui::Terminal;
use std::error::Error;
use std::time::{Duration, Instant};
use termwiz::input::InputEvent;
use termwiz::terminal::Terminal as TermwizTerminal;
use tui_textarea::{Input, Key, TextArea};

const TICK_RATE: Duration = Duration::from_millis(100);

fn main() -> Result<(), Box<dyn Error>> {
let backend = TermwizBackend::new()?;
let mut term = Terminal::new(backend)?;
term.hide_cursor()?;

let mut textarea = TextArea::default();
textarea.set_block(
Block::default()
.borders(Borders::ALL)
.title("Termwiz Minimal Example"),
);
let mut last_tick = Instant::now();

// The event loop
loop {
term.draw(|f| {
let widget = textarea.widget();
f.render_widget(widget, f.size());
})?;

let timeout = TICK_RATE
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));

if let Some(input) = term
.backend_mut()
.buffered_terminal_mut()
.terminal()
.poll_input(Some(timeout))?
{
if let InputEvent::Resized { cols, rows } = input {
term.backend_mut()
.buffered_terminal_mut()
.resize(cols, rows);
} else {
match input.into() {
Input { key: Key::Esc, .. } => break,
input => {
textarea.input(input);
}
}
}
}

if last_tick.elapsed() >= TICK_RATE {
last_tick = Instant::now();
}
}

term.show_cursor()?;
term.flush()?;
drop(term); // Leave terminal raw mode to print the following line

println!("Lines: {:?}", textarea.lines());
Ok(())
}
44 changes: 22 additions & 22 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use arbitrary::Arbitrary;
use termion::event::{Event as TermionEvent, Key as TermionKey, MouseEvent as TermionMouseEvent};
#[cfg(feature = "ratatui-termwiz")]
use termwiz::input::{
InputEvent as TermwizInputEvent, KeyEvent as TermwizKeyEvent, MouseEvent as TermwizMouseEvent,
PixelMouseEvent,
InputEvent as TermwizInputEvent, KeyEvent as TermwizKeyEvent,
MouseButtons as TermwizMouseButtons, MouseEvent as TermwizMouseEvent, PixelMouseEvent,
};

/// Backend-agnostic key input kind.
Expand Down Expand Up @@ -269,26 +269,34 @@ impl From<TermwizKeyEvent> for Input {
}
}

#[cfg(feature = "ratatui-termwiz")]
impl From<TermwizMouseButtons> for Key {
/// Convert [`termwiz::input::MouseButtons`] to [`Key`].
fn from(buttons: TermwizMouseButtons) -> Self {
if buttons.contains(TermwizMouseButtons::VERT_WHEEL) {
if buttons.contains(TermwizMouseButtons::WHEEL_POSITIVE) {
Key::MouseScrollUp
} else {
Key::MouseScrollDown
}
} else {
Key::Null
}
}
}

#[cfg(feature = "ratatui-termwiz")]
impl From<TermwizMouseEvent> for Input {
/// Convert [`termwiz::input::MouseEvent`] to [`Input`].
fn from(mouse: TermwizMouseEvent) -> Self {
use termwiz::input::{Modifiers, MouseButtons};
use termwiz::input::Modifiers;

let TermwizMouseEvent {
mouse_buttons,
modifiers,
..
} = mouse;
let key = if mouse_buttons.contains(MouseButtons::VERT_WHEEL) {
if mouse_buttons.contains(MouseButtons::WHEEL_POSITIVE) {
Key::MouseScrollDown
} else {
Key::MouseScrollUp
}
} else {
Key::Null
};
let key = Key::from(mouse_buttons);
let ctrl = modifiers.contains(Modifiers::CTRL);
let alt = modifiers.contains(Modifiers::ALT);

Expand All @@ -300,22 +308,14 @@ impl From<TermwizMouseEvent> for Input {
impl From<PixelMouseEvent> for Input {
/// Convert [`termwiz::input::PixelMouseEvent`] to [`Input`].
fn from(mouse: PixelMouseEvent) -> Self {
use termwiz::input::{Modifiers, MouseButtons};
use termwiz::input::Modifiers;

let PixelMouseEvent {
mouse_buttons,
modifiers,
..
} = mouse;
let key = if mouse_buttons.contains(MouseButtons::VERT_WHEEL) {
if mouse_buttons.contains(MouseButtons::WHEEL_POSITIVE) {
Key::MouseScrollDown
} else {
Key::MouseScrollUp
}
} else {
Key::Null
};
let key = Key::from(mouse_buttons);
let ctrl = modifiers.contains(Modifiers::CTRL);
let alt = modifiers.contains(Modifiers::ALT);

Expand Down

0 comments on commit 575ed15

Please sign in to comment.