mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
add more edn view examples
This commit is contained in:
parent
f64a9731ce
commit
a8611db452
6 changed files with 34 additions and 17 deletions
|
|
@ -10,11 +10,17 @@ pub type EdnRenderCallback<'a, O: Output, State> =
|
|||
|
||||
/// Provides values to the template
|
||||
pub trait EdnViewData<E: Output> {
|
||||
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") }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<TuiOut>);
|
||||
|
||||
impl EdnViewData<TuiOut> 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<TuiOut> for Example {
|
||||
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 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
1
tek/examples/edn05.edn
Normal 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
1
tek/examples/edn06.edn
Normal 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
1
tek/examples/edn07.edn
Normal 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
1
tek/examples/edn08.edn
Normal file
|
|
@ -0,0 +1 @@
|
|||
(fixed/xy 30 20 (bsp/w (fixed/xy 5 5 :hello) (fixed/xy 5 5 :world)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue