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

@ -1,49 +0,0 @@
use tek_tui::{*, tek_layout::{*, tek_engine::*}};
use tek_edn::*;
use std::sync::{Arc, RwLock};
use crossterm::event::{*, KeyCode::*};
const EDN: &'static [&'static str] = &[
include_str!("edn01.edn"),
include_str!("edn02.edn"),
];
fn main () -> Usually<()> {
let state = Arc::new(RwLock::new(Example(0)));
Tui::new().unwrap().run(&state)?;
Ok(())
}
pub struct Example(usize);
impl EdnViewData<Tui> for &Example {
fn get_content <'a> (&'a self, sym: EdnItem<&'a str>) -> RenderBox<'a, Tui> {
Box::new(Thunk::new(move||match sym {
EdnItem::Sym(":hello-world") => "Hello world!",
EdnItem::Sym(":hello") => "Hello",
EdnItem::Sym(":world") => "world",
_ => ""
}))
}
}
impl Content<TuiOut> for Example {
fn content (&self) -> impl Render<TuiOut> {
Bsp::a(
&format!("{}", self.0),
EdnView::new(self, EDN[self.0])
.unwrap_or_else(|e|format!("Failed to render {}: {e}", self.0))
)
}
}
impl Handle<TuiIn> for Example {
fn handle (&mut self, input: &TuiIn) -> Perhaps<bool> {
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))
}
}

View file

@ -1 +0,0 @@
:hello-world

View file

@ -1 +0,0 @@
(bsp/s :hello :world)

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

@ -1,73 +0,0 @@
(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<Engine = Tui> {
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(())
//})))))
//}))
}

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}"))
}
}
}