breaking: refactor: cooking
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
same mf who else 2026-03-08 22:13:12 +02:00
parent ad070a4cbb
commit 8f0a2accce
13 changed files with 1819 additions and 2157 deletions

200
src/tui.rs Normal file
View file

@ -0,0 +1,200 @@
use crate::*;
#[cfg(feature = "tui")] pub use self::tui_fns::*;
#[cfg(feature = "tui")] mod tui_fns {
use crate::*;
}
/// Interpret TUI-specific layout operation.
///
/// ```
/// use tengri::{Namespace, Understand, Tui, ratatui::prelude::Color};
///
/// struct State;
/// impl<'b> Namespace<'b, bool> for State {}
/// impl<'b> Namespace<'b, u16> for State {}
/// impl<'b> Namespace<'b, Color> for State {}
/// impl Understand<Tui, ()> for State {}
/// # fn main () -> tengri::Usually<()> {
/// let state = State;
/// let mut out = TuiOut::default();
/// tengri::eval_view_tui(&state, &mut out, "")?;
/// tengri::eval_view_tui(&state, &mut out, "text Hello world!")?;
/// tengri::eval_view_tui(&state, &mut out, "fg (g 0) (text Hello world!)")?;
/// tengri::eval_view_tui(&state, &mut out, "bg (g 2) (text Hello world!)")?;
/// tengri::eval_view_tui(&state, &mut out, "(bg (g 3) (fg (g 4) (text Hello world!)))")?;
/// # Ok(()) }
/// ```
pub fn eval_view_tui <'a, S> (
state: &S, output: &mut Buffer, expr: impl Expression + 'a
) -> Usually<bool> where
S: Understand<TuiOut, ()>
+ for<'b>Namespace<'b, bool>
+ for<'b>Namespace<'b, u16>
+ for<'b>Namespace<'b, Color>
{
// See `tengri::eval_view`
let head = expr.head()?;
let mut frags = head.src()?.unwrap_or_default().split("/");
let args = expr.tail();
let arg0 = args.head();
let tail0 = args.tail();
let arg1 = tail0.head();
let tail1 = tail0.tail();
let arg2 = tail1.head();
match frags.next() {
Some("text") => {
if let Some(src) = args?.src()? { output.place(&src) }
},
Some("fg") => {
let arg0 = arg0?.expect("fg: expected arg 0 (color)");
let color = Namespace::namespace(state, arg0)?.unwrap_or_else(||panic!("fg: {arg0:?}: not a color"));
let thunk = Thunk::new(move|output: &mut Buffer|state.understand(output, &arg1).unwrap());
output.place(&TuiOut::fg(color, thunk))
},
Some("bg") => {
//panic!("expr: {expr:?}\nhead: {head:?}\nfrags: {frags:?}\nargs: {args:?}\narg0: {arg0:?}\ntail0: {tail0:?}\narg1: {arg1:?}\ntail1: {tail1:?}\narg2: {arg2:?}");
//panic!("head: {head:?}\narg0: {arg0:?}\narg1: {arg1:?}\narg2: {arg2:?}");;
//panic!("head: {head:?}\narg0: {arg0:?}\narg1: {arg1:?}\narg2: {arg2:?}");
let arg0 = arg0?.expect("bg: expected arg 0 (color)");
let color = Namespace::namespace(state, arg0)?.unwrap_or_else(||panic!("bg: {arg0:?}: not a color"));
let thunk = Thunk::new(move|output: &mut Buffer|state.understand(output, &arg1).unwrap());
output.place(&TuiOut::bg(color, thunk))
},
_ => return Ok(false)
};
Ok(true)
}
#[cfg(feature = "tui")] pub fn tui_setup <W: Write> (backend: &mut CrosstermBackend<W>) -> Usually<()> {
let better_panic_handler = Settings::auto().verbosity(Verbosity::Full).create_panic_handler();
std::panic::set_hook(Box::new(move |info: &std::panic::PanicHookInfo|{
stdout().execute(LeaveAlternateScreen).unwrap();
CrosstermBackend::new(stdout()).show_cursor().unwrap();
disable_raw_mode().unwrap();
better_panic_handler(info);
}));
stdout().execute(EnterAlternateScreen)?;
backend.hide_cursor()?;
enable_raw_mode().map_err(Into::into)
}
#[cfg(feature = "tui")] pub fn tui_teardown <W: Write> (backend: &mut CrosstermBackend<W>) -> Usually<()> {
stdout().execute(LeaveAlternateScreen)?;
backend.show_cursor()?;
disable_raw_mode().map_err(Into::into)
}
#[cfg(feature = "tui")] pub fn tui_update (
buf: &mut Buffer, area: XYWH<u16>, callback: &impl Fn(&mut Cell, u16, u16)
) {
for row in 0..area.h() {
let y = area.y() + row;
for col in 0..area.w() {
let x = area.x() + col;
if x < buf.area.width && y < buf.area.height {
if let Some(cell) = buf.cell_mut(ratatui::prelude::Position { x, y }) {
callback(cell, col, row);
}
}
}
}
}
#[cfg(feature = "tui")] pub(crate) fn tui_wh <W: Write> (
backend: &mut CrosstermBackend<W>
) -> WH<u16> {
let Size { width, height } = backend.size().expect("get size failed");
WH(width, height)
}
/// Spawn the TUI input thread which reads keys from the terminal.
#[cfg(feature = "tui")] pub fn tui_input <T: Act<TuiEvent, T> + Send + Sync + 'static> (
exited: &Arc<AtomicBool>, state: &Arc<RwLock<T>>, poll: Duration
) -> Result<Thread, std::io::Error> {
let exited = exited.clone();
let state = state.clone();
Thread::new_poll(exited.clone(), poll, move |_| {
let event = read().unwrap();
match event {
// Hardcoded exit.
Event::Key(KeyEvent {
modifiers: KeyModifiers::CONTROL,
code: KeyCode::Char('c'),
kind: KeyEventKind::Press,
state: KeyEventState::NONE
}) => { exited.store(true, Relaxed); },
// Handle all other events by the state:
_ => {
let event = TuiEvent::from_crossterm(event);
if let Err(e) = state.write().unwrap().handle(&event) {
panic!("{e}")
}
}
}
})
}
/// Spawn the TUI output thread which writes colored characters to the terminal.
#[cfg(feature = "tui")] pub fn tui_output <T: Draw<Buffer> + Send + Sync + 'static> (
exited: &Arc<AtomicBool>, state: &Arc<RwLock<T>>, sleep: Duration
) -> Result<Thread, std::io::Error> {
let state = state.clone();
let mut backend = CrosstermBackend::new(stdout());
let WH(width, height) = tui_wh(&mut backend);
let mut buffer_a = Buffer::empty(Rect { x: 0, y: 0, width, height });
let mut buffer_b = Buffer::empty(Rect { x: 0, y: 0, width, height });
Thread::new_sleep(exited.clone(), sleep, move |perf| {
let size = tui_wh(&mut backend);
if let Ok(state) = state.try_read() {
tui_resize(&mut backend, &mut buffer_a, size);
buffer_a = tui_redraw(&mut backend, &mut buffer_a, &mut buffer_b);
}
let timer = format!("{:>3.3}ms", perf.used.load(Relaxed));
buffer_a.set_string(0, 0, &timer, Style::default());
})
}
#[cfg(feature = "tui")] pub fn tui_resize <W: Write> (
backend: &mut CrosstermBackend<W>,
buffer: &mut Buffer,
size: WH<u16>
) {
if buffer.area != size {
backend.clear_region(ClearType::All).unwrap();
buffer.resize(size);
buffer.reset();
}
}
#[cfg(feature = "tui")] pub fn tui_redraw <'b, W: Write> (
backend: &mut CrosstermBackend<W>,
mut prev_buffer: &'b mut Buffer,
mut next_buffer: &'b mut Buffer
) {
let updates = prev_buffer.diff(&next_buffer);
backend.draw(updates.into_iter()).expect("failed to render");
Backend::flush(backend).expect("failed to flush output new_buffer");
std::mem::swap(&mut prev_buffer, &mut next_buffer);
next_buffer.reset();
next_buffer
}
#[macro_export] macro_rules! tui_main {
($expr:expr) => {
fn main () -> Usually<()> {
let engine = ::tengri::Tui::new(Box::new(stdout()))?;
let state = ::std::sync::Arc::new(std::sync::RwLock::new($expr));
engine.run(true, &state)?;
Ok(())
}
};
}