From ce4574ed78423552d9d4ad29579e630a7c0e3847 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 5 Jan 2025 16:51:26 +0100 Subject: [PATCH] switchable edn example --- edn/examples/edn01.rs | 32 ++++++++++++++---- edn/examples/edn02.rs | 2 +- edn/examples/edn03.edn | 73 ++++++++++++++++++++++++++++++++++++++++++ edn/examples/edn03.rs | 0 edn/src/edn_main.rs | 6 ---- edn/src/edn_view.rs | 8 ++--- edn/src/lib.rs | 3 +- src/groovebox.rs | 2 +- 8 files changed, 105 insertions(+), 21 deletions(-) create mode 100644 edn/examples/edn03.edn create mode 100644 edn/examples/edn03.rs delete mode 100644 edn/src/edn_main.rs diff --git a/edn/examples/edn01.rs b/edn/examples/edn01.rs index 0fc3eb4d..42d641c4 100644 --- a/edn/examples/edn01.rs +++ b/edn/examples/edn01.rs @@ -1,27 +1,45 @@ use tek_tui::{*, tek_layout::{*, tek_engine::*}}; use tek_edn::*; use std::sync::{Arc, RwLock}; +use crossterm::event::{*, KeyCode::*}; -const EDN: &'static str = include_str!("edn01.edn"); +const EDN: &'static [&'static str] = &[ + include_str!("edn01.edn"), + include_str!("edn02.edn"), +]; fn main () -> Usually<()> { - let state = Arc::new(RwLock::new(Example)); + let state = Arc::new(RwLock::new(Example(0))); Tui::new().unwrap().run(&state)?; Ok(()) } -pub struct Example; +pub struct Example(usize); -impl EdnLayout for &Example { +impl EdnViewData for &Example { fn get_content <'a> (&'a self, sym: &'a str) -> RenderBox<'a, Tui> { - Box::new(Thunk::new(move||if sym == ":hello-world" { "Hello world!" } else { "" })) + Box::new(Thunk::new(move||match sym { + ":hello-world" => "Hello world!", + ":hello" => "Hello", + ":world" => "world", + _ => "" + })) } } impl Content for Example { fn content (&self) -> impl Render { - EdnView::new(self, EDN).unwrap() + EdnView::new(self, EDN[self.0]).unwrap() } } -impl Handle for Example {} +impl Handle for Example { + fn handle (&mut self, input: &TuiIn) -> Perhaps { + match input.event() { + kpat!(Right) => self.0 = (self.0 + 1) % EDN.len(), + kpat!(Left) => self.0 = if self.0 > 0 { self.0 - 1 } else { EDN.len() - 1 }, + _ => return Ok(None) + } + Ok(Some(true)) + } +} diff --git a/edn/examples/edn02.rs b/edn/examples/edn02.rs index 35e2466c..7e85f3ce 100644 --- a/edn/examples/edn02.rs +++ b/edn/examples/edn02.rs @@ -12,7 +12,7 @@ fn main () -> Usually<()> { pub struct Example; -impl EdnLayout for &Example { +impl EdnViewData for &Example { fn get_content <'a> (&'a self, sym: &'a str) -> RenderBox<'a, Tui> { Box::new(Thunk::new(move||match sym { ":hello" => "Hello", diff --git a/edn/examples/edn03.edn b/edn/examples/edn03.edn new file mode 100644 index 00000000..cac47263 --- /dev/null +++ b/edn/examples/edn03.edn @@ -0,0 +1,73 @@ +(align/c (bg/behind :bg0 (margin/xy 1 1 (col + (bg/behind :bg1 (border/around :border1 (margin/xy 2 1 :label1))) + (bg/behind :bg2 (border/around :border2 (margin/xy 4 2 :label2))) + (bg/behind :bg3 (border/around :border3 (margin/xy 6 3 :label3))))))) + + fn content (&self) -> dyn Render { + let border_style = Style::default().fg(Color::Rgb(0,0,0)); + Align::Center(Layers::new(move|add|{ + + add(&Background(Color::Rgb(0,128,128)))?; + + add(&Margin::XY(1, 1, Stack::down(|add|{ + + add(&Layers::new(|add|{ + add(&Background(Color::Rgb(128,96,0)))?; + add(&Border(Square(border_style)))?; + add(&Margin::XY(2, 1, "..."))?; + Ok(()) + }).debug())?; + + add(&Layers::new(|add|{ + add(&Background(Color::Rgb(128,64,0)))?; + add(&Border(Lozenge(border_style)))?; + add(&Margin::XY(4, 2, "---"))?; + Ok(()) + }).debug())?; + + add(&Layers::new(|add|{ + add(&Background(Color::Rgb(96,64,0)))?; + add(&Border(SquareBold(border_style)))?; + add(&Margin::XY(6, 3, "~~~"))?; + Ok(()) + }).debug())?; + + Ok(()) + })).debug())?; + + Ok(()) + + })) + //Align::Center(Margin::X(1, Layers::new(|add|{ + //add(&Background(Color::Rgb(128,0,0)))?; + //add(&Stack::down(|add|{ + //add(&Margin::Y(1, Layers::new(|add|{ + //add(&Background(Color::Rgb(0,128,0)))?; + //add(&Align::Center("12345"))?; + //add(&Align::Center("FOO")) + //})))?; + //add(&Margin::XY(1, 1, Layers::new(|add|{ + //add(&Align::Center("1234567"))?; + //add(&Align::Center("BAR"))?; + //add(&Background(Color::Rgb(0,0,128))) + //}))) + //})) + //}))) + + //Align::Y(Layers::new(|add|{ + //add(&Background(Color::Rgb(128,0,0)))?; + //add(&Margin::X(1, Align::Center(Stack::down(|add|{ + //add(&Align::X(Margin::Y(1, Layers::new(|add|{ + //add(&Background(Color::Rgb(0,128,0)))?; + //add(&Align::Center("12345"))?; + //add(&Align::Center("FOO")) + //})))?; + //add(&Margin::XY(1, 1, Layers::new(|add|{ + //add(&Align::Center("1234567"))?; + //add(&Align::Center("BAR"))?; + //add(&Background(Color::Rgb(0,0,128))) + //})))?; + //Ok(()) + //}))))) + //})) + } diff --git a/edn/examples/edn03.rs b/edn/examples/edn03.rs new file mode 100644 index 00000000..e69de29b diff --git a/edn/src/edn_main.rs b/edn/src/edn_main.rs deleted file mode 100644 index 1bd543b9..00000000 --- a/edn/src/edn_main.rs +++ /dev/null @@ -1,6 +0,0 @@ -#[macro_export] macro_rules! run_tek_edn { - ($source:expr) => { - struct EdnRunner; - impl EdnLayout for EdnRunner; - } -} diff --git a/edn/src/edn_view.rs b/edn/src/edn_view.rs index 170bef06..cf9d91b6 100644 --- a/edn/src/edn_view.rs +++ b/edn/src/edn_view.rs @@ -9,7 +9,7 @@ pub type EdnCallback<'a, Engine, State> = pub type EdnRenderCallback<'a, Engine, State> = Box>; -pub trait EdnLayout { +pub trait EdnViewData { fn get_bool (&self, _sym: &str) -> bool { false } fn get_unit (&self, _sym: &str) -> E::Unit { 0.into() } fn get_usize (&self, _sym: &str) -> usize { 0 } @@ -17,21 +17,21 @@ pub trait EdnLayout { } /// Renders from EDN source and context. -pub struct EdnView> { +pub struct EdnView> { _engine: PhantomData, context: T, layout: EdnItem //render: BoxBox + Send + Sync + 'a> + Send + Sync + 'a> } -impl> EdnView { +impl> EdnView { pub fn new (context: T, source: &str) -> Usually { let layout = EdnItem::read_one(&source)?.0; Ok(Self { _engine: Default::default(), context, layout, }) } } -impl + Send + Sync> Content for EdnView { +impl + Send + Sync> Content for EdnView { fn content (&self) -> impl Render { use EdnItem::*; match &self.layout { diff --git a/edn/src/lib.rs b/edn/src/lib.rs index 397cd068..28e540ce 100644 --- a/edn/src/lib.rs +++ b/edn/src/lib.rs @@ -9,7 +9,6 @@ pub use ::tek_layout; mod edn_error; pub use self::edn_error::*; mod edn_item; pub use self::edn_item::*; mod edn_iter; pub use self::edn_iter::*; -mod edn_main; pub use self::edn_main::*; mod edn_token; pub use self::edn_token::*; mod edn_view; pub use self::edn_view::*; @@ -38,6 +37,6 @@ mod edn_view; pub use self::edn_view::*; EdnItem::::read_all(include_str!("../examples/edn01.edn"))?; EdnItem::::read_all(include_str!("../examples/edn02.edn"))?; //panic!("{layout:?}"); - //let content = >::from(&layout); + //let content = >::from(&layout); Ok(()) } diff --git a/src/groovebox.rs b/src/groovebox.rs index 20c83b4b..ce865ae6 100644 --- a/src/groovebox.rs +++ b/src/groovebox.rs @@ -61,7 +61,7 @@ impl Groovebox { has_clock!(|self: Groovebox|self.player.clock()); -impl EdnLayout for Groovebox { +impl EdnViewData for Groovebox { fn get_bool (&self, item: &str) -> bool { todo!() } fn get_unit (&self, item: &str) -> u16 { use EdnItem::*;