From 0a868c9e6c90c6186b740de942a056a0338c295b Mon Sep 17 00:00:00 2001 From: facile pop culture reference Date: Thu, 1 Oct 2026 15:28:39 +0300 Subject: [PATCH] tui: semi-fix resize --- src/term.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/term.rs b/src/term.rs index b686da8..bd0b3a9 100644 --- a/src/term.rs +++ b/src/term.rs @@ -135,6 +135,13 @@ impl Screen for Tui { } impl Tui { + pub fn new (width: u16, height: u16) -> Self { + Self( + XYWH(0, 0, width, height), + Some(Buffer::empty(Rect { x: 0, y: 0, width, height })) + ) + } + pub fn setup_panic () { use ::std::panic::{set_hook, PanicHookInfo}; use ::better_panic::{Settings, Verbosity}; @@ -146,6 +153,7 @@ impl Tui { panic(info); })); } + pub fn run_main (exit: Exit, state: Arc>) -> Usually<()> where T: Draw + Apply> + Send + Sync + 'static { @@ -155,6 +163,7 @@ impl Tui { let _ = output.join(); Tui::teardown(&mut stdout()) } + /// Spawn the TUI input and output threadsl. pub fn io < T: Draw + Apply> + Send + Sync + 'static, @@ -171,6 +180,7 @@ impl Tui { Tui::output(exited, state, sleep, output)?, )) } + /// Spawn the TUI input thread which reads keys from the terminal. pub fn input > + Send + Sync + 'static> ( exited: &Arc, state: &Arc>, poll: Duration @@ -186,19 +196,17 @@ impl Tui { } }) } + pub fn teardown (backend: &mut W) -> Usually<()> { use ::ratatui::backend::Backend; stdout().execute(LeaveAlternateScreen)?; CrosstermBackend::new(backend).show_cursor()?; disable_raw_mode().map_err(Into::into) } - pub fn new (width: u16, height: u16) -> Self { - Self( - XYWH(0, 0, width, height), - Some(Buffer::empty(Rect { x: 0, y: 0, width, height })) - ) - } - pub fn resize (&mut self, back: &mut CrosstermBackend, width: u16, height: u16) { + + pub fn resize ( + &mut self, back: &mut CrosstermBackend, width: u16, height: u16 + ) { let size = Rect { x: 0, y: 0, width, height }; self.buffer().map(|buffer|{ if buffer.area != size { @@ -208,13 +216,11 @@ impl Tui { } }); } + pub fn redraw <'b, W: Write> ( - &'b mut self, - back: &mut CrosstermBackend, - next: &'b mut Self + &'b mut self, back: &mut CrosstermBackend, next: &'b mut Self ) { - if let Some(prev) = self.buffer() - && let Some(next) = next.buffer() { + if let Some(prev) = self.buffer() && let Some(next) = next.buffer() { let updates = prev.diff(&next); back.draw(updates.into_iter()).expect("failed to render"); Backend::flush(back).expect("failed to flush output new"); @@ -222,6 +228,7 @@ impl Tui { next.reset(); } } + pub fn update (&mut self, callback: &impl Fn(&mut Cell, u16, u16)) -> XYWH { let XYWH(x0, y0, w, h) = self.area(); if let Some(buffer) = self.buffer() { @@ -239,6 +246,7 @@ impl Tui { } self.xywh() } + pub fn blit (&mut self, text: &impl AsRef, x: u16, y: u16, style: Option