switchable edn example

This commit is contained in:
🪞👃🪞 2025-01-05 16:51:26 +01:00
parent 4ae31bbba0
commit ce4574ed78
8 changed files with 105 additions and 21 deletions

View file

@ -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<Tui> for &Example {
impl EdnViewData<Tui> 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<Tui> for Example {
fn content (&self) -> impl Render<Tui> {
EdnView::new(self, EDN).unwrap()
EdnView::new(self, EDN[self.0]).unwrap()
}
}
impl Handle<Tui> for Example {}
impl Handle<Tui> 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

@ -12,7 +12,7 @@ fn main () -> Usually<()> {
pub struct Example;
impl EdnLayout<Tui> for &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",

73
edn/examples/edn03.edn Normal file
View file

@ -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<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(())
//})))))
//}))
}

0
edn/examples/edn03.rs Normal file
View file