tek/tek/examples/edn.rs

70 lines
2.5 KiB
Rust

use tek::*;
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"),
include_str!("edn03.edn"),
include_str!("edn04.edn"),
include_str!("edn05.edn"),
include_str!("edn06.edn"),
include_str!("edn07.edn"),
include_str!("edn08.edn"),
include_str!("edn09.edn"),
include_str!("edn10.edn"),
include_str!("edn11.edn"),
include_str!("edn12.edn"),
include_str!("edn13.edn"),
];
fn main () -> Usually<()> {
let state = Arc::new(RwLock::new(Example(10, Measure::new())));
Tui::new().unwrap().run(&state)?;
Ok(())
}
pub struct Example(usize, Measure<TuiOut>);
impl EdnViewData<TuiOut> for &Example {
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(name) => self.get_sym(name), TODO
Sym(":title") => Tui::bg(Color::Rgb(60,10,10), Push::y(1,
Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len())))).boxed(),
Sym(":code") => Tui::bg(Color::Rgb(10,60,10), Push::y(2,
Align::n(format!("{}", EDN[self.0])))).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 {}/{} 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]));
self.1.of(Bsp::s(title, Bsp::n(""/*code*/, content)))
}
}
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))
}
}