tui: Carry modifiers on mouse events
b723340dcf32e38740629f456f784a121f057978
Mouse events need to preserve terminal modifier state so applications can distinguish ordinary clicks from control-clicks. Decode SGR mouse bytes in the termion backend because termion enables that mode but drops the modifier bits.
1 parent
e0d32de9
crates/diff/src/renderer.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
crates/tui-core/src/event.rs
+50 -0
| 80 | 80 | Press { |
|
| 81 | 81 | /// Pressed button. |
|
| 82 | 82 | button: MouseButton, |
|
| 83 | 83 | /// Position. |
|
| 84 | 84 | at: Point, |
|
| 85 | + | /// Active modifier keys. |
|
| 86 | + | modifiers: Modifiers, |
|
| 85 | 87 | }, |
|
| 86 | 88 | /// Button release. |
|
| 87 | 89 | Release { |
|
| 88 | 90 | /// Position. |
|
| 89 | 91 | at: Point, |
|
| 92 | + | /// Active modifier keys. |
|
| 93 | + | modifiers: Modifiers, |
|
| 90 | 94 | }, |
|
| 91 | 95 | /// Mouse drag or hold. |
|
| 92 | 96 | Hold { |
|
| 93 | 97 | /// Position. |
|
| 94 | 98 | at: Point, |
|
| 99 | + | /// Active modifier keys. |
|
| 100 | + | modifiers: Modifiers, |
|
| 95 | 101 | }, |
|
| 96 | 102 | } |
|
| 97 | 103 | ||
| 98 | 104 | /// Mouse button. |
|
| 99 | 105 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 109 | 115 | /// Wheel down. |
|
| 110 | 116 | WheelDown, |
|
| 111 | 117 | /// Unknown button. |
|
| 112 | 118 | Unknown, |
|
| 113 | 119 | } |
|
| 120 | + | ||
| 121 | + | /// Active keyboard modifiers attached to an input event. |
|
| 122 | + | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)] |
|
| 123 | + | pub struct Modifiers { |
|
| 124 | + | /// Packed modifier flags. |
|
| 125 | + | bits: u8, |
|
| 126 | + | } |
|
| 127 | + | ||
| 128 | + | impl Modifiers { |
|
| 129 | + | /// No modifiers are active. |
|
| 130 | + | pub const NONE: Self = Self { bits: 0 }; |
|
| 131 | + | /// Shift is active. |
|
| 132 | + | pub const SHIFT: Self = Self { bits: 1 << 0 }; |
|
| 133 | + | /// Alt or Meta is active. |
|
| 134 | + | pub const ALT: Self = Self { bits: 1 << 1 }; |
|
| 135 | + | /// Control is active. |
|
| 136 | + | pub const CONTROL: Self = Self { bits: 1 << 2 }; |
|
| 137 | + | ||
| 138 | + | /// Returns whether all bits in `modifier` are active. |
|
| 139 | + | pub const fn contains(self, modifier: Self) -> bool { |
|
| 140 | + | self.bits & modifier.bits == modifier.bits |
|
| 141 | + | } |
|
| 142 | + | ||
| 143 | + | /// Returns whether no modifiers are active. |
|
| 144 | + | pub const fn is_empty(self) -> bool { |
|
| 145 | + | self.bits == 0 |
|
| 146 | + | } |
|
| 147 | + | } |
|
| 148 | + | ||
| 149 | + | impl std::ops::BitOr for Modifiers { |
|
| 150 | + | type Output = Self; |
|
| 151 | + | ||
| 152 | + | fn bitor(self, rhs: Self) -> Self::Output { |
|
| 153 | + | Self { |
|
| 154 | + | bits: self.bits | rhs.bits, |
|
| 155 | + | } |
|
| 156 | + | } |
|
| 157 | + | } |
|
| 158 | + | ||
| 159 | + | impl std::ops::BitOrAssign for Modifiers { |
|
| 160 | + | fn bitor_assign(&mut self, rhs: Self) { |
|
| 161 | + | self.bits |= rhs.bits; |
|
| 162 | + | } |
|
| 163 | + | } |
crates/tui-core/src/lib.rs
+1 -1
| 62 | 62 | pub use app::{App, Control, drive}; |
|
| 63 | 63 | pub use backend::{Backend, TestBackend}; |
|
| 64 | 64 | pub use border::{Border, BorderChars}; |
|
| 65 | 65 | pub use buffer::{Buffer, Cell}; |
|
| 66 | 66 | pub use cell::Text; |
|
| 67 | - | pub use event::{Event, Key, Mouse, MouseButton}; |
|
| 67 | + | pub use event::{Event, Key, Modifiers, Mouse, MouseButton}; |
|
| 68 | 68 | pub use frame::Frame; |
|
| 69 | 69 | pub use hstack::HStack; |
|
| 70 | 70 | pub use label::{Label, label}; |
|
| 71 | 71 | pub use layout::{Direction, Length, Point, Rect}; |
|
| 72 | 72 | pub use primitive::{Constraint, Line, Size, StyledLine}; |
crates/tui-termion/src/backend.rs
added
+174 -0
| 1 | + | //! Termion-backed terminal output. |
|
| 2 | + | ||
| 3 | + | use std::io::{self, Write}; |
|
| 4 | + | ||
| 5 | + | use rustik_tui_core as tui_core; |
|
| 6 | + | use tui_core::backend::Backend; |
|
| 7 | + | use tui_core::buffer::DirtyRun; |
|
| 8 | + | use tui_core::layout::Point; |
|
| 9 | + | use tui_core::{PaintMode, Size}; |
|
| 10 | + | ||
| 11 | + | /// Termion-backed terminal output. |
|
| 12 | + | #[derive(Debug)] |
|
| 13 | + | pub struct TermionBackend<W> { |
|
| 14 | + | writer: W, |
|
| 15 | + | paint: PaintMode, |
|
| 16 | + | } |
|
| 17 | + | ||
| 18 | + | impl<W> TermionBackend<W> { |
|
| 19 | + | /// Creates a backend that writes ANSI styling escapes. |
|
| 20 | + | pub fn new(writer: W) -> Self { |
|
| 21 | + | Self { |
|
| 22 | + | writer, |
|
| 23 | + | paint: PaintMode::Styled, |
|
| 24 | + | } |
|
| 25 | + | } |
|
| 26 | + | ||
| 27 | + | /// Creates a backend that writes no ANSI styling escapes. |
|
| 28 | + | pub fn plain(writer: W) -> Self { |
|
| 29 | + | Self { |
|
| 30 | + | writer, |
|
| 31 | + | paint: PaintMode::Plain, |
|
| 32 | + | } |
|
| 33 | + | } |
|
| 34 | + | ||
| 35 | + | /// Returns the wrapped writer. |
|
| 36 | + | pub fn into_inner(self) -> W { |
|
| 37 | + | self.writer |
|
| 38 | + | } |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | impl<W: Write> Backend for TermionBackend<W> { |
|
| 42 | + | type Error = io::Error; |
|
| 43 | + | ||
| 44 | + | fn size(&mut self) -> Result<Size, Self::Error> { |
|
| 45 | + | let (cols, rows) = termion::terminal_size()?; |
|
| 46 | + | ||
| 47 | + | Ok(Size::new(cols as usize, rows as usize)) |
|
| 48 | + | } |
|
| 49 | + | ||
| 50 | + | fn draw(&mut self, runs: &[DirtyRun<'_>]) -> Result<(), Self::Error> { |
|
| 51 | + | for run in runs { |
|
| 52 | + | write!( |
|
| 53 | + | self.writer, |
|
| 54 | + | "{}", |
|
| 55 | + | termion::cursor::Goto(coord(run.x), coord(run.y)) |
|
| 56 | + | )?; |
|
| 57 | + | for cell in run.cells { |
|
| 58 | + | if cell.is_continuation() { |
|
| 59 | + | continue; |
|
| 60 | + | } |
|
| 61 | + | let paint = cell.paint(); |
|
| 62 | + | write!(self.writer, "{}", paint.rendered(self.paint))?; |
|
| 63 | + | } |
|
| 64 | + | } |
|
| 65 | + | Ok(()) |
|
| 66 | + | } |
|
| 67 | + | ||
| 68 | + | fn clear(&mut self) -> Result<(), Self::Error> { |
|
| 69 | + | write!(self.writer, "{}", termion::clear::All) |
|
| 70 | + | } |
|
| 71 | + | ||
| 72 | + | fn flush(&mut self) -> Result<(), Self::Error> { |
|
| 73 | + | self.writer.flush() |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | fn set_cursor_position(&mut self, point: Point) -> Result<(), Self::Error> { |
|
| 77 | + | write!( |
|
| 78 | + | self.writer, |
|
| 79 | + | "{}", |
|
| 80 | + | termion::cursor::Goto(coord(point.x), coord(point.y)) |
|
| 81 | + | ) |
|
| 82 | + | } |
|
| 83 | + | ||
| 84 | + | fn set_cursor_visible(&mut self, visible: bool) -> Result<(), Self::Error> { |
|
| 85 | + | if visible { |
|
| 86 | + | write!(self.writer, "{}", termion::cursor::Show) |
|
| 87 | + | } else { |
|
| 88 | + | write!(self.writer, "{}", termion::cursor::Hide) |
|
| 89 | + | } |
|
| 90 | + | } |
|
| 91 | + | } |
|
| 92 | + | ||
| 93 | + | /// Converts a zero-based coordinate into a terminal coordinate. |
|
| 94 | + | fn coord(value: usize) -> u16 { |
|
| 95 | + | u16::try_from(value.saturating_add(1)).unwrap_or(u16::MAX) |
|
| 96 | + | } |
|
| 97 | + | ||
| 98 | + | #[cfg(test)] |
|
| 99 | + | #[allow(clippy::unwrap_used)] |
|
| 100 | + | mod tests { |
|
| 101 | + | use super::*; |
|
| 102 | + | use tui_core::{Buffer, Color, Rect, Style}; |
|
| 103 | + | ||
| 104 | + | #[test] |
|
| 105 | + | fn termion_backend_writes_dirty_runs_with_one_based_coordinates() { |
|
| 106 | + | let cells = [tui_core::buffer::Cell::new('x', Style::default())]; |
|
| 107 | + | let run = DirtyRun { |
|
| 108 | + | x: 2, |
|
| 109 | + | y: 3, |
|
| 110 | + | cells: &cells, |
|
| 111 | + | }; |
|
| 112 | + | let mut backend = TermionBackend::new(Vec::new()); |
|
| 113 | + | ||
| 114 | + | backend.draw(&[run]).unwrap(); |
|
| 115 | + | backend.flush().unwrap(); |
|
| 116 | + | let out = String::from_utf8(backend.into_inner()).unwrap(); |
|
| 117 | + | ||
| 118 | + | assert!(out.starts_with("\x1B[4;3H")); |
|
| 119 | + | assert!(out.ends_with('x')); |
|
| 120 | + | } |
|
| 121 | + | ||
| 122 | + | #[test] |
|
| 123 | + | fn termion_backend_paint_mode_is_explicit() { |
|
| 124 | + | let cells = [tui_core::buffer::Cell::new('x', Color::Red.style())]; |
|
| 125 | + | let run = DirtyRun { |
|
| 126 | + | x: 0, |
|
| 127 | + | y: 0, |
|
| 128 | + | cells: &cells, |
|
| 129 | + | }; |
|
| 130 | + | ||
| 131 | + | let mut styled = TermionBackend::new(Vec::new()); |
|
| 132 | + | styled.draw(&[run]).unwrap(); |
|
| 133 | + | assert!( |
|
| 134 | + | String::from_utf8(styled.into_inner()) |
|
| 135 | + | .unwrap() |
|
| 136 | + | .contains("\x1B[31m") |
|
| 137 | + | ); |
|
| 138 | + | ||
| 139 | + | let mut plain = TermionBackend::plain(Vec::new()); |
|
| 140 | + | plain.draw(&[run]).unwrap(); |
|
| 141 | + | let out = String::from_utf8(plain.into_inner()).unwrap(); |
|
| 142 | + | ||
| 143 | + | assert_eq!(out, "\x1B[1;1Hx"); |
|
| 144 | + | } |
|
| 145 | + | ||
| 146 | + | #[test] |
|
| 147 | + | fn termion_backend_does_not_write_wide_continuation_cells() { |
|
| 148 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 1)); |
|
| 149 | + | buffer.reset_dirty(); |
|
| 150 | + | buffer.set_str(0, 0, "๐a", Style::default()); |
|
| 151 | + | let runs = buffer.dirty_runs(); |
|
| 152 | + | let mut backend = TermionBackend::new(Vec::new()); |
|
| 153 | + | ||
| 154 | + | backend.draw(&runs).unwrap(); |
|
| 155 | + | let out = String::from_utf8(backend.into_inner()).unwrap(); |
|
| 156 | + | ||
| 157 | + | assert!(out.ends_with("๐a")); |
|
| 158 | + | assert!(!out.ends_with("๐ a")); |
|
| 159 | + | } |
|
| 160 | + | ||
| 161 | + | #[test] |
|
| 162 | + | fn termion_backend_cursor_and_visibility_commands_are_written() { |
|
| 163 | + | let mut backend = TermionBackend::new(Vec::new()); |
|
| 164 | + | ||
| 165 | + | backend.set_cursor_visible(false).unwrap(); |
|
| 166 | + | backend.set_cursor_position(Point::new(1, 2)).unwrap(); |
|
| 167 | + | backend.set_cursor_visible(true).unwrap(); |
|
| 168 | + | let out = String::from_utf8(backend.into_inner()).unwrap(); |
|
| 169 | + | ||
| 170 | + | assert!(out.contains("\x1B[?25l")); |
|
| 171 | + | assert!(out.contains("\x1B[3;2H")); |
|
| 172 | + | assert!(out.contains("\x1B[?25h")); |
|
| 173 | + | } |
|
| 174 | + | } |
crates/tui-termion/src/event.rs
added
+293 -0
| 1 | + | //! Termion event conversion. |
|
| 2 | + | //! |
|
| 3 | + | //! Termion's parsed events are translated into typed `rustik-tui` events. The |
|
| 4 | + | //! public entry point is [`TermionEvent`], which pairs the parsed event with |
|
| 5 | + | //! its raw bytes so that SGR mouse sequences (carrying modifier state termion |
|
| 6 | + | //! drops) can be recovered. |
|
| 7 | + | ||
| 8 | + | use rustik_tui_core as tui_core; |
|
| 9 | + | use tui_core::layout::Point; |
|
| 10 | + | use tui_core::{Event, Key, Modifiers, Mouse, MouseButton}; |
|
| 11 | + | ||
| 12 | + | /// Termion input event paired with its source bytes. |
|
| 13 | + | #[derive(Debug, Clone, PartialEq, Eq)] |
|
| 14 | + | pub struct TermionEvent { |
|
| 15 | + | /// Parsed termion event. |
|
| 16 | + | event: termion::event::Event, |
|
| 17 | + | /// Raw bytes that produced the event. |
|
| 18 | + | raw: Vec<u8>, |
|
| 19 | + | } |
|
| 20 | + | ||
| 21 | + | impl From<TermionEvent> for Event<()> { |
|
| 22 | + | fn from(event: TermionEvent) -> Self { |
|
| 23 | + | if let Some(mouse) = sgr_mouse_event(&event.raw) { |
|
| 24 | + | Event::Mouse(mouse) |
|
| 25 | + | } else { |
|
| 26 | + | convert_event(event.event) |
|
| 27 | + | } |
|
| 28 | + | } |
|
| 29 | + | } |
|
| 30 | + | ||
| 31 | + | impl From<(termion::event::Event, Vec<u8>)> for TermionEvent { |
|
| 32 | + | fn from((event, raw): (termion::event::Event, Vec<u8>)) -> Self { |
|
| 33 | + | Self { event, raw } |
|
| 34 | + | } |
|
| 35 | + | } |
|
| 36 | + | ||
| 37 | + | /// Converts a termion event into a typed `rustik-tui` event. |
|
| 38 | + | fn convert_event(event: termion::event::Event) -> Event<()> { |
|
| 39 | + | match event { |
|
| 40 | + | termion::event::Event::Key(key) => Event::Key(convert_key(key)), |
|
| 41 | + | termion::event::Event::Mouse(mouse) => Event::Mouse(convert_mouse(mouse)), |
|
| 42 | + | termion::event::Event::Unsupported(_) => Event::Unsupported, |
|
| 43 | + | } |
|
| 44 | + | } |
|
| 45 | + | ||
| 46 | + | /// Converts a termion key into a typed `rustik-tui` key. |
|
| 47 | + | fn convert_key(key: termion::event::Key) -> Key { |
|
| 48 | + | match key { |
|
| 49 | + | termion::event::Key::Backspace => Key::Backspace, |
|
| 50 | + | termion::event::Key::Left => Key::Left, |
|
| 51 | + | termion::event::Key::Right => Key::Right, |
|
| 52 | + | termion::event::Key::Up => Key::Up, |
|
| 53 | + | termion::event::Key::Down => Key::Down, |
|
| 54 | + | termion::event::Key::Home => Key::Home, |
|
| 55 | + | termion::event::Key::End => Key::End, |
|
| 56 | + | termion::event::Key::PageUp => Key::PageUp, |
|
| 57 | + | termion::event::Key::PageDown => Key::PageDown, |
|
| 58 | + | termion::event::Key::BackTab => Key::BackTab, |
|
| 59 | + | termion::event::Key::Delete => Key::Delete, |
|
| 60 | + | termion::event::Key::Insert => Key::Insert, |
|
| 61 | + | termion::event::Key::F(n) => Key::F(n), |
|
| 62 | + | termion::event::Key::Char('\n') => Key::Enter, |
|
| 63 | + | termion::event::Key::Char('\t') => Key::Tab, |
|
| 64 | + | termion::event::Key::Char(ch) => Key::Char(ch), |
|
| 65 | + | termion::event::Key::Alt(ch) => Key::Alt(ch), |
|
| 66 | + | termion::event::Key::Ctrl(ch) => Key::Ctrl(ch), |
|
| 67 | + | termion::event::Key::Null => Key::Null, |
|
| 68 | + | termion::event::Key::Esc => Key::Esc, |
|
| 69 | + | _ => Key::Unknown, |
|
| 70 | + | } |
|
| 71 | + | } |
|
| 72 | + | ||
| 73 | + | /// Converts a termion mouse event into a typed `rustik-tui` mouse event. |
|
| 74 | + | fn convert_mouse(event: termion::event::MouseEvent) -> Mouse { |
|
| 75 | + | match event { |
|
| 76 | + | termion::event::MouseEvent::Press(button, x, y) => Mouse::Press { |
|
| 77 | + | button: convert_button(button), |
|
| 78 | + | at: point(x, y), |
|
| 79 | + | modifiers: Modifiers::NONE, |
|
| 80 | + | }, |
|
| 81 | + | termion::event::MouseEvent::Release(x, y) => Mouse::Release { |
|
| 82 | + | at: point(x, y), |
|
| 83 | + | modifiers: Modifiers::NONE, |
|
| 84 | + | }, |
|
| 85 | + | termion::event::MouseEvent::Hold(x, y) => Mouse::Hold { |
|
| 86 | + | at: point(x, y), |
|
| 87 | + | modifiers: Modifiers::NONE, |
|
| 88 | + | }, |
|
| 89 | + | } |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | /// Converts a termion mouse button into a typed `rustik-tui` mouse button. |
|
| 93 | + | fn convert_button(button: termion::event::MouseButton) -> MouseButton { |
|
| 94 | + | match button { |
|
| 95 | + | termion::event::MouseButton::Left => MouseButton::Left, |
|
| 96 | + | termion::event::MouseButton::Right => MouseButton::Right, |
|
| 97 | + | termion::event::MouseButton::Middle => MouseButton::Middle, |
|
| 98 | + | termion::event::MouseButton::WheelUp => MouseButton::WheelUp, |
|
| 99 | + | termion::event::MouseButton::WheelDown => MouseButton::WheelDown, |
|
| 100 | + | _ => MouseButton::Unknown, |
|
| 101 | + | } |
|
| 102 | + | } |
|
| 103 | + | ||
| 104 | + | /// Converts one-based terminal coordinates to a zero-based point. |
|
| 105 | + | fn point(x: u16, y: u16) -> Point { |
|
| 106 | + | Point::new(x.saturating_sub(1) as usize, y.saturating_sub(1) as usize) |
|
| 107 | + | } |
|
| 108 | + | ||
| 109 | + | /// Converts an SGR mouse byte sequence into a typed mouse event. |
|
| 110 | + | fn sgr_mouse_event(raw: &[u8]) -> Option<Mouse> { |
|
| 111 | + | if !raw.starts_with(b"\x1B[<") { |
|
| 112 | + | return None; |
|
| 113 | + | } |
|
| 114 | + | let final_byte = *raw.last()?; |
|
| 115 | + | if final_byte != b'M' && final_byte != b'm' { |
|
| 116 | + | return None; |
|
| 117 | + | } |
|
| 118 | + | let body = std::str::from_utf8(&raw[3..raw.len().saturating_sub(1)]).ok()?; |
|
| 119 | + | let mut parts = body.split(';'); |
|
| 120 | + | let cb = parts.next()?.parse::<u16>().ok()?; |
|
| 121 | + | let x = parts.next()?.parse::<u16>().ok()?; |
|
| 122 | + | let y = parts.next()?.parse::<u16>().ok()?; |
|
| 123 | + | let button_number = (cb & 0b0000_0011) | ((cb & 0b1100_0000) >> 4); |
|
| 124 | + | let dragging = cb & 0b0010_0000 != 0; |
|
| 125 | + | let mut modifiers = Modifiers::NONE; |
|
| 126 | + | let at = point(x, y); |
|
| 127 | + | ||
| 128 | + | if cb & 0b0000_0100 != 0 { |
|
| 129 | + | modifiers |= Modifiers::SHIFT; |
|
| 130 | + | } |
|
| 131 | + | if cb & 0b0000_1000 != 0 { |
|
| 132 | + | modifiers |= Modifiers::ALT; |
|
| 133 | + | } |
|
| 134 | + | if cb & 0b0001_0000 != 0 { |
|
| 135 | + | modifiers |= Modifiers::CONTROL; |
|
| 136 | + | } |
|
| 137 | + | ||
| 138 | + | match (button_number, dragging, final_byte) { |
|
| 139 | + | (0..=2, true, _) => Some(Mouse::Hold { at, modifiers }), |
|
| 140 | + | (3, _, _) | (0..=5, false, b'm') => Some(Mouse::Release { at, modifiers }), |
|
| 141 | + | (0, false, b'M') => Some(Mouse::Press { |
|
| 142 | + | button: MouseButton::Left, |
|
| 143 | + | at, |
|
| 144 | + | modifiers, |
|
| 145 | + | }), |
|
| 146 | + | (1, false, b'M') => Some(Mouse::Press { |
|
| 147 | + | button: MouseButton::Middle, |
|
| 148 | + | at, |
|
| 149 | + | modifiers, |
|
| 150 | + | }), |
|
| 151 | + | (2, false, b'M') => Some(Mouse::Press { |
|
| 152 | + | button: MouseButton::Right, |
|
| 153 | + | at, |
|
| 154 | + | modifiers, |
|
| 155 | + | }), |
|
| 156 | + | (4, false, b'M') => Some(Mouse::Press { |
|
| 157 | + | button: MouseButton::WheelUp, |
|
| 158 | + | at, |
|
| 159 | + | modifiers, |
|
| 160 | + | }), |
|
| 161 | + | (5, false, b'M') => Some(Mouse::Press { |
|
| 162 | + | button: MouseButton::WheelDown, |
|
| 163 | + | at, |
|
| 164 | + | modifiers, |
|
| 165 | + | }), |
|
| 166 | + | _ => None, |
|
| 167 | + | } |
|
| 168 | + | } |
|
| 169 | + | ||
| 170 | + | #[cfg(test)] |
|
| 171 | + | #[allow(clippy::unwrap_used)] |
|
| 172 | + | mod tests { |
|
| 173 | + | use super::*; |
|
| 174 | + | ||
| 175 | + | #[test] |
|
| 176 | + | fn termion_keys_are_typed() { |
|
| 177 | + | assert_eq!(convert_key(termion::event::Key::Char('\n')), Key::Enter); |
|
| 178 | + | assert_eq!(convert_key(termion::event::Key::Ctrl('c')), Key::Ctrl('c')); |
|
| 179 | + | } |
|
| 180 | + | ||
| 181 | + | #[test] |
|
| 182 | + | fn all_common_termion_keys_are_mapped() { |
|
| 183 | + | assert_eq!(convert_key(termion::event::Key::Backspace), Key::Backspace); |
|
| 184 | + | assert_eq!(convert_key(termion::event::Key::Left), Key::Left); |
|
| 185 | + | assert_eq!(convert_key(termion::event::Key::Right), Key::Right); |
|
| 186 | + | assert_eq!(convert_key(termion::event::Key::Up), Key::Up); |
|
| 187 | + | assert_eq!(convert_key(termion::event::Key::Down), Key::Down); |
|
| 188 | + | assert_eq!(convert_key(termion::event::Key::Home), Key::Home); |
|
| 189 | + | assert_eq!(convert_key(termion::event::Key::End), Key::End); |
|
| 190 | + | assert_eq!(convert_key(termion::event::Key::PageUp), Key::PageUp); |
|
| 191 | + | assert_eq!(convert_key(termion::event::Key::PageDown), Key::PageDown); |
|
| 192 | + | assert_eq!(convert_key(termion::event::Key::BackTab), Key::BackTab); |
|
| 193 | + | assert_eq!(convert_key(termion::event::Key::Delete), Key::Delete); |
|
| 194 | + | assert_eq!(convert_key(termion::event::Key::Insert), Key::Insert); |
|
| 195 | + | assert_eq!(convert_key(termion::event::Key::F(5)), Key::F(5)); |
|
| 196 | + | assert_eq!(convert_key(termion::event::Key::Char('\t')), Key::Tab); |
|
| 197 | + | assert_eq!(convert_key(termion::event::Key::Alt('x')), Key::Alt('x')); |
|
| 198 | + | assert_eq!(convert_key(termion::event::Key::Null), Key::Null); |
|
| 199 | + | assert_eq!(convert_key(termion::event::Key::Esc), Key::Esc); |
|
| 200 | + | } |
|
| 201 | + | ||
| 202 | + | #[test] |
|
| 203 | + | fn termion_events_are_mapped() { |
|
| 204 | + | let key = termion::event::Event::Key(termion::event::Key::Char('x')); |
|
| 205 | + | let mouse = termion::event::Event::Mouse(termion::event::MouseEvent::Release(2, 3)); |
|
| 206 | + | let unsupported = termion::event::Event::Unsupported(vec![1, 2, 3]); |
|
| 207 | + | ||
| 208 | + | assert_eq!(convert_event(key), Event::Key(Key::Char('x'))); |
|
| 209 | + | assert_eq!( |
|
| 210 | + | convert_event(mouse), |
|
| 211 | + | Event::Mouse(Mouse::Release { |
|
| 212 | + | at: Point::new(1, 2), |
|
| 213 | + | modifiers: Modifiers::NONE, |
|
| 214 | + | }) |
|
| 215 | + | ); |
|
| 216 | + | assert_eq!(convert_event(unsupported), Event::Unsupported); |
|
| 217 | + | } |
|
| 218 | + | ||
| 219 | + | #[test] |
|
| 220 | + | fn termion_mouse_events_are_zero_based() { |
|
| 221 | + | assert_eq!( |
|
| 222 | + | convert_mouse(termion::event::MouseEvent::Press( |
|
| 223 | + | termion::event::MouseButton::Left, |
|
| 224 | + | 1, |
|
| 225 | + | 2, |
|
| 226 | + | )), |
|
| 227 | + | Mouse::Press { |
|
| 228 | + | button: MouseButton::Left, |
|
| 229 | + | at: Point::new(0, 1), |
|
| 230 | + | modifiers: Modifiers::NONE, |
|
| 231 | + | } |
|
| 232 | + | ); |
|
| 233 | + | assert_eq!( |
|
| 234 | + | convert_mouse(termion::event::MouseEvent::Hold(3, 4)), |
|
| 235 | + | Mouse::Hold { |
|
| 236 | + | at: Point::new(2, 3), |
|
| 237 | + | modifiers: Modifiers::NONE, |
|
| 238 | + | } |
|
| 239 | + | ); |
|
| 240 | + | } |
|
| 241 | + | ||
| 242 | + | #[test] |
|
| 243 | + | fn sgr_mouse_events_preserve_modifiers() { |
|
| 244 | + | let press = |bytes: &[u8]| { |
|
| 245 | + | let event = TermionEvent::from(( |
|
| 246 | + | termion::event::Event::Unsupported(bytes.to_vec()), |
|
| 247 | + | bytes.to_vec(), |
|
| 248 | + | )); |
|
| 249 | + | Event::from(event) |
|
| 250 | + | }; |
|
| 251 | + | ||
| 252 | + | assert_eq!( |
|
| 253 | + | press(b"\x1B[<16;3;4M"), |
|
| 254 | + | Event::Mouse(Mouse::Press { |
|
| 255 | + | button: MouseButton::Left, |
|
| 256 | + | at: Point::new(2, 3), |
|
| 257 | + | modifiers: Modifiers::CONTROL, |
|
| 258 | + | }) |
|
| 259 | + | ); |
|
| 260 | + | assert_eq!( |
|
| 261 | + | press(b"\x1B[<0;1;2M"), |
|
| 262 | + | Event::Mouse(Mouse::Press { |
|
| 263 | + | button: MouseButton::Left, |
|
| 264 | + | at: Point::new(0, 1), |
|
| 265 | + | modifiers: Modifiers::NONE, |
|
| 266 | + | }) |
|
| 267 | + | ); |
|
| 268 | + | } |
|
| 269 | + | ||
| 270 | + | #[test] |
|
| 271 | + | fn mouse_buttons_are_mapped() { |
|
| 272 | + | assert_eq!( |
|
| 273 | + | convert_button(termion::event::MouseButton::Left), |
|
| 274 | + | MouseButton::Left |
|
| 275 | + | ); |
|
| 276 | + | assert_eq!( |
|
| 277 | + | convert_button(termion::event::MouseButton::Right), |
|
| 278 | + | MouseButton::Right |
|
| 279 | + | ); |
|
| 280 | + | assert_eq!( |
|
| 281 | + | convert_button(termion::event::MouseButton::Middle), |
|
| 282 | + | MouseButton::Middle |
|
| 283 | + | ); |
|
| 284 | + | assert_eq!( |
|
| 285 | + | convert_button(termion::event::MouseButton::WheelUp), |
|
| 286 | + | MouseButton::WheelUp |
|
| 287 | + | ); |
|
| 288 | + | assert_eq!( |
|
| 289 | + | convert_button(termion::event::MouseButton::WheelDown), |
|
| 290 | + | MouseButton::WheelDown |
|
| 291 | + | ); |
|
| 292 | + | } |
|
| 293 | + | } |
crates/tui-termion/src/lib.rs
+6 -350
| 1 | - | //! Termion integration for the `rustik_tui` crate. |
|
| 1 | + | //! Termion integration for the `rustik-tui` crate. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! This crate keeps terminal I/O and termion event conversion outside the core |
|
| 4 | 4 | //! crate. It provides a concrete backend plus a `run` helper for wiring raw |
|
| 5 | 5 | //! terminal sessions to the core app/update/render loop. |
|
| 6 | 6 | //! |
|
| 7 | 7 | #![deny(unsafe_code)] |
|
| 8 | 8 | #![warn(missing_docs)] |
|
| 9 | 9 | #![warn(clippy::unwrap_used)] |
|
| 10 | 10 | ||
| 11 | - | use std::io::{self, Write}; |
|
| 12 | - | ||
| 13 | - | use rustik_tui_core as tui_core; |
|
| 14 | - | use tui_core::backend::Backend; |
|
| 15 | - | use tui_core::buffer::DirtyRun; |
|
| 16 | - | use tui_core::layout::Point; |
|
| 17 | - | use tui_core::{Event, Key, Mouse, MouseButton, PaintMode, Size}; |
|
| 18 | - | ||
| 19 | - | /// Runs an application on stdout using termion. |
|
| 20 | - | pub use term::run; |
|
| 11 | + | mod backend; |
|
| 12 | + | mod event; |
|
| 21 | 13 | ||
| 22 | 14 | /// Termion session helpers. |
|
| 23 | 15 | pub mod term; |
|
| 24 | 16 | ||
| 25 | - | /// Termion-backed terminal output. |
|
| 26 | - | #[derive(Debug)] |
|
| 27 | - | pub struct TermionBackend<W> { |
|
| 28 | - | writer: W, |
|
| 29 | - | paint: PaintMode, |
|
| 30 | - | } |
|
| 31 | - | ||
| 32 | - | impl<W> TermionBackend<W> { |
|
| 33 | - | /// Creates a backend from a writer. |
|
| 34 | - | pub fn new(writer: W) -> Self { |
|
| 35 | - | Self::with_paint(writer, PaintMode::Styled) |
|
| 36 | - | } |
|
| 37 | - | ||
| 38 | - | /// Creates a backend from a writer and paint mode. |
|
| 39 | - | pub fn with_paint(writer: W, paint: PaintMode) -> Self { |
|
| 40 | - | Self { writer, paint } |
|
| 41 | - | } |
|
| 42 | - | ||
| 43 | - | /// Creates a backend that writes no ANSI styling escapes. |
|
| 44 | - | pub fn plain(writer: W) -> Self { |
|
| 45 | - | Self::with_paint(writer, PaintMode::Plain) |
|
| 46 | - | } |
|
| 47 | - | ||
| 48 | - | /// Returns the current paint mode. |
|
| 49 | - | pub const fn paint_mode(&self) -> PaintMode { |
|
| 50 | - | self.paint |
|
| 51 | - | } |
|
| 52 | - | ||
| 53 | - | /// Sets the paint mode. |
|
| 54 | - | pub fn set_paint_mode(&mut self, paint: PaintMode) { |
|
| 55 | - | self.paint = paint; |
|
| 56 | - | } |
|
| 57 | - | ||
| 58 | - | /// Returns the wrapped writer. |
|
| 59 | - | pub fn into_inner(self) -> W { |
|
| 60 | - | self.writer |
|
| 61 | - | } |
|
| 62 | - | ||
| 63 | - | /// Returns a mutable writer reference. |
|
| 64 | - | pub fn writer_mut(&mut self) -> &mut W { |
|
| 65 | - | &mut self.writer |
|
| 66 | - | } |
|
| 67 | - | } |
|
| 68 | - | ||
| 69 | - | impl<W: Write> Backend for TermionBackend<W> { |
|
| 70 | - | type Error = io::Error; |
|
| 71 | - | ||
| 72 | - | fn size(&mut self) -> Result<Size, Self::Error> { |
|
| 73 | - | let (cols, rows) = termion::terminal_size()?; |
|
| 74 | - | ||
| 75 | - | Ok(Size::new(cols as usize, rows as usize)) |
|
| 76 | - | } |
|
| 77 | - | ||
| 78 | - | fn draw(&mut self, runs: &[DirtyRun<'_>]) -> Result<(), Self::Error> { |
|
| 79 | - | for run in runs { |
|
| 80 | - | write!( |
|
| 81 | - | self.writer, |
|
| 82 | - | "{}", |
|
| 83 | - | termion::cursor::Goto(coord(run.x), coord(run.y)) |
|
| 84 | - | )?; |
|
| 85 | - | for cell in run.cells { |
|
| 86 | - | if cell.is_continuation() { |
|
| 87 | - | continue; |
|
| 88 | - | } |
|
| 89 | - | let paint = cell.paint(); |
|
| 90 | - | write!(self.writer, "{}", paint.rendered(self.paint))?; |
|
| 91 | - | } |
|
| 92 | - | } |
|
| 93 | - | Ok(()) |
|
| 94 | - | } |
|
| 95 | - | ||
| 96 | - | fn clear(&mut self) -> Result<(), Self::Error> { |
|
| 97 | - | write!(self.writer, "{}", termion::clear::All) |
|
| 98 | - | } |
|
| 99 | - | ||
| 100 | - | fn flush(&mut self) -> Result<(), Self::Error> { |
|
| 101 | - | self.writer.flush() |
|
| 102 | - | } |
|
| 103 | - | ||
| 104 | - | fn set_cursor_position(&mut self, point: Point) -> Result<(), Self::Error> { |
|
| 105 | - | write!( |
|
| 106 | - | self.writer, |
|
| 107 | - | "{}", |
|
| 108 | - | termion::cursor::Goto(coord(point.x), coord(point.y)) |
|
| 109 | - | ) |
|
| 110 | - | } |
|
| 111 | - | ||
| 112 | - | fn set_cursor_visible(&mut self, visible: bool) -> Result<(), Self::Error> { |
|
| 113 | - | if visible { |
|
| 114 | - | write!(self.writer, "{}", termion::cursor::Show) |
|
| 115 | - | } else { |
|
| 116 | - | write!(self.writer, "{}", termion::cursor::Hide) |
|
| 117 | - | } |
|
| 118 | - | } |
|
| 119 | - | } |
|
| 120 | - | ||
| 121 | - | /// Converts a termion event into a typed `rustik_tui` event. |
|
| 122 | - | pub fn event(event: termion::event::Event) -> Event<()> { |
|
| 123 | - | match event { |
|
| 124 | - | termion::event::Event::Key(key) => Event::Key(key_event(key)), |
|
| 125 | - | termion::event::Event::Mouse(mouse) => Event::Mouse(mouse_event(mouse)), |
|
| 126 | - | termion::event::Event::Unsupported(_) => Event::Unsupported, |
|
| 127 | - | } |
|
| 128 | - | } |
|
| 129 | - | ||
| 130 | - | /// Converts a termion key into a typed `rustik_tui` key. |
|
| 131 | - | pub fn key_event(key: termion::event::Key) -> Key { |
|
| 132 | - | match key { |
|
| 133 | - | termion::event::Key::Backspace => Key::Backspace, |
|
| 134 | - | termion::event::Key::Left => Key::Left, |
|
| 135 | - | termion::event::Key::Right => Key::Right, |
|
| 136 | - | termion::event::Key::Up => Key::Up, |
|
| 137 | - | termion::event::Key::Down => Key::Down, |
|
| 138 | - | termion::event::Key::Home => Key::Home, |
|
| 139 | - | termion::event::Key::End => Key::End, |
|
| 140 | - | termion::event::Key::PageUp => Key::PageUp, |
|
| 141 | - | termion::event::Key::PageDown => Key::PageDown, |
|
| 142 | - | termion::event::Key::BackTab => Key::BackTab, |
|
| 143 | - | termion::event::Key::Delete => Key::Delete, |
|
| 144 | - | termion::event::Key::Insert => Key::Insert, |
|
| 145 | - | termion::event::Key::F(n) => Key::F(n), |
|
| 146 | - | termion::event::Key::Char('\n') => Key::Enter, |
|
| 147 | - | termion::event::Key::Char('\t') => Key::Tab, |
|
| 148 | - | termion::event::Key::Char(ch) => Key::Char(ch), |
|
| 149 | - | termion::event::Key::Alt(ch) => Key::Alt(ch), |
|
| 150 | - | termion::event::Key::Ctrl(ch) => Key::Ctrl(ch), |
|
| 151 | - | termion::event::Key::Null => Key::Null, |
|
| 152 | - | termion::event::Key::Esc => Key::Esc, |
|
| 153 | - | _ => Key::Unknown, |
|
| 154 | - | } |
|
| 155 | - | } |
|
| 156 | - | ||
| 157 | - | /// Converts a termion mouse event into a typed `rustik_tui` mouse event. |
|
| 158 | - | pub fn mouse_event(event: termion::event::MouseEvent) -> Mouse { |
|
| 159 | - | match event { |
|
| 160 | - | termion::event::MouseEvent::Press(button, x, y) => Mouse::Press { |
|
| 161 | - | button: mouse_button(button), |
|
| 162 | - | at: point(x, y), |
|
| 163 | - | }, |
|
| 164 | - | termion::event::MouseEvent::Release(x, y) => Mouse::Release { at: point(x, y) }, |
|
| 165 | - | termion::event::MouseEvent::Hold(x, y) => Mouse::Hold { at: point(x, y) }, |
|
| 166 | - | } |
|
| 167 | - | } |
|
| 168 | - | ||
| 169 | - | /// Converts a termion mouse button into a typed `rustik_tui` mouse button. |
|
| 170 | - | pub fn mouse_button(button: termion::event::MouseButton) -> MouseButton { |
|
| 171 | - | match button { |
|
| 172 | - | termion::event::MouseButton::Left => MouseButton::Left, |
|
| 173 | - | termion::event::MouseButton::Right => MouseButton::Right, |
|
| 174 | - | termion::event::MouseButton::Middle => MouseButton::Middle, |
|
| 175 | - | termion::event::MouseButton::WheelUp => MouseButton::WheelUp, |
|
| 176 | - | termion::event::MouseButton::WheelDown => MouseButton::WheelDown, |
|
| 177 | - | _ => MouseButton::Unknown, |
|
| 178 | - | } |
|
| 179 | - | } |
|
| 180 | - | ||
| 181 | - | /// Converts a zero-based coordinate into a terminal coordinate. |
|
| 182 | - | fn coord(value: usize) -> u16 { |
|
| 183 | - | u16::try_from(value.saturating_add(1)).unwrap_or(u16::MAX) |
|
| 184 | - | } |
|
| 185 | - | ||
| 186 | - | /// Converts one-based terminal coordinates to zero-based points. |
|
| 187 | - | fn point(x: u16, y: u16) -> Point { |
|
| 188 | - | Point::new(x.saturating_sub(1) as usize, y.saturating_sub(1) as usize) |
|
| 189 | - | } |
|
| 190 | - | ||
| 191 | - | #[cfg(test)] |
|
| 192 | - | #[allow(clippy::unwrap_used)] |
|
| 193 | - | mod tests { |
|
| 194 | - | use super::*; |
|
| 195 | - | use rustik_tui_core as tui_core; |
|
| 196 | - | use tui_core::backend::Backend; |
|
| 197 | - | use tui_core::{Buffer, Color, Rect, Style}; |
|
| 198 | - | ||
| 199 | - | #[test] |
|
| 200 | - | fn termion_backend_writes_dirty_runs_with_one_based_coordinates() { |
|
| 201 | - | let cells = [tui_core::buffer::Cell::new('x', Style::default())]; |
|
| 202 | - | let run = DirtyRun { |
|
| 203 | - | x: 2, |
|
| 204 | - | y: 3, |
|
| 205 | - | cells: &cells, |
|
| 206 | - | }; |
|
| 207 | - | let mut backend = TermionBackend::new(Vec::new()); |
|
| 208 | - | ||
| 209 | - | backend.draw(&[run]).unwrap(); |
|
| 210 | - | backend.flush().unwrap(); |
|
| 211 | - | let out = String::from_utf8(backend.into_inner()).unwrap(); |
|
| 212 | - | ||
| 213 | - | assert!(out.starts_with("\x1B[4;3H")); |
|
| 214 | - | assert!(out.ends_with('x')); |
|
| 215 | - | } |
|
| 216 | - | ||
| 217 | - | #[test] |
|
| 218 | - | fn termion_backend_paint_mode_is_explicit() { |
|
| 219 | - | let cells = [tui_core::buffer::Cell::new('x', Color::Red.style())]; |
|
| 220 | - | let run = DirtyRun { |
|
| 221 | - | x: 0, |
|
| 222 | - | y: 0, |
|
| 223 | - | cells: &cells, |
|
| 224 | - | }; |
|
| 225 | - | ||
| 226 | - | let mut styled = TermionBackend::new(Vec::new()); |
|
| 227 | - | styled.draw(&[run]).unwrap(); |
|
| 228 | - | assert_eq!(styled.paint_mode(), PaintMode::Styled); |
|
| 229 | - | assert!( |
|
| 230 | - | String::from_utf8(styled.into_inner()) |
|
| 231 | - | .unwrap() |
|
| 232 | - | .contains("\x1B[31m") |
|
| 233 | - | ); |
|
| 234 | - | ||
| 235 | - | let mut plain = TermionBackend::new(Vec::new()); |
|
| 236 | - | plain.set_paint_mode(PaintMode::Plain); |
|
| 237 | - | assert_eq!( |
|
| 238 | - | TermionBackend::plain(Vec::<u8>::new()).paint_mode(), |
|
| 239 | - | PaintMode::Plain |
|
| 240 | - | ); |
|
| 241 | - | plain.draw(&[run]).unwrap(); |
|
| 242 | - | let out = String::from_utf8(plain.into_inner()).unwrap(); |
|
| 243 | - | ||
| 244 | - | assert_eq!(out, "\x1B[1;1Hx"); |
|
| 245 | - | } |
|
| 246 | - | ||
| 247 | - | #[test] |
|
| 248 | - | fn termion_backend_does_not_write_wide_continuation_cells() { |
|
| 249 | - | let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 1)); |
|
| 250 | - | buffer.reset_dirty(); |
|
| 251 | - | buffer.set_str(0, 0, "๐a", Style::default()); |
|
| 252 | - | let runs = buffer.dirty_runs(); |
|
| 253 | - | let mut backend = TermionBackend::new(Vec::new()); |
|
| 254 | - | ||
| 255 | - | backend.draw(&runs).unwrap(); |
|
| 256 | - | let out = String::from_utf8(backend.into_inner()).unwrap(); |
|
| 257 | - | ||
| 258 | - | assert!(out.ends_with("๐a")); |
|
| 259 | - | assert!(!out.ends_with("๐ a")); |
|
| 260 | - | } |
|
| 261 | - | ||
| 262 | - | #[test] |
|
| 263 | - | fn termion_backend_cursor_and_visibility_commands_are_written() { |
|
| 264 | - | let mut backend = TermionBackend::new(Vec::new()); |
|
| 265 | - | ||
| 266 | - | backend.set_cursor_visible(false).unwrap(); |
|
| 267 | - | backend.set_cursor_position(Point::new(1, 2)).unwrap(); |
|
| 268 | - | backend.set_cursor_visible(true).unwrap(); |
|
| 269 | - | let out = String::from_utf8(backend.into_inner()).unwrap(); |
|
| 270 | - | ||
| 271 | - | assert!(out.contains("\x1B[?25l")); |
|
| 272 | - | assert!(out.contains("\x1B[3;2H")); |
|
| 273 | - | assert!(out.contains("\x1B[?25h")); |
|
| 274 | - | } |
|
| 275 | - | ||
| 276 | - | #[test] |
|
| 277 | - | fn termion_keys_are_typed() { |
|
| 278 | - | assert_eq!(key_event(termion::event::Key::Char('\n')), Key::Enter); |
|
| 279 | - | assert_eq!(key_event(termion::event::Key::Ctrl('c')), Key::Ctrl('c')); |
|
| 280 | - | } |
|
| 281 | - | ||
| 282 | - | #[test] |
|
| 283 | - | fn all_common_termion_keys_are_mapped() { |
|
| 284 | - | assert_eq!(key_event(termion::event::Key::Backspace), Key::Backspace); |
|
| 285 | - | assert_eq!(key_event(termion::event::Key::Left), Key::Left); |
|
| 286 | - | assert_eq!(key_event(termion::event::Key::Right), Key::Right); |
|
| 287 | - | assert_eq!(key_event(termion::event::Key::Up), Key::Up); |
|
| 288 | - | assert_eq!(key_event(termion::event::Key::Down), Key::Down); |
|
| 289 | - | assert_eq!(key_event(termion::event::Key::Home), Key::Home); |
|
| 290 | - | assert_eq!(key_event(termion::event::Key::End), Key::End); |
|
| 291 | - | assert_eq!(key_event(termion::event::Key::PageUp), Key::PageUp); |
|
| 292 | - | assert_eq!(key_event(termion::event::Key::PageDown), Key::PageDown); |
|
| 293 | - | assert_eq!(key_event(termion::event::Key::BackTab), Key::BackTab); |
|
| 294 | - | assert_eq!(key_event(termion::event::Key::Delete), Key::Delete); |
|
| 295 | - | assert_eq!(key_event(termion::event::Key::Insert), Key::Insert); |
|
| 296 | - | assert_eq!(key_event(termion::event::Key::F(5)), Key::F(5)); |
|
| 297 | - | assert_eq!(key_event(termion::event::Key::Char('\t')), Key::Tab); |
|
| 298 | - | assert_eq!(key_event(termion::event::Key::Alt('x')), Key::Alt('x')); |
|
| 299 | - | assert_eq!(key_event(termion::event::Key::Null), Key::Null); |
|
| 300 | - | assert_eq!(key_event(termion::event::Key::Esc), Key::Esc); |
|
| 301 | - | } |
|
| 302 | - | ||
| 303 | - | #[test] |
|
| 304 | - | fn termion_events_are_mapped() { |
|
| 305 | - | let key = termion::event::Event::Key(termion::event::Key::Char('x')); |
|
| 306 | - | let mouse = termion::event::Event::Mouse(termion::event::MouseEvent::Release(2, 3)); |
|
| 307 | - | let unsupported = termion::event::Event::Unsupported(vec![1, 2, 3]); |
|
| 308 | - | ||
| 309 | - | assert_eq!(event(key), Event::Key(Key::Char('x'))); |
|
| 310 | - | assert_eq!( |
|
| 311 | - | event(mouse), |
|
| 312 | - | Event::Mouse(Mouse::Release { |
|
| 313 | - | at: Point::new(1, 2), |
|
| 314 | - | }) |
|
| 315 | - | ); |
|
| 316 | - | assert_eq!(event(unsupported), Event::Unsupported); |
|
| 317 | - | } |
|
| 318 | - | ||
| 319 | - | #[test] |
|
| 320 | - | fn termion_mouse_events_are_zero_based() { |
|
| 321 | - | assert_eq!( |
|
| 322 | - | mouse_event(termion::event::MouseEvent::Press( |
|
| 323 | - | termion::event::MouseButton::Left, |
|
| 324 | - | 1, |
|
| 325 | - | 2, |
|
| 326 | - | )), |
|
| 327 | - | Mouse::Press { |
|
| 328 | - | button: MouseButton::Left, |
|
| 329 | - | at: Point::new(0, 1), |
|
| 330 | - | } |
|
| 331 | - | ); |
|
| 332 | - | assert_eq!( |
|
| 333 | - | mouse_event(termion::event::MouseEvent::Hold(3, 4)), |
|
| 334 | - | Mouse::Hold { |
|
| 335 | - | at: Point::new(2, 3), |
|
| 336 | - | } |
|
| 337 | - | ); |
|
| 338 | - | } |
|
| 339 | - | ||
| 340 | - | #[test] |
|
| 341 | - | fn mouse_buttons_are_mapped() { |
|
| 342 | - | assert_eq!( |
|
| 343 | - | mouse_button(termion::event::MouseButton::Left), |
|
| 344 | - | MouseButton::Left |
|
| 345 | - | ); |
|
| 346 | - | assert_eq!( |
|
| 347 | - | mouse_button(termion::event::MouseButton::Right), |
|
| 348 | - | MouseButton::Right |
|
| 349 | - | ); |
|
| 350 | - | assert_eq!( |
|
| 351 | - | mouse_button(termion::event::MouseButton::Middle), |
|
| 352 | - | MouseButton::Middle |
|
| 353 | - | ); |
|
| 354 | - | assert_eq!( |
|
| 355 | - | mouse_button(termion::event::MouseButton::WheelUp), |
|
| 356 | - | MouseButton::WheelUp |
|
| 357 | - | ); |
|
| 358 | - | assert_eq!( |
|
| 359 | - | mouse_button(termion::event::MouseButton::WheelDown), |
|
| 360 | - | MouseButton::WheelDown |
|
| 361 | - | ); |
|
| 362 | - | } |
|
| 363 | - | } |
|
| 17 | + | pub use backend::TermionBackend; |
|
| 18 | + | pub use event::TermionEvent; |
|
| 19 | + | pub use term::run; |
crates/tui-termion/src/term.rs
+12 -18
| 1 | 1 | //! Termion terminal session helpers. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! This module owns the raw-screen setup and event iterator plumbing needed to |
|
| 4 | - | //! run a `rustik_tui` app on a real terminal. Keeping it outside the core crate avoids |
|
| 4 | + | //! run a `rustik-tui` app on a real terminal. Keeping it outside the core crate avoids |
|
| 5 | 5 | //! tying widget rendering to termion. |
|
| 6 | 6 | ||
| 7 | 7 | use std::fs::File; |
|
| 8 | 8 | use std::io; |
|
| 9 | - | use std::io::IsTerminal; |
|
| 10 | 9 | use std::io::Write as _; |
|
| 11 | 10 | use std::sync::mpsc; |
|
| 12 | 11 | use std::thread; |
|
| 13 | 12 | ||
| 14 | - | use termion::input::{MouseTerminal, TermRead}; |
|
| 13 | + | use termion::input::{MouseTerminal, TermReadEventsAndRaw}; |
|
| 15 | 14 | use termion::raw::{IntoRawMode, RawTerminal}; |
|
| 16 | 15 | use termion::screen::{AlternateScreen, IntoAlternateScreen}; |
|
| 17 | 16 | ||
| 18 | 17 | use rustik_signals as signals; |
|
| 19 | 18 | use rustik_tui_core as tui_core; |
| 21 | 20 | use tui_core::app::{App, Control}; |
|
| 22 | 21 | use tui_core::event::Event; |
|
| 23 | 22 | use tui_core::terminal::Terminal; |
|
| 24 | 23 | use tui_core::{Constraint, Size, Widget}; |
|
| 25 | 24 | ||
| 26 | - | use crate::TermionBackend; |
|
| 25 | + | use crate::{TermionBackend, TermionEvent}; |
|
| 27 | 26 | ||
| 28 | 27 | /// Signals observed by terminal applications. |
|
| 29 | 28 | const SIGNALS: &[signals::Signal] = &[signals::Signal::WindowChanged]; |
|
| 30 | 29 | /// Controlling terminal path used when standard input is application data. |
|
| 31 | 30 | const TTY_DEVICE: &str = "/dev/tty"; |
| 52 | 51 | fn terminal_events() -> io::Result<impl Iterator<Item = io::Result<Event<()>>>> { |
|
| 53 | 52 | let terminal = File::open(TTY_DEVICE) |
|
| 54 | 53 | .or_else(|_| File::open(STDOUT_DEVICE)) |
|
| 55 | 54 | .or_else(|_| File::open(STDERR_DEVICE))?; |
|
| 56 | 55 | ||
| 57 | - | Ok(terminal.events().map(|event| event.map(crate::event))) |
|
| 56 | + | Ok(terminal |
|
| 57 | + | .events_and_raw() |
|
| 58 | + | .map(|event| event.map(TermionEvent::from).map(Event::from))) |
|
| 58 | 59 | } |
|
| 59 | 60 | ||
| 60 | 61 | /// Returns the current terminal viewport size. |
|
| 61 | - | pub fn viewport() -> Option<Size> { |
|
| 62 | + | fn viewport() -> Option<Size> { |
|
| 62 | 63 | termion::terminal_size() |
|
| 63 | 64 | .map(|(cols, rows)| Size::new(cols as usize, rows as usize)) |
|
| 64 | 65 | .ok() |
|
| 65 | 66 | } |
|
| 66 | 67 | ||
| 67 | - | /// Prints a widget to stdout. |
|
| 68 | - | pub fn print<W: Widget + ?Sized>(widget: &mut W) { |
|
| 69 | - | let constraint = if io::stdout().is_terminal() { |
|
| 70 | - | Constraint::max(viewport().unwrap_or(Size::MAX)) |
|
| 71 | - | } else { |
|
| 72 | - | Constraint::default() |
|
| 73 | - | }; |
|
| 74 | - | print_constrained(widget, constraint).ok(); |
|
| 75 | - | } |
|
| 76 | - | ||
| 77 | 68 | /// Writes a widget to stdout under the given constraint. |
|
| 78 | 69 | pub fn print_constrained<W: Widget + ?Sized>( |
|
| 79 | 70 | widget: &mut W, |
|
| 80 | 71 | constraints: Constraint, |
|
| 81 | 72 | ) -> io::Result<()> { |
| 95 | 86 | let mut terminal = Terminal::new(backend)?; |
|
| 96 | 87 | ||
| 97 | 88 | terminal.clear()?; |
|
| 98 | 89 | terminal.set_cursor_visible(false)?; |
|
| 99 | 90 | ||
| 100 | - | let result = _run(app, &mut terminal); |
|
| 91 | + | let result = event_loop(app, &mut terminal); |
|
| 101 | 92 | let cursor = terminal.set_cursor_visible(true); |
|
| 102 | 93 | ||
| 103 | 94 | result.and(cursor) |
|
| 104 | 95 | } |
|
| 105 | 96 | ||
| 106 | 97 | /// Runs the application event loop. |
|
| 107 | - | fn _run<A>(app: &mut A, terminal: &mut Terminal<TermionBackend<StdoutScreen>>) -> io::Result<()> |
|
| 98 | + | fn event_loop<A>( |
|
| 99 | + | app: &mut A, |
|
| 100 | + | terminal: &mut Terminal<TermionBackend<StdoutScreen>>, |
|
| 101 | + | ) -> io::Result<()> |
|
| 108 | 102 | where |
|
| 109 | 103 | A: App<Event = ()>, |
|
| 110 | 104 | { |
|
| 111 | 105 | terminal.draw(|frame| app.render(frame))?; |
|
| 112 | 106 |
crates/tui-widgets/src/viewport.rs
+20 -1
| 6 | 6 | //! scrolling policy into rendering code. |
|
| 7 | 7 | ||
| 8 | 8 | use std::ops::Range; |
|
| 9 | 9 | ||
| 10 | 10 | use rustik_tui_core as tui_core; |
|
| 11 | - | use tui_core::{Buffer, Line, Rect}; |
|
| 11 | + | use tui_core::{Buffer, Line, Point, Rect}; |
|
| 12 | 12 | ||
| 13 | 13 | /// Scroll movement request. |
|
| 14 | 14 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
|
| 15 | 15 | pub enum Scroll { |
|
| 16 | 16 | /// Move by a signed number of rows. |
| 240 | 240 | let row = self.offset.saturating_add(local_row); |
|
| 241 | 241 | ||
| 242 | 242 | (row < self.content_rows).then_some(row) |
|
| 243 | 243 | } |
|
| 244 | 244 | ||
| 245 | + | /// Returns the content row at a screen point inside `area`. |
|
| 246 | + | pub fn row_at(self, area: Rect, point: Point) -> Option<usize> { |
|
| 247 | + | if !area.contains(point) { |
|
| 248 | + | return None; |
|
| 249 | + | } |
|
| 250 | + | self.content_row(point.y - area.y) |
|
| 251 | + | } |
|
| 252 | + | ||
| 245 | 253 | /// Renders visible lines into an area. |
|
| 246 | 254 | pub fn render_lines<'a>( |
|
| 247 | 255 | self, |
|
| 248 | 256 | lines: impl IntoIterator<Item = &'a Line>, |
|
| 249 | 257 | area: Rect, |
| 367 | 375 | assert_eq!(view.content_row(0), Some(3)); |
|
| 368 | 376 | assert_eq!(view.content_row(2), Some(5)); |
|
| 369 | 377 | assert_eq!(view.content_row(3), None); |
|
| 370 | 378 | } |
|
| 371 | 379 | ||
| 380 | + | #[test] |
|
| 381 | + | fn view_maps_screen_points_to_content_rows() { |
|
| 382 | + | let view = View::new(4, 10, 3); |
|
| 383 | + | let area = Rect::new(2, 5, 8, 3); |
|
| 384 | + | ||
| 385 | + | assert_eq!(view.row_at(area, Point::new(2, 5)), Some(4)); |
|
| 386 | + | assert_eq!(view.row_at(area, Point::new(9, 7)), Some(6)); |
|
| 387 | + | assert_eq!(view.row_at(area, Point::new(10, 7)), None); |
|
| 388 | + | assert_eq!(view.row_at(area, Point::new(2, 8)), None); |
|
| 389 | + | } |
|
| 390 | + | ||
| 372 | 391 | #[test] |
|
| 373 | 392 | fn viewport_scrolls_and_clamps_to_content() { |
|
| 374 | 393 | let mut viewport = Viewport::new(); |
|
| 375 | 394 | ||
| 376 | 395 | assert!(viewport.scroll_down(2, 10, 4)); |