From a8611db452b0c0da735d283170398932f19f3023 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 10 Jan 2025 19:01:59 +0100 Subject: [PATCH] add more edn view examples --- output/src/edn_view.rs | 14 ++++++++++---- tek/examples/edn.rs | 33 ++++++++++++++++++++------------- tek/examples/edn05.edn | 1 + tek/examples/edn06.edn | 1 + tek/examples/edn07.edn | 1 + tek/examples/edn08.edn | 1 + 6 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 tek/examples/edn05.edn create mode 100644 tek/examples/edn06.edn create mode 100644 tek/examples/edn07.edn create mode 100644 tek/examples/edn08.edn diff --git a/output/src/edn_view.rs b/output/src/edn_view.rs index 3a62f875..c9b58c23 100644 --- a/output/src/edn_view.rs +++ b/output/src/edn_view.rs @@ -10,11 +10,17 @@ pub type EdnRenderCallback<'a, O: Output, State> = /// Provides values to the template pub trait EdnViewData { - fn get_bool (&self, _sym: EdnItem<&str>) -> bool { false } - fn get_usize (&self, _sym: EdnItem<&str>) -> usize { 0 } - fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> { Box::new(()) } + fn get_bool (&self, _sym: EdnItem<&str>) -> bool { + panic!("no content") + } + 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 { - 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") } } } diff --git a/tek/examples/edn.rs b/tek/examples/edn.rs index 5c24a81d..a0cf8b2c 100644 --- a/tek/examples/edn.rs +++ b/tek/examples/edn.rs @@ -8,37 +8,44 @@ const EDN: &'static [&'static str] = &[ include_str!("edn02.edn"), include_str!("edn03.edn"), include_str!("edn04.edn"), + include_str!("edn05.edn"), + include_str!("edn06.edn"), + include_str!("edn07.edn"), + include_str!("edn08.edn"), ]; 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)?; Ok(()) } -pub struct Example(usize); +pub struct Example(usize, Measure); impl EdnViewData for &Example { - fn get_content <'a> (&'a self, sym: EdnItem<&'a str>) -> RenderBox<'a, TuiOut> { - Box::new(Thunk::new(move||match sym { - EdnItem::Sym(":title") => Tui::bg(Color::Rgb(60,10,10), Push::y(1, + fn get_content <'a> (&'a self, item: EdnItem<&'a str>) -> RenderBox<'a, TuiOut> { + use EdnItem::*; + 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(), - 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(), - EdnItem::Sym(":hello-world") => "Hello world!".boxed(), - EdnItem::Sym(":hello") => "Hello".boxed(), - EdnItem::Sym(":world") => "world".boxed(), - _ => "".boxed() - })) + Sym(":hello-world") => "Hello world!".boxed(), + Sym(":hello") => Tui::bg(Color::Rgb(10, 100, 10), "Hello").boxed(), + Sym(":world") => Tui::bg(Color::Rgb(100, 10, 10), "world").boxed(), + _ => panic!("no content for {item:?}") + } } } impl Content for Example { fn content (&self) -> impl Render { - 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 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))) } } diff --git a/tek/examples/edn05.edn b/tek/examples/edn05.edn new file mode 100644 index 00000000..9a4a46a3 --- /dev/null +++ b/tek/examples/edn05.edn @@ -0,0 +1 @@ +(fixed/xy 30 20 (bsp/s (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world))) diff --git a/tek/examples/edn06.edn b/tek/examples/edn06.edn new file mode 100644 index 00000000..99311e9f --- /dev/null +++ b/tek/examples/edn06.edn @@ -0,0 +1 @@ +(fixed/xy 30 20 (bsp/e (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world))) diff --git a/tek/examples/edn07.edn b/tek/examples/edn07.edn new file mode 100644 index 00000000..b7b1ea85 --- /dev/null +++ b/tek/examples/edn07.edn @@ -0,0 +1 @@ +(fixed/xy 30 20 (bsp/n (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world))) diff --git a/tek/examples/edn08.edn b/tek/examples/edn08.edn new file mode 100644 index 00000000..ecb1c81c --- /dev/null +++ b/tek/examples/edn08.edn @@ -0,0 +1 @@ +(fixed/xy 30 20 (bsp/w (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world)))