tui: Implement TUI resizing
b005b61f5f24dde1f18cb2c42fed2715600d9e4c
1 parent
fff10911
crates/diff/src/bin/rustik-diff.rs
+1 -1
| 144 | 144 | if !io::stdout().is_terminal() { |
|
| 145 | 145 | let mut viewer = DiffViewer::new(document, options); |
|
| 146 | 146 | let rows = viewer.content_rows(STATIC_COLS).min(STATIC_MAX_ROWS); |
|
| 147 | 147 | let constraints = tui::Constraint::max(tui::Size::new(STATIC_COLS, rows)); |
|
| 148 | 148 | ||
| 149 | - | tui_termion::term::write_to_stdout(&mut viewer, constraints)?; |
|
| 149 | + | tui_termion::term::print_constrained(&mut viewer, constraints)?; |
|
| 150 | 150 | return Ok(()); |
|
| 151 | 151 | } |
|
| 152 | 152 | let backend = tui_termion::term::screen()?; |
|
| 153 | 153 | let mut terminal = Terminal::new(backend)?; |
|
| 154 | 154 | let mut registry = syntax::Registry::default(); |
+0 -0
+0 -0
a.rs
+0 -0
crates/tui-termion/Cargo.toml
+1 -0
| 7 | 7 | edition.workspace = true |
|
| 8 | 8 | license.workspace = true |
|
| 9 | 9 | ||
| 10 | 10 | [dependencies] |
|
| 11 | 11 | termion.workspace = true |
|
| 12 | + | rustik-signals.workspace = true |
|
| 12 | 13 | rustik-tui-core.workspace = true |
|
| 13 | 14 | ||
| 14 | 15 | [lints] |
|
| 15 | 16 | workspace = true |
crates/tui-termion/src/term.rs
+78 -23
| 12 | 12 | ||
| 13 | 13 | use termion::input::{MouseTerminal, TermRead}; |
|
| 14 | 14 | use termion::raw::{IntoRawMode, RawTerminal}; |
|
| 15 | 15 | use termion::screen::{AlternateScreen, IntoAlternateScreen}; |
|
| 16 | 16 | ||
| 17 | + | use rustik_signals as signals; |
|
| 17 | 18 | use rustik_tui_core as tui_core; |
|
| 19 | + | ||
| 18 | 20 | use tui_core::app::{App, Control}; |
|
| 19 | 21 | use tui_core::event::Event; |
|
| 20 | 22 | use tui_core::terminal::Terminal; |
|
| 21 | 23 | use tui_core::{Constraint, Size, Widget}; |
|
| 22 | 24 | ||
| 23 | 25 | use crate::TermionBackend; |
|
| 24 | 26 | ||
| 27 | + | /// Signals observed by terminal applications. |
|
| 28 | + | const SIGNALS: &[signals::Signal] = &[signals::Signal::WindowChanged]; |
|
| 29 | + | ||
| 25 | 30 | /// Raw alternate stdout screen. |
|
| 26 | 31 | pub type StdoutScreen = AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>; |
|
| 27 | 32 | ||
| 28 | 33 | /// Creates a raw alternate-screen backend on stdout. |
|
| 29 | 34 | pub fn screen() -> io::Result<TermionBackend<StdoutScreen>> { |
|
| 30 | 35 | let screen = MouseTerminal::from(io::stdout().into_raw_mode()?).into_alternate_screen()?; |
|
| 31 | - | ||
| 32 | 36 | Ok(TermionBackend::new(screen)) |
|
| 33 | 37 | } |
|
| 34 | 38 | ||
| 35 | 39 | /// Returns typed events from an input stream. |
|
| 36 | 40 | pub fn events_from<R: io::Read>(input: R) -> impl Iterator<Item = io::Result<Event<()>>> { |
|
| 37 | 41 | input.events().map(|event| event.map(crate::event)) |
|
| 38 | 42 | } |
|
| 39 | 43 | ||
| 40 | 44 | /// Returns typed events from stdin. |
|
| 41 | 45 | pub fn events() -> impl Iterator<Item = io::Result<Event<()>>> { |
|
| 42 | - | events_from(io::stdin()) |
|
| 46 | + | event_receiver().into_iter() |
|
| 43 | 47 | } |
|
| 44 | 48 | ||
| 45 | 49 | /// Returns the current terminal viewport size. |
|
| 46 | 50 | pub fn viewport() -> Option<Size> { |
|
| 47 | 51 | termion::terminal_size() |
|
| 48 | 52 | .map(|(cols, rows)| Size::new(cols as usize, rows as usize)) |
|
| 49 | 53 | .ok() |
|
| 50 | 54 | } |
|
| 51 | 55 | ||
| 52 | - | /// Returns the stdout-backed widget constraint. |
|
| 53 | - | pub fn stdout_constraint() -> Option<Constraint> { |
|
| 54 | - | if io::stdout().is_terminal() { |
|
| 55 | - | Some(Constraint::max(viewport().unwrap_or(Size::MAX))) |
|
| 56 | - | } else { |
|
| 57 | - | None |
|
| 58 | - | } |
|
| 59 | - | } |
|
| 60 | - | ||
| 61 | 56 | /// Prints a widget to stdout. |
|
| 62 | 57 | pub fn print<W: Widget + ?Sized>(widget: &mut W) { |
|
| 63 | - | let constraint = stdout_constraint().unwrap_or_default(); |
|
| 64 | - | ||
| 65 | - | write_to_stdout(widget, constraint).ok(); |
|
| 58 | + | let constraint = if io::stdout().is_terminal() { |
|
| 59 | + | Constraint::max(viewport().unwrap_or(Size::MAX)) |
|
| 60 | + | } else { |
|
| 61 | + | Constraint::default() |
|
| 62 | + | }; |
|
| 63 | + | print_constrained(widget, constraint).ok(); |
|
| 66 | 64 | } |
|
| 67 | 65 | ||
| 68 | 66 | /// Writes a widget to stdout under the given constraint. |
|
| 69 | - | pub fn write_to_stdout<W: Widget + ?Sized>( |
|
| 67 | + | pub fn print_constrained<W: Widget + ?Sized>( |
|
| 70 | 68 | widget: &mut W, |
|
| 71 | 69 | constraints: Constraint, |
|
| 72 | 70 | ) -> io::Result<()> { |
|
| 73 | 71 | let mut stdout = io::stdout(); |
|
| 74 | 72 | for line in widget.display(constraints).lines() { |
| 86 | 84 | let mut terminal = Terminal::new(backend)?; |
|
| 87 | 85 | ||
| 88 | 86 | terminal.clear()?; |
|
| 89 | 87 | terminal.set_cursor_visible(false)?; |
|
| 90 | 88 | ||
| 91 | - | let result = run_loop(app, &mut terminal); |
|
| 89 | + | let result = _run(app, &mut terminal); |
|
| 92 | 90 | let cursor = terminal.set_cursor_visible(true); |
|
| 93 | 91 | ||
| 94 | 92 | result.and(cursor) |
|
| 95 | 93 | } |
|
| 96 | 94 | ||
| 97 | 95 | /// Runs the application event loop. |
|
| 98 | - | fn run_loop<A>(app: &mut A, terminal: &mut Terminal<TermionBackend<StdoutScreen>>) -> io::Result<()> |
|
| 96 | + | fn _run<A>(app: &mut A, terminal: &mut Terminal<TermionBackend<StdoutScreen>>) -> io::Result<()> |
|
| 99 | 97 | where |
|
| 100 | 98 | A: App<Event = ()>, |
|
| 101 | 99 | { |
|
| 102 | 100 | terminal.draw(|frame| app.render(frame))?; |
|
| 103 | 101 |
| 121 | 119 | } |
|
| 122 | 120 | ||
| 123 | 121 | /// Spawns a blocking stdin reader and returns its event channel. |
|
| 124 | 122 | fn event_receiver() -> mpsc::Receiver<io::Result<Event<()>>> { |
|
| 125 | 123 | let (tx, rx) = mpsc::channel(); |
|
| 126 | - | thread::spawn(move || { |
|
| 127 | - | for event in events() { |
|
| 128 | - | if tx.send(event).is_err() { |
|
| 129 | - | break; |
|
| 124 | + | ||
| 125 | + | // Spawns a blocking stdin event reader. |
|
| 126 | + | thread::spawn({ |
|
| 127 | + | let tx = tx.clone(); |
|
| 128 | + | move || { |
|
| 129 | + | for event in events_from(io::stdin()) { |
|
| 130 | + | if tx.send(event).is_err() { |
|
| 131 | + | break; |
|
| 132 | + | } |
|
| 130 | 133 | } |
|
| 131 | 134 | } |
|
| 132 | 135 | }); |
|
| 136 | + | // Spawns a signal notification reader. |
|
| 137 | + | thread::spawn(move || match signals::Signals::install(SIGNALS) { |
|
| 138 | + | Ok(signals) => { |
|
| 139 | + | while let Ok(signal) = signals.recv() { |
|
| 140 | + | match signal { |
|
| 141 | + | signals::Signal::WindowChanged => { |
|
| 142 | + | if tx |
|
| 143 | + | .send(Ok(Event::Resize(viewport().unwrap_or(Size::MAX)))) |
|
| 144 | + | .is_err() |
|
| 145 | + | { |
|
| 146 | + | break; |
|
| 147 | + | } |
|
| 148 | + | } |
|
| 149 | + | signals::Signal::Interrupt |
|
| 150 | + | | signals::Signal::Terminate |
|
| 151 | + | | signals::Signal::Hangup => {} |
|
| 152 | + | } |
|
| 153 | + | } |
|
| 154 | + | } |
|
| 155 | + | Err(error) => { |
|
| 156 | + | let _ = tx.send(Err(error)); |
|
| 157 | + | } |
|
| 158 | + | }); |
|
| 159 | + | ||
| 133 | 160 | rx |
|
| 134 | 161 | } |
|
| 135 | 162 | ||
| 136 | 163 | /// Applies one event and records whether the caller should redraw. |
|
| 137 | 164 | fn update<A>(app: &mut A, event: Event<()>, redraw: &mut bool) -> Control |
|
| 138 | 165 | where |
|
| 139 | 166 | A: App<Event = ()>, |
|
| 140 | 167 | { |
|
| 168 | + | let resized = matches!(event, Event::Resize(_)); |
|
| 141 | 169 | match app.update(event) { |
|
| 142 | - | Control::Continue | Control::Wait => Control::Continue, |
|
| 170 | + | Control::Continue | Control::Wait => { |
|
| 171 | + | *redraw |= resized; |
|
| 172 | + | Control::Continue |
|
| 173 | + | } |
|
| 143 | 174 | Control::Redraw => { |
|
| 144 | 175 | *redraw = true; |
|
| 145 | 176 | Control::Redraw |
|
| 146 | 177 | } |
|
| 147 | 178 | Control::Quit => Control::Quit, |
| 153 | 184 | mod tests { |
|
| 154 | 185 | use std::io::Cursor; |
|
| 155 | 186 | ||
| 156 | 187 | use super::*; |
|
| 157 | 188 | use rustik_tui_core as tui_core; |
|
| 158 | - | use tui_core::Key; |
|
| 189 | + | use tui_core::{Key, Label}; |
|
| 159 | 190 | ||
| 160 | 191 | #[test] |
|
| 161 | 192 | fn events_from_maps_termion_input_stream() { |
|
| 162 | 193 | let events = events_from(Cursor::new([b'a', b'\n', 0x1b])) |
|
| 163 | 194 | .collect::<io::Result<Vec<_>>>() |
| 165 | 196 | ||
| 166 | 197 | assert_eq!(events[0], Event::Key(Key::Char('a'))); |
|
| 167 | 198 | assert_eq!(events[1], Event::Key(Key::Enter)); |
|
| 168 | 199 | assert_eq!(events[2], Event::Key(Key::Esc)); |
|
| 169 | 200 | } |
|
| 201 | + | ||
| 202 | + | #[test] |
|
| 203 | + | fn update_redraws_after_resize_even_when_app_continues() { |
|
| 204 | + | struct ResizeAware; |
|
| 205 | + | ||
| 206 | + | impl App for ResizeAware { |
|
| 207 | + | type Event = (); |
|
| 208 | + | ||
| 209 | + | fn update(&mut self, _: Event<Self::Event>) -> Control { |
|
| 210 | + | Control::Continue |
|
| 211 | + | } |
|
| 212 | + | ||
| 213 | + | fn render(&self, frame: &mut tui_core::Frame<'_>) { |
|
| 214 | + | frame.render_widget(Label::new("x"), frame.area); |
|
| 215 | + | } |
|
| 216 | + | } |
|
| 217 | + | let mut app = ResizeAware; |
|
| 218 | + | let mut redraw = false; |
|
| 219 | + | ||
| 220 | + | let control = update(&mut app, Event::Resize(Size::new(80, 24)), &mut redraw); |
|
| 221 | + | ||
| 222 | + | assert_eq!(control, Control::Continue); |
|
| 223 | + | assert!(redraw); |
|
| 224 | + | } |
|
| 170 | 225 | } |