tui: add ErrorBoundary component
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
🪞👃🪞 2025-08-04 15:17:46 +03:00
parent 104bb1c8e7
commit b52c1f5828
2 changed files with 26 additions and 1 deletions

View file

@ -15,10 +15,11 @@ macro_rules! impl_content_layout_render {
mod tui_border; pub use self::tui_border::*;
mod tui_button; //pub use self::tui_button::*;
mod tui_color; pub use self::tui_color::*;
mod tui_error; pub use self::tui_error::*;
mod tui_field; pub use self::tui_field::*;
mod tui_number; //pub use self::tui_number::*;
mod tui_phat; pub use self::tui_phat::*;
mod tui_repeat; pub use self::tui_repeat::*;
mod tui_number; //pub use self::tui_number::*;
mod tui_scroll; pub use self::tui_scroll::*;
mod tui_string; pub use self::tui_string::*;
mod tui_style; pub use self::tui_style::*;

View file

@ -0,0 +1,24 @@
use crate::*;
use ratatui::style::Stylize;
// Thunks can be natural error boundaries!
pub struct ErrorBoundary<O: Output, T: Content<O>>(std::marker::PhantomData<O>, Perhaps<T>);
impl<O: Output, T: Content<O>> ErrorBoundary<O, T> {
pub fn new (content: Perhaps<T>) -> Self {
Self(Default::default(), content)
}
}
impl<T: Content<TuiOut>> Content<TuiOut> for ErrorBoundary<TuiOut, T> {
fn content (&self) -> impl Render<TuiOut> + '_ {
ThunkRender::new(|to|match self.1.as_ref() {
Ok(Some(content)) => content.render(to),
Ok(None) => to.blit(&"empty?", 0, 0, Some(Style::default().yellow())),
Err(e) => Content::render(&Tui::fg_bg(
Color::Rgb(255,224,244), Color::Rgb(96,24,24), Bsp::s(
Bsp::e(Tui::bold(true, "oops. "), "rendering failed."),
Bsp::e("\"why?\" ", Tui::bold(true, &format!("{e}"))))), to)
})
}
}