view: ErrorBoundary

This commit is contained in:
🪞👃🪞 2025-05-21 13:57:27 +03:00
parent e81955890c
commit 8c175e22f5
2 changed files with 22 additions and 27 deletions

View file

@ -2,37 +2,32 @@ use crate::*;
pub(crate) use std::fmt::Write; pub(crate) use std::fmt::Write;
pub(crate) use ::tengri::tui::ratatui::prelude::Position; pub(crate) use ::tengri::tui::ratatui::prelude::Position;
impl App { // Thunks can be natural error boundaries!
pub fn view (&self) -> impl Content<TuiOut> + '_ { struct ErrorBoundary<O: Output, T: Content<O>>(std::marker::PhantomData<O>, Perhaps<T>);
let view: Perhaps<Box<dyn Render<TuiOut>>> = impl<T: Content<TuiOut>> Content<TuiOut> for ErrorBoundary<TuiOut, T> {
Namespace::take_from(self, &mut self.config.view.clone()); fn content (&self) -> impl Render<TuiOut> + '_ {
Either(view.is_ok(), ThunkRender::new(|to|match self.1.as_ref() {
ThunkRender::new(move|to|if let Some(view) = view.as_ref().unwrap().as_ref() { Ok(Some(content)) => content.render(to),
Content::render(view, to) Ok(None) => to.blit(&"empty?", 0, 0, Some(Style::default().yellow())),
}), Err(e) => Content::render(&Tui::fg_bg(
"error?!") Rgb(255,224,244),
//Fill::xy(self.size.of(col! { Rgb(96,24,24),
//Tui::bg(Rgb(72,72,0), When(matches!(&view, Err(_)), Bsp::s( Bsp::s(
//Fixed::y(1, "view error"), Bsp::e(Tui::bold(true, "oops. "), "rendering failed."),
//Bsp::s( Bsp::e("\"why?\" ", Tui::bold(true, &format!("{e}"))),
//view.as_ref().err().map(|e|format!("{e}")), )), to)
//format!("{}", &self.config.view.0.0))))), })
//When(matches!(&view, Ok(None)), Bsp::s(
//Tui::bg(Rgb(96,48,0),
//Fixed::y(1, Fill::x(Align::w("no view returned, as defined by:")))),
//Tui::bg(Rgb(72,32,0),
//Fill::x(Stack::south(|add: &mut dyn FnMut(&dyn Render<TuiOut>)|{
//for line in self.config.view.0.0.split('\n') {
//add(&Fill::x(Align::w(line)));
//}
//}))))),
//When(matches!(&view, Ok(Some(_))), &view.unwrap().unwrap()),
//}))
} }
} }
#[tengri_proc::view(TuiOut)] #[tengri_proc::view(TuiOut)]
impl App { impl App {
pub fn view (app: &Self) -> impl Content<TuiOut> {
ErrorBoundary::<TuiOut, Box<dyn Render<TuiOut> + '_>>(
Default::default(),
Namespace::take_from(app, &mut app.config.view.clone())
)
}
pub fn view_nil (app: &Self) -> impl Content<TuiOut> { pub fn view_nil (app: &Self) -> impl Content<TuiOut> {
"nil" "nil"
} }

2
deps/tengri vendored

@ -1 +1 @@
Subproject commit 776cea6f1ba502b87bcaed3a7d794076e328df2e Subproject commit 7516517078d71183cfa4c409aa8a6948675ebe7a