fix tui example

This commit is contained in:
🪞👃🪞 2025-04-25 14:11:04 +03:00
parent 649a89443b
commit 2e9ba1b92d

View file

@ -2,33 +2,16 @@ use tengri::{self, input::*, output::*, tui::*, dsl::*};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use crossterm::event::{*, KeyCode::*}; use crossterm::event::{*, KeyCode::*};
use crate::ratatui::style::Color; use crate::ratatui::style::Color;
fn main () -> Usually<()> { fn main () -> Usually<()> {
let state = Arc::new(RwLock::new(Example(10, Measure::new()))); let state = Arc::new(RwLock::new(Example(10, Measure::new())));
Tui::new().unwrap().run(&state)?; Tui::new().unwrap().run(&state)?;
Ok(()) Ok(())
} }
#[derive(Debug)] pub struct Example(usize, Measure<TuiOut>); #[derive(Debug)] pub struct Example(usize, Measure<TuiOut>);
const KEYMAP: &str = "(:left prev) (:right next)";
handle!(TuiIn: |self: Example, input|{ const KEYMAP: &str = "(@left prev) (@right next)";
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 EXAMPLES: &'static [&'static str] = &[ const EXAMPLES: &'static [&'static str] = &[
include_str!("edn01.edn"), include_str!("edn01.edn"),
include_str!("edn02.edn"), include_str!("edn02.edn"),
@ -44,6 +27,34 @@ const EXAMPLES: &'static [&'static str] = &[
include_str!("edn12.edn"), include_str!("edn12.edn"),
include_str!("edn13.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|{ view!(TuiOut: |self: Example|{
let index = self.0 + 1; let index = self.0 + 1;
let wh = self.1.wh(); let wh = self.1.wh();
@ -60,6 +71,11 @@ view!(TuiOut: |self: Example|{
":world" => Tui::bg(Color::Rgb(100, 10, 10), "world").boxed(), ":world" => Tui::bg(Color::Rgb(100, 10, 10), "world").boxed(),
":hello-world" => "Hello world!".boxed() ":hello-world" => "Hello world!".boxed()
}); });
provide_bool!(bool: |self: Example| {});
provide_num!(u16: |self: Example| {}); expose! {
provide_num!(usize: |self: Example| {}); [self: Example] {
[bool] => {}
[u16] => {}
[usize] => {}
}
}