update examples

This commit is contained in:
🪞👃🪞 2025-01-05 22:17:45 +01:00
parent 0e821e098f
commit 400fd9b6e9
9 changed files with 12 additions and 45 deletions

View file

@ -119,13 +119,9 @@ plugin:
reset
cargo run --bin tek_plugin
edn01:
edn:
reset
cd edn && cargo run --example edn01
edn02:
reset
cd edn && cargo run --example edn02
cargo run --example edn
test:
reset

View file

@ -1,31 +0,0 @@
use tek_tui::{*, tek_layout::{*, tek_engine::*}};
use tek_edn::*;
use std::sync::{Arc, RwLock};
const EDN: &'static str = include_str!("edn02.edn");
fn main () -> Usually<()> {
let state = Arc::new(RwLock::new(Example));
Tui::new().unwrap().run(&state)?;
Ok(())
}
pub struct Example;
impl EdnViewData<Tui> for &Example {
fn get_content <'a> (&'a self, sym: &'a str) -> RenderBox<'a, Tui> {
Box::new(Thunk::new(move||match sym {
":hello" => "Hello",
":world" => "world",
_ => ""
}))
}
}
impl Content<TuiOut> for Example {
fn content (&self) -> impl Render<TuiOut> {
EdnView::new(self, EDN).unwrap()
}
}
impl Handle<TuiIn> for Example {}

View file

View file

@ -9,6 +9,7 @@ pub type EdnCallback<'a, Output, State> =
pub type EdnRenderCallback<'a, Output, State> =
Box<EdnCallback<'a, Output, State>>;
/// Provides values to the template
pub trait EdnViewData<E: Output> {
fn get_bool (&self, _sym: EdnItem<&str>) -> bool { false }
fn get_unit (&self, _sym: EdnItem<&str>) -> E::Unit { 0.into() }
@ -25,9 +26,11 @@ pub enum EdnView<E: Output, T: EdnViewData<E>> {
}
impl<E: Output, T: EdnViewData<E>> EdnView<E, T> {
pub fn new (context: T, source: &str) -> Usually<Self> {
let layout = EdnItem::read_one(&source)?.0;
Ok(Self::Ok(context, layout))
pub fn new (state: T, source: &str) -> Self {
match EdnItem::read_one(&source) {
Ok((layout, _)) => Self::Ok(state, layout),
Err(error) => Self::Err(format!("{error}"))
}
}
}

View file

@ -16,8 +16,8 @@ fn main () -> Usually<()> {
pub struct Example(usize);
impl EdnViewData<Tui> for &Example {
fn get_content <'a> (&'a self, sym: EdnItem<&'a str>) -> RenderBox<'a, Tui> {
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(":hello-world") => "Hello world!",
EdnItem::Sym(":hello") => "Hello",
@ -30,9 +30,8 @@ impl EdnViewData<Tui> for &Example {
impl Content<TuiOut> for Example {
fn content (&self) -> impl Render<TuiOut> {
Bsp::a(
&format!("{}", self.0),
Push::xy(1, 1, Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len()))),
EdnView::new(self, EDN[self.0])
.unwrap_or_else(|e|format!("Failed to render {}: {e}", self.0))
)
}
}

View file

@ -8,7 +8,7 @@ const EDN: &'static str = include_str!("groovebox.edn");
impl Content<TuiOut> for Groovebox {
fn content (&self) -> impl Render<TuiOut> {
self.size.of(EdnView::new(self, EDN).expect("failed to build view"))
self.size.of(EdnView::new(self, EDN))
}
}