mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
update examples
This commit is contained in:
parent
0e821e098f
commit
400fd9b6e9
9 changed files with 12 additions and 45 deletions
48
examples/edn.rs
Normal file
48
examples/edn.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
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<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",
|
||||
EdnItem::Sym(":world") => "world",
|
||||
_ => ""
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl Content<TuiOut> for Example {
|
||||
fn content (&self) -> impl Render<TuiOut> {
|
||||
Bsp::a(
|
||||
Push::xy(1, 1, Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len()))),
|
||||
EdnView::new(self, EDN[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))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue