tui-termion: Buffer terminal output
fdf87fb1b232a7d01d622d741c37d5ba5e6b8056
Collect terminal output in a bounded buffer and flush each frame to reduce device writes during scrolling. Flush pending output and keyboard protocol cleanup before the terminal guards are dropped. Assisted-by: Codex:gpt-6
1 parent
0d710a9a
crates/tui-termion/src/term.rs
+52 -5
| 4 | 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, OpenOptions}; |
|
| 8 | 8 | use std::io; |
|
| 9 | - | use std::io::Write; |
|
| 9 | + | use std::io::{BufWriter, Write}; |
|
| 10 | 10 | use std::sync::mpsc; |
|
| 11 | 11 | use std::thread; |
|
| 12 | 12 | ||
| 13 | 13 | use termion::input::{MouseTerminal, TermReadEventsAndRaw}; |
|
| 14 | 14 | use termion::raw::{IntoRawMode, RawTerminal}; |
| 49 | 49 | let screen = mouse.into_alternate_screen()?; |
|
| 50 | 50 | let keyboard = KeyboardProtocol::new(screen)?; |
|
| 51 | 51 | Ok(TermionBackend::new(keyboard)) |
|
| 52 | 52 | } |
|
| 53 | 53 | ||
| 54 | - | /// Output wrapper that enables the enhanced keyboard protocol for its lifetime. |
|
| 54 | + | /// Buffered output wrapper that enables the enhanced keyboard protocol for its lifetime. |
|
| 55 | 55 | /// |
|
| 56 | 56 | /// The protocol lets the terminal report modified keys unambiguously; terminals that do not |
|
| 57 | 57 | /// implement it ignore the enabling sequence, so this degrades to the legacy keyboard encoding. The |
|
| 58 | 58 | /// restoring sequence is written when the wrapper is dropped, before the underlying |
|
| 59 | 59 | /// raw/alternate-screen guards tear down. |
|
| 60 | 60 | #[derive(Debug)] |
|
| 61 | 61 | pub struct KeyboardProtocol<W: Write> { |
|
| 62 | - | /// Wrapped terminal writer. |
|
| 63 | - | inner: W, |
|
| 62 | + | /// Terminal writer that collects output until the frame is flushed. |
|
| 63 | + | inner: BufWriter<W>, |
|
| 64 | 64 | } |
|
| 65 | 65 | ||
| 66 | 66 | impl<W: Write> KeyboardProtocol<W> { |
|
| 67 | 67 | /// Enables the protocol on `inner` and returns the guarding wrapper. |
|
| 68 | - | fn new(mut inner: W) -> io::Result<Self> { |
|
| 68 | + | fn new(inner: W) -> io::Result<Self> { |
|
| 69 | + | let mut inner = BufWriter::new(inner); |
|
| 69 | 70 | inner.write_all(KEYBOARD_PUSH.as_bytes())?; |
|
| 70 | 71 | inner.flush()?; |
|
| 71 | 72 | Ok(Self { inner }) |
|
| 72 | 73 | } |
|
| 73 | 74 | } |
| 264 | 265 | mod tests { |
|
| 265 | 266 | use super::*; |
|
| 266 | 267 | use rustik_tui_core as tui_core; |
|
| 267 | 268 | use tui_core::Label; |
|
| 268 | 269 | ||
| 270 | + | /// Records writes to the terminal device. |
|
| 271 | + | #[derive(Default)] |
|
| 272 | + | struct RecordingWriter { |
|
| 273 | + | /// Bytes passed to each device write. |
|
| 274 | + | writes: Vec<Vec<u8>>, |
|
| 275 | + | } |
|
| 276 | + | ||
| 277 | + | impl Write for RecordingWriter { |
|
| 278 | + | fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { |
|
| 279 | + | self.writes.push(bytes.to_vec()); |
|
| 280 | + | Ok(bytes.len()) |
|
| 281 | + | } |
|
| 282 | + | ||
| 283 | + | fn flush(&mut self) -> io::Result<()> { |
|
| 284 | + | Ok(()) |
|
| 285 | + | } |
|
| 286 | + | } |
|
| 287 | + | ||
| 288 | + | #[test] |
|
| 289 | + | fn terminal_output_flushes_frames_and_protocol_sequences() { |
|
| 290 | + | let mut writer = RecordingWriter::default(); |
|
| 291 | + | { |
|
| 292 | + | let mut terminal = KeyboardProtocol::new(&mut writer).unwrap(); |
|
| 293 | + | assert_eq!(terminal.inner.get_ref().writes, [KEYBOARD_PUSH.as_bytes()]); |
|
| 294 | + | ||
| 295 | + | for byte in b"\x1b[31mhello\x1b[0m" { |
|
| 296 | + | terminal.write_all(&[*byte]).unwrap(); |
|
| 297 | + | } |
|
| 298 | + | terminal.flush().unwrap(); |
|
| 299 | + | assert_eq!( |
|
| 300 | + | terminal.inner.get_ref().writes, |
|
| 301 | + | [KEYBOARD_PUSH.as_bytes(), b"\x1b[31mhello\x1b[0m"] |
|
| 302 | + | ); |
|
| 303 | + | ||
| 304 | + | terminal.write_all(b"\x1b[?25h").unwrap(); |
|
| 305 | + | } |
|
| 306 | + | assert_eq!( |
|
| 307 | + | writer.writes, |
|
| 308 | + | [ |
|
| 309 | + | KEYBOARD_PUSH.as_bytes(), |
|
| 310 | + | b"\x1b[31mhello\x1b[0m", |
|
| 311 | + | b"\x1b[?25h\x1b[<u", |
|
| 312 | + | ] |
|
| 313 | + | ); |
|
| 314 | + | } |
|
| 315 | + | ||
| 269 | 316 | #[test] |
|
| 270 | 317 | fn update_redraws_after_resize_even_when_app_continues() { |
|
| 271 | 318 | struct ResizeAware; |
|
| 272 | 319 | ||
| 273 | 320 | impl App for ResizeAware { |