add Tui::new, remove View

This commit is contained in:
same mf who else 2026-02-21 18:13:21 +02:00
parent 85ccb0737f
commit b294f2e62b
4 changed files with 40 additions and 57 deletions

View file

@ -983,11 +983,38 @@ impl PerfModel {
}
impl Tui {
pub fn new (output: Stdout) -> Usually<Self> {
let backend = CrosstermBackend::new(output);
let Size { width, height } = backend.size()?;
Ok(Self {
exited: Arc::new(AtomicBool::new(false)),
buffer: Buffer::empty(Rect { x: 0, y: 0, width, height }),
area: [0, 0, width, height],
perf: Default::default(),
backend,
})
}
/// Create and launch a terminal user interface.
pub fn run <T> (join: bool, state: T) -> Usually<Arc<RwLock<Self>>> where
pub fn run <T> (self, join: bool, state: &Arc<RwLock<T>>) -> Usually<Arc<RwLock<Self>>> where
T: Handle<TuiIn> + Draw<TuiOut> + Send + Sync + 'static
{
tui_run(join, &Arc::new(RwLock::new(state)))
let tui = Arc::new(RwLock::new(self));
let _input_thread = tui_input(tui.clone(), state, Duration::from_millis(100));
tui.write().unwrap().setup()?;
let render_thread = tui_output(tui.clone(), state, Duration::from_millis(10))?;
if join {
match render_thread.join() {
Ok(result) => {
tui.write().unwrap().teardown()?;
println!("\n\rRan successfully: {result:?}\n\r");
},
Err(error) => {
tui.write().unwrap().teardown()?;
panic!("\n\rDraw thread failed: error={error:?}.\n\r")
},
}
}
Ok(tui)
}
/// True if done
pub fn exited (&self) -> bool { self.exited.fetch_and(true, Relaxed) }
@ -1021,6 +1048,11 @@ impl TuiEvent {
Ok(TuiKey::from_dsl(dsl)?.to_crossterm().map(Self))
}
}
impl From<char> for TuiEvent {
fn from (c: char) -> Self {
Self(Event::Key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE)))
}
}
impl TuiKey {
const SPLIT: char = '/';
#[cfg(feature = "dsl")] pub fn from_dsl (dsl: impl Language) -> Usually<Self> {