add more edn view examples

This commit is contained in:
🪞👃🪞 2025-01-10 19:01:59 +01:00
parent f64a9731ce
commit a8611db452
6 changed files with 34 additions and 17 deletions

View file

@ -10,11 +10,17 @@ pub type EdnRenderCallback<'a, O: Output, State> =
/// Provides values to the template /// Provides values to the template
pub trait EdnViewData<E: Output> { pub trait EdnViewData<E: Output> {
fn get_bool (&self, _sym: EdnItem<&str>) -> bool { false } fn get_bool (&self, _sym: EdnItem<&str>) -> bool {
fn get_usize (&self, _sym: EdnItem<&str>) -> usize { 0 } panic!("no content")
fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> { Box::new(()) } }
fn get_usize (&self, _sym: EdnItem<&str>) -> usize {
panic!("no content")
}
fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> {
panic!("no content")
}
fn get_unit (&self, num: EdnItem<&str>) -> E::Unit { fn get_unit (&self, num: EdnItem<&str>) -> E::Unit {
if let EdnItem::Num(n) = num { (n as u16).into() } else { 0.into() } if let EdnItem::Num(n) = num { (n as u16).into() } else { panic!("not a number") }
} }
} }

View file

@ -8,37 +8,44 @@ const EDN: &'static [&'static str] = &[
include_str!("edn02.edn"), include_str!("edn02.edn"),
include_str!("edn03.edn"), include_str!("edn03.edn"),
include_str!("edn04.edn"), include_str!("edn04.edn"),
include_str!("edn05.edn"),
include_str!("edn06.edn"),
include_str!("edn07.edn"),
include_str!("edn08.edn"),
]; ];
fn main () -> Usually<()> { fn main () -> Usually<()> {
let state = Arc::new(RwLock::new(Example(0))); let state = Arc::new(RwLock::new(Example(4, Measure::new())));
Tui::new().unwrap().run(&state)?; Tui::new().unwrap().run(&state)?;
Ok(()) Ok(())
} }
pub struct Example(usize); pub struct Example(usize, Measure<TuiOut>);
impl EdnViewData<TuiOut> for &Example { impl EdnViewData<TuiOut> for &Example {
fn get_content <'a> (&'a self, sym: EdnItem<&'a str>) -> RenderBox<'a, TuiOut> { fn get_content <'a> (&'a self, item: EdnItem<&'a str>) -> RenderBox<'a, TuiOut> {
Box::new(Thunk::new(move||match sym { use EdnItem::*;
EdnItem::Sym(":title") => Tui::bg(Color::Rgb(60,10,10), Push::y(1, match item {
Nil => Box::new(()),
Exp(items) => Box::new(EdnView::from_items(*self, items.as_slice())),
Sym(":title") => Tui::bg(Color::Rgb(60,10,10), Push::y(1,
Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len())))).boxed(), Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len())))).boxed(),
EdnItem::Sym(":code") => Tui::bg(Color::Rgb(10,60,10), Push::y(2, Sym(":code") => Tui::bg(Color::Rgb(10,60,10), Push::y(2,
Align::n(format!("{}", EDN[self.0])))).boxed(), Align::n(format!("{}", EDN[self.0])))).boxed(),
EdnItem::Sym(":hello-world") => "Hello world!".boxed(), Sym(":hello-world") => "Hello world!".boxed(),
EdnItem::Sym(":hello") => "Hello".boxed(), Sym(":hello") => Tui::bg(Color::Rgb(10, 100, 10), "Hello").boxed(),
EdnItem::Sym(":world") => "world".boxed(), Sym(":world") => Tui::bg(Color::Rgb(100, 10, 10), "world").boxed(),
_ => "".boxed() _ => panic!("no content for {item:?}")
})) }
} }
} }
impl Content<TuiOut> for Example { impl Content<TuiOut> for Example {
fn content (&self) -> impl Render<TuiOut> { fn content (&self) -> impl Render<TuiOut> {
let title = Tui::bg(Color::Rgb(60,10,10), Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len())))); let title = Tui::bg(Color::Rgb(60,10,10), Push::y(1, Align::n(format!("Example {}/{} in {:?}", self.0 + 1, EDN.len(), &self.1.wh()))));
let code = Tui::bg(Color::Rgb(10,60,10), Push::y(2, Align::n(format!("{}", EDN[self.0])))); let code = Tui::bg(Color::Rgb(10,60,10), Push::y(2, Align::n(format!("{}", EDN[self.0]))));
let content = Tui::bg(Color::Rgb(10,10,60), EdnView::from_source(self, EDN[self.0])); let content = Tui::bg(Color::Rgb(10,10,60), EdnView::from_source(self, EDN[self.0]));
Bsp::s(title, Bsp::n(code, content)) self.1.of(Bsp::s(title, Bsp::n(code, content)))
} }
} }

1
tek/examples/edn05.edn Normal file
View file

@ -0,0 +1 @@
(fixed/xy 30 20 (bsp/s (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world)))

1
tek/examples/edn06.edn Normal file
View file

@ -0,0 +1 @@
(fixed/xy 30 20 (bsp/e (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world)))

1
tek/examples/edn07.edn Normal file
View file

@ -0,0 +1 @@
(fixed/xy 30 20 (bsp/n (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world)))

1
tek/examples/edn08.edn Normal file
View file

@ -0,0 +1 @@
(fixed/xy 30 20 (bsp/w (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world)))