group tui_engine and tui_content; cargo update

This commit is contained in:
🪞👃🪞 2025-03-16 23:33:27 +02:00
parent 877b344765
commit 71dead5150
13 changed files with 150 additions and 72 deletions

View file

@ -1,5 +1,11 @@
use crate::*;
use std::time::Duration;
mod tui_buffer; pub use self::tui_buffer::*;
mod tui_input; pub use self::tui_input::*;
mod tui_output; pub use self::tui_output::*;
mod tui_perf; pub use self::tui_perf::*;
pub struct Tui {
pub exited: Arc<AtomicBool>,
pub backend: CrosstermBackend<Stdout>,
@ -7,6 +13,31 @@ pub struct Tui {
pub area: [u16;4],
pub perf: PerfModel,
}
pub trait TuiRun<R: Render<TuiOut> + Handle<TuiIn> + 'static> {
/// Run an app in the main loop.
fn run (&self, state: &Arc<RwLock<R>>) -> Usually<()>;
}
impl<T: Render<TuiOut> + Handle<TuiIn> + 'static> TuiRun<T> for Arc<RwLock<Tui>> {
fn run (&self, state: &Arc<RwLock<T>>) -> Usually<()> {
let _input_thread = TuiIn::run_input(self, state, Duration::from_millis(100));
self.write().unwrap().setup()?;
let render_thread = TuiOut::run_output(self, state, Duration::from_millis(10));
match render_thread.join() {
Ok(result) => {
self.write().unwrap().teardown()?;
println!("\n\rRan successfully: {result:?}\n\r");
},
Err(error) => {
self.write().unwrap().teardown()?;
panic!("\n\rRender thread failed: {error:?}.\n\r")
},
}
Ok(())
}
}
impl Tui {
/// Construct a new TUI engine and wrap it for shared ownership.
pub fn new () -> Usually<Arc<RwLock<Self>>> {
@ -58,25 +89,3 @@ impl Tui {
disable_raw_mode().map_err(Into::into)
}
}
pub trait TuiRun<R: Render<TuiOut> + Handle<TuiIn> + 'static> {
/// Run an app in the main loop.
fn run (&self, state: &Arc<RwLock<R>>) -> Usually<()>;
}
impl<T: Render<TuiOut> + Handle<TuiIn> + 'static> TuiRun<T> for Arc<RwLock<Tui>> {
fn run (&self, state: &Arc<RwLock<T>>) -> Usually<()> {
let _input_thread = TuiIn::run_input(self, state, Duration::from_millis(100));
self.write().unwrap().setup()?;
let render_thread = TuiOut::run_output(self, state, Duration::from_millis(10));
match render_thread.join() {
Ok(result) => {
self.write().unwrap().teardown()?;
println!("\n\rRan successfully: {result:?}\n\r");
},
Err(error) => {
self.write().unwrap().teardown()?;
panic!("\n\rRender thread failed: {error:?}.\n\r")
},
}
Ok(())
}
}