diff --git a/tui/examples/tui.rs b/tui/examples/tui.rs index a52fbb4..f737fb3 100644 --- a/tui/examples/tui.rs +++ b/tui/examples/tui.rs @@ -2,33 +2,16 @@ 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); -const KEYMAP: &str = "(:left prev) (:right next)"; -handle!(TuiIn: |self: Example, input|{ - let keymap = SourceIter::new(KEYMAP); - let command = keymap.command::<_, ExampleCommand, _>(self, input); - if let Some(command) = command { - command.execute(self)?; - return Ok(Some(true)) - } - return Ok(None) -}); -enum ExampleCommand { Next, Previous } -atom_command!(ExampleCommand: |app: Example| { - (":prev" [] Some(Self::Previous)) - (":next" [] Some(Self::Next)) -}); -command!(|self: ExampleCommand, state: Example|match self { - Self::Next => - { state.0 = (state.0 + 1) % EXAMPLES.len(); None }, - Self::Previous => - { state.0 = if state.0 > 0 { state.0 - 1 } else { EXAMPLES.len() - 1 }; None }, -}); + +const KEYMAP: &str = "(@left prev) (@right next)"; const EXAMPLES: &'static [&'static str] = &[ include_str!("edn01.edn"), include_str!("edn02.edn"), @@ -44,6 +27,34 @@ const EXAMPLES: &'static [&'static str] = &[ include_str!("edn12.edn"), include_str!("edn13.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(); @@ -60,6 +71,11 @@ view!(TuiOut: |self: Example|{ ":world" => Tui::bg(Color::Rgb(100, 10, 10), "world").boxed(), ":hello-world" => "Hello world!".boxed() }); -provide_bool!(bool: |self: Example| {}); -provide_num!(u16: |self: Example| {}); -provide_num!(usize: |self: Example| {}); + +expose! { + [self: Example] { + [bool] => {} + [u16] => {} + [usize] => {} + } +}