tengri/tui/examples/tui.rs
2025-09-07 21:28:47 +03:00

101 lines
3.5 KiB
Rust

use tengri::{self, Usually, Perhaps, 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!("edn/edn01.edn"),
include_str!("edn/edn02.edn"),
include_str!("edn/edn03.edn"),
include_str!("edn/edn04.edn"),
include_str!("edn/edn05.edn"),
include_str!("edn/edn06.edn"),
include_str!("edn/edn07.edn"),
include_str!("edn/edn08.edn"),
include_str!("edn/edn09.edn"),
include_str!("edn/edn10.edn"),
include_str!("edn/edn11.edn"),
include_str!("edn/edn12.edn"),
//include_str!("edn/edn13.edn"),
include_str!("edn/edn14.edn"),
include_str!("edn/edn15.edn"),
include_str!("edn/edn16.edn"),
include_str!("edn/edn17.edn"),
];
handle!(TuiIn: |self: Example, input|{
Ok(None)/*if let Some(command) = CstIter::new(KEYMAP).command::<_, ExampleCommand, _>(self, input) {
command.execute(self)?;
Some(true)
} else {
None
})*/
});
//#[tengri_proc::expose]
//impl Example {
//fn _todo_u16_stub (&self) -> u16 { todo!() }
//fn _todo_bool_stub (&self) -> bool { todo!() }
//fn _todo_usize_stub (&self) -> usize { todo!() }
////[bool] => {}
////[u16] => {}
////[usize] => {}
//}
#[tengri_proc::command(Example)]
impl ExampleCommand {
fn next (state: &mut Example) -> Perhaps<Self> {
state.0 = (state.0 + 1) % EXAMPLES.len();
Ok(None)
}
fn prev (state: &mut Example) -> Perhaps<Self> {
state.0 = if state.0 > 0 { state.0 - 1 } else { EXAMPLES.len() - 1 };
Ok(None)
}
}
tui_draw!(|self: Example, to|to.place(&self.content()));
content!(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, CstIter::new(src)));
self.1.of(Bsp::s(title, Bsp::n(""/*code*/, content)))
});
//#[tengri_proc::view(TuiOut)]
//impl Example {
//pub fn title (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(60, 10, 10), Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EXAMPLES.len())))).boxed()
//}
//pub fn code (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(10, 60, 10), Push::y(2, Align::n(format!("{}", EXAMPLES[self.0])))).boxed()
//}
//pub fn hello (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(10, 100, 10), "Hello").boxed()
//}
//pub fn world (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(100, 10, 10), "world").boxed()
//}
//pub fn hello_world (&self) -> impl Draw<TuiOut> + use<'_> {
//"Hello world!".boxed()
//}
//pub fn map_e (&self) -> impl Draw<TuiOut> + use<'_> {
//Map::east(5u16, ||0..5u16, |n, _i|format!("{n}")).boxed()
//}
//pub fn map_s (&self) -> impl Draw<TuiOut> + use<'_> {
//Map::south(5u16, ||0..5u16, |n, _i|format!("{n}")).boxed()
//}
//}