tengri/tui/examples/tui.rs

87 lines
2.8 KiB
Rust

use tengri::{self, input::*, output::*, tui::*, dsl::*};
use std::sync::{Arc, RwLock};
use crossterm::event::{*, KeyCode::*};
use crate::ratatui::style::Color;
fn main () -> Usually<()> {
let state = Arc::new(RwLock::new(Example(10, Measure::new())));
Tui::new().unwrap().run(&state)?;
Ok(())
}
#[derive(Debug)] pub struct Example(usize, Measure<TuiOut>);
const KEYMAP: &str = "(@left prev) (@right next)";
const EXAMPLES: &'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"),
include_str!("edn14.edn"),
include_str!("edn15.edn"),
include_str!("edn16.edn"),
include_str!("edn17.edn"),
];
handle!(TuiIn: |self: Example, input|{
Ok(if let Some(command) = SourceIter::new(KEYMAP).command::<_, ExampleCommand, _>(self, input) {
command.execute(self)?;
Some(true)
} else {
None
})
});
defcom! { |self, state: Example|
ExampleCommand {
Next => {
state.0 = (state.0 + 1) % EXAMPLES.len();
None
}
Prev => {
state.0 = if state.0 > 0 { state.0 - 1 } else { EXAMPLES.len() - 1 };
None
}
}
}
atom_command!(ExampleCommand: |app: Example| {
("prev" [] Some(Self::Prev))
("next" [] Some(Self::Next))
});
view!(TuiOut: |self: Example|{
let index = self.0 + 1;
let wh = self.1.wh();
let src = EXAMPLES.get(self.0).unwrap_or(&"");
let heading = format!("Example {}/{} in {:?}", index, EXAMPLES.len(), &wh);
let title = Tui::bg(Color::Rgb(60, 10, 10), Push::y(1, Align::n(heading)));
let code = Tui::bg(Color::Rgb(10, 60, 10), Push::y(2, Align::n(format!("{}", src))));
let content = Tui::bg(Color::Rgb(10, 10, 60), View(self, SourceIter::new(src)));
self.1.of(Bsp::s(title, Bsp::n(""/*code*/, content)))
}; {
":title" => Tui::bg(Color::Rgb(60, 10, 10), Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EXAMPLES.len())))).boxed(),
":code" => Tui::bg(Color::Rgb(10, 60, 10), Push::y(2, Align::n(format!("{}", EXAMPLES[self.0])))).boxed(),
":hello" => Tui::bg(Color::Rgb(10, 100, 10), "Hello").boxed(),
":world" => Tui::bg(Color::Rgb(100, 10, 10), "world").boxed(),
":hello-world" => "Hello world!".boxed(),
":map-e" => Map::east(5u16, ||0..5u16, |n, i|format!("{n}")).boxed(),
":map-s" => Map::south(5u16, ||0..5u16, |n, i|format!("{n}")).boxed(),
});
expose! {
[self: Example] {
[bool] => {}
[u16] => {}
[usize] => {}
}
}