mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-10-08 12:16:43 +02:00
tui: semi-fix resize
This commit is contained in:
parent
614b8ee5d9
commit
0a868c9e6c
1 changed files with 24 additions and 12 deletions
36
src/term.rs
36
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 <T> (exit: Exit, state: Arc<RwLock<T>>) -> Usually<()> where
|
||||
T: Draw<Tui> + Apply<TuiEvent, Usually<()>> + 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<Tui> + Apply<TuiEvent, Usually<()>> + 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 <T: Apply<TuiEvent, Usually<()>> + Send + Sync + 'static> (
|
||||
exited: &Arc<AtomicBool>, state: &Arc<RwLock<T>>, poll: Duration
|
||||
|
|
@ -186,19 +196,17 @@ impl Tui {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn teardown <W: Write> (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 <W: Write> (&mut self, back: &mut CrosstermBackend<W>, width: u16, height: u16) {
|
||||
|
||||
pub fn resize <W: Write> (
|
||||
&mut self, back: &mut CrosstermBackend<W>, 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<W>,
|
||||
next: &'b mut Self
|
||||
&'b mut self, back: &mut CrosstermBackend<W>, 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<u16> {
|
||||
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<str>, x: u16, y: u16, style: Option<Style>) {
|
||||
if let Some(buffer) = self.buffer() {
|
||||
let text = text.as_ref();
|
||||
|
|
@ -248,6 +256,7 @@ impl Tui {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tint_all (&mut self, fg: Color, bg: Color, modifier: Modifier) {
|
||||
if let Some(buffer) = self.buffer() {
|
||||
for cell in buffer.content.iter_mut() {
|
||||
|
|
@ -257,6 +266,7 @@ impl Tui {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn the TUI output thread which writes colored characters to the terminal.
|
||||
///
|
||||
/// ```
|
||||
|
|
@ -292,6 +302,7 @@ impl Tui {
|
|||
if let Some(state) = state.try_read() {
|
||||
prev.resize(&mut backend, width, height);
|
||||
next.resize(&mut backend, width, height);
|
||||
next.buffer().map(|buffer|buffer.reset());
|
||||
state.draw(&mut next).expect("draw failed"); // TODO draw error
|
||||
prev.redraw(&mut backend, &mut next);
|
||||
}
|
||||
|
|
@ -299,6 +310,7 @@ impl Tui {
|
|||
//prev.blit(&timer, 0, 0, Some(Style::default()));
|
||||
})?)
|
||||
}
|
||||
|
||||
/// Draw TUI content or its error message.
|
||||
///
|
||||
/// ```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue