dsl, output, tui: add tests, examples, root dispatchers

This commit is contained in:
🪞👃🪞 2025-09-08 18:44:42 +03:00
parent 8dfe20a58c
commit ca862b9802
16 changed files with 637 additions and 377 deletions

View file

@ -4,6 +4,9 @@ description = "UI metaframework, Ratatui backend."
version = { workspace = true }
edition = { workspace = true }
[lib]
path = "src/tui.rs"
[features]
dsl = [ "dep:tengri_dsl", "tengri_output/dsl" ]
bumpalo = [ "dep:bumpalo" ]

View file

@ -1,226 +0,0 @@
use tengri::{self, Usually, Perhaps, input::*, output::*, tui::*, dsl::*};
use std::sync::{Arc, RwLock};
use crate::ratatui::style::Color;
//use crossterm::event::{*, KeyCode::*};
fn main () -> Usually<()> {
let state = Example::new();
Tui::new().unwrap().run(&state)?;
Ok(())
}
#[derive(Debug)] pub struct Example(
usize,
Measure<TuiOut>
);
impl Example {
fn new () -> Arc<RwLock<Self>> {
Arc::new(RwLock::new(Example(10, Measure::new())))
}
const BINDS: &'static str = stringify! {
(@left prev)
(@right next)
};
const VIEWS: &'static [&'static str] = &[
stringify! { :hello-world },
stringify! { (fill/xy :hello-world) },
stringify! { (bsp/s :hello :world) },
stringify! { (fixed/xy 20 10 :hello-world) },
stringify! { (bsp/s (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/e (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/n (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/w (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/a (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/b (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! {
(bsp/s
(bsp/e (align/nw (fixed/xy 5 3 :hello))
(bsp/e (align/n (fixed/xy 5 3 :hello))
(align/ne (fixed/xy 5 3 :hello))))
(bsp/s
(bsp/e (align/w (fixed/xy 5 3 :hello))
(bsp/e (align/c (fixed/xy 5 3 :hello))
(align/e (fixed/xy 5 3 :hello))))
(bsp/e (align/sw (fixed/xy 5 3 :hello))
(bsp/e (align/s (fixed/xy 5 3 :hello))
(align/se (fixed/xy 5 3 :hello))))))
},
stringify! {
(bsp/s
(bsp/e (fixed/xy 8 5 (align/nw :hello))
(bsp/e (fixed/xy 8 5 (align/n :hello))
(fixed/xy 8 5 (align/ne :hello))))
(bsp/s
(bsp/e (fixed/xy 8 5 (align/w :hello))
(bsp/e (fixed/xy 8 5 (align/c :hello))
(fixed/xy 8 5 (align/e :hello))))
(bsp/e (fixed/xy 8 5 (align/sw :hello))
(bsp/e (fixed/xy 8 5 (align/s :hello))
(fixed/xy 8 5 (align/se :hello))))))
},
stringify! {
(bsp/s
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/nw :hello)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/n :hello)))
(grow/xy 1 1 (fixed/xy 8 5 (align/ne :hello)))))
(bsp/s
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/w :hello)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/c :hello)))
(grow/xy 1 1 (fixed/xy 8 5 (align/e :hello)))))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/sw :hello)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/s :hello)))
(grow/xy 1 1 (fixed/xy 8 5 (align/se :hello)))))))
},
stringify! { :map-e },
stringify! { (align/c :map-e) },
stringify! { :map-s },
stringify! { (align/c :map-s) },
stringify! {
(align/c (bg/behind :bg0 (margin/xy 1 1 (col
(bg/behind :bg1 (border/around :border1 (margin/xy 2 1 :label1)))
(bg/behind :bg2 (border/around :border2 (margin/xy 4 2 :label2)))
(bg/behind :bg3 (border/around :border3 (margin/xy 6 3 :label3)))))))
},
];
}
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)))
});
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)
}
}
//#[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()
//}
//}
/*
fn content (&self) -> dyn Draw<Engine = Tui> {
let border_style = Style::default().fg(Color::Rgb(0,0,0));
Align::Center(Layers::new(move|add|{
add(&Background(Color::Rgb(0,128,128)))?;
add(&Margin::XY(1, 1, Stack::down(|add|{
add(&Layers::new(|add|{
add(&Background(Color::Rgb(128,96,0)))?;
add(&Border(Square(border_style)))?;
add(&Margin::XY(2, 1, "..."))?;
Ok(())
}).debug())?;
add(&Layers::new(|add|{
add(&Background(Color::Rgb(128,64,0)))?;
add(&Border(Lozenge(border_style)))?;
add(&Margin::XY(4, 2, "---"))?;
Ok(())
}).debug())?;
add(&Layers::new(|add|{
add(&Background(Color::Rgb(96,64,0)))?;
add(&Border(SquareBold(border_style)))?;
add(&Margin::XY(6, 3, "~~~"))?;
Ok(())
}).debug())?;
Ok(())
})).debug())?;
Ok(())
}))
//Align::Center(Margin::X(1, Layers::new(|add|{
//add(&Background(Color::Rgb(128,0,0)))?;
//add(&Stack::down(|add|{
//add(&Margin::Y(1, Layers::new(|add|{
//add(&Background(Color::Rgb(0,128,0)))?;
//add(&Align::Center("12345"))?;
//add(&Align::Center("FOO"))
//})))?;
//add(&Margin::XY(1, 1, Layers::new(|add|{
//add(&Align::Center("1234567"))?;
//add(&Align::Center("BAR"))?;
//add(&Background(Color::Rgb(0,0,128)))
//})))
//}))
//})))
//Align::Y(Layers::new(|add|{
//add(&Background(Color::Rgb(128,0,0)))?;
//add(&Margin::X(1, Align::Center(Stack::down(|add|{
//add(&Align::X(Margin::Y(1, Layers::new(|add|{
//add(&Background(Color::Rgb(0,128,0)))?;
//add(&Align::Center("12345"))?;
//add(&Align::Center("FOO"))
//})))?;
//add(&Margin::XY(1, 1, Layers::new(|add|{
//add(&Align::Center("1234567"))?;
//add(&Align::Center("BAR"))?;
//add(&Background(Color::Rgb(0,0,128)))
//})))?;
//Ok(())
//})))))
//}))
}
*/

124
tui/examples/tui_00.rs Normal file
View file

@ -0,0 +1,124 @@
use tengri::{self, Usually, Perhaps, input::*, output::*, tui::*, dsl::*};
use std::sync::{Arc, RwLock};
use crate::ratatui::style::Color;
//use crossterm::event::{*, KeyCode::*};
fn main () -> Usually<()> {
let state = Example::new();
Tui::new().unwrap().run(&state)?;
Ok(())
}
#[derive(Debug)] struct Example(usize, Measure<TuiOut>);
handle!(TuiIn: |self: Example, input|Ok(None));
enum ExampleCommand { Next, Prev }
impl ExampleCommand {
fn eval (&self, state: &mut Example) -> Perhaps<Self> {
match self {
Self::Next => {
state.0 = (state.0 + 1) % Example::VIEWS.len();
Ok(Some(Self::Prev))
},
Self::Prev => {
state.0 = if state.0 > 0 { state.0 - 1 } else { Example::VIEWS.len() - 1 };
Ok(Some(Self::Next))
}
}
}
}
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 = Self::VIEWS.get(self.0).unwrap_or(&"");
let heading = format!("Example {}/{} in {:?}", index, Self::VIEWS.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)))
});
impl View<TuiOut, ()> for Example {
fn view_expr <'a> (&'a self, to: &mut TuiOut, expr: &'a impl DslExpr) -> Usually<()> {
if evaluate_output_expression(self, to, expr)?
|| evaluate_output_expression_tui(self, to, expr)? {
Ok(())
} else {
Err(format!("Example::view_expr: unexpected: {expr:?}").into())
}
}
}
impl Example {
fn new () -> Arc<RwLock<Self>> {
Arc::new(RwLock::new(Example(10, Measure::new())))
}
const BINDS: &'static str = stringify! {
(@left prev)
(@right next)
};
const VIEWS: &'static [&'static str] = &[
stringify! { :hello-world },
stringify! { (fill/xy :hello-world) },
stringify! { (bsp/s :hello :world) },
stringify! { (fixed/xy 20 10 :hello-world) },
stringify! { (bsp/s (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/e (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/n (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/w (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/a (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! { (bsp/b (fixed/xy 5 6 :hello) (fixed/xy 7 8 :world)) },
stringify! {
(bsp/s
(bsp/e (align/nw (fixed/xy 5 3 :hello))
(bsp/e (align/n (fixed/xy 5 3 :hello))
(align/ne (fixed/xy 5 3 :hello))))
(bsp/s
(bsp/e (align/w (fixed/xy 5 3 :hello))
(bsp/e (align/c (fixed/xy 5 3 :hello))
(align/e (fixed/xy 5 3 :hello))))
(bsp/e (align/sw (fixed/xy 5 3 :hello))
(bsp/e (align/s (fixed/xy 5 3 :hello))
(align/se (fixed/xy 5 3 :hello))))))
},
stringify! {
(bsp/s
(bsp/e (fixed/xy 8 5 (align/nw :hello))
(bsp/e (fixed/xy 8 5 (align/n :hello))
(fixed/xy 8 5 (align/ne :hello))))
(bsp/s
(bsp/e (fixed/xy 8 5 (align/w :hello))
(bsp/e (fixed/xy 8 5 (align/c :hello))
(fixed/xy 8 5 (align/e :hello))))
(bsp/e (fixed/xy 8 5 (align/sw :hello))
(bsp/e (fixed/xy 8 5 (align/s :hello))
(fixed/xy 8 5 (align/se :hello))))))
},
stringify! {
(bsp/s
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/nw :hello)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/n :hello)))
(grow/xy 1 1 (fixed/xy 8 5 (align/ne :hello)))))
(bsp/s
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/w :hello)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/c :hello)))
(grow/xy 1 1 (fixed/xy 8 5 (align/e :hello)))))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/sw :hello)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/s :hello)))
(grow/xy 1 1 (fixed/xy 8 5 (align/se :hello)))))))
},
stringify! { :map-e },
stringify! { (align/c :map-e) },
stringify! { :map-s },
stringify! { (align/c :map-s) },
stringify! {
(align/c (bg/behind :bg0 (margin/xy 1 1 (col
(bg/behind :bg1 (border/around :border1 (margin/xy 2 1 :label1)))
(bg/behind :bg2 (border/around :border2 (margin/xy 4 2 :label2)))
(bg/behind :bg3 (border/around :border3 (margin/xy 6 3 :label3)))))))
},
];
}

105
tui/examples/tui_01.rs Normal file
View file

@ -0,0 +1,105 @@
fn main () {}
//#[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::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, VIEWS.len())))).boxed()
//}
//pub fn code (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(10, 60, 10), Push::y(2, Align::n(format!("{}", VIEWS[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()
//}
//}
//fn content (&self) -> dyn Draw<Engine = Tui> {
//let border_style = Style::default().fg(Color::Rgb(0,0,0));
//Align::Center(Layers::new(move|add|{
//add(&Background(Color::Rgb(0,128,128)))?;
//add(&Margin::XY(1, 1, Stack::down(|add|{
//add(&Layers::new(|add|{
//add(&Background(Color::Rgb(128,96,0)))?;
//add(&Border(Square(border_style)))?;
//add(&Margin::XY(2, 1, "..."))?;
//Ok(())
//}).debug())?;
//add(&Layers::new(|add|{
//add(&Background(Color::Rgb(128,64,0)))?;
//add(&Border(Lozenge(border_style)))?;
//add(&Margin::XY(4, 2, "---"))?;
//Ok(())
//}).debug())?;
//add(&Layers::new(|add|{
//add(&Background(Color::Rgb(96,64,0)))?;
//add(&Border(SquareBold(border_style)))?;
//add(&Margin::XY(6, 3, "~~~"))?;
//Ok(())
//}).debug())?;
//Ok(())
//})).debug())?;
//Ok(())
//}))
////Align::Center(Margin::X(1, Layers::new(|add|{
////add(&Background(Color::Rgb(128,0,0)))?;
////add(&Stack::down(|add|{
////add(&Margin::Y(1, Layers::new(|add|{
////add(&Background(Color::Rgb(0,128,0)))?;
////add(&Align::Center("12345"))?;
////add(&Align::Center("FOO"))
////})))?;
////add(&Margin::XY(1, 1, Layers::new(|add|{
////add(&Align::Center("1234567"))?;
////add(&Align::Center("BAR"))?;
////add(&Background(Color::Rgb(0,0,128)))
////})))
////}))
////})))
////Align::Y(Layers::new(|add|{
////add(&Background(Color::Rgb(128,0,0)))?;
////add(&Margin::X(1, Align::Center(Stack::down(|add|{
////add(&Align::X(Margin::Y(1, Layers::new(|add|{
////add(&Background(Color::Rgb(0,128,0)))?;
////add(&Align::Center("12345"))?;
////add(&Align::Center("FOO"))
////})))?;
////add(&Margin::XY(1, 1, Layers::new(|add|{
////add(&Align::Center("1234567"))?;
////add(&Align::Center("BAR"))?;
////add(&Background(Color::Rgb(0,0,128)))
////})))?;
////Ok(())
////})))))
////}))
//}

View file

@ -1,59 +0,0 @@
#![feature(type_changing_struct_update)]
mod tui_engine; pub use self::tui_engine::*;
mod tui_content; pub use self::tui_content::*;
pub(crate) use ::tengri_core::*;
#[cfg(feature = "dsl")] pub use ::tengri_dsl::*;
pub use ::tengri_input as input; pub(crate) use ::tengri_input::*;
pub use ::tengri_output as output; pub(crate) use ::tengri_output::*;
pub(crate) use atomic_float::AtomicF64;
pub use ::better_panic; pub(crate) use ::better_panic::{Settings, Verbosity};
pub use ::palette; pub(crate) use ::palette::{*, convert::*, okhsl::*};
pub use ::crossterm; pub(crate) use ::crossterm::{
ExecutableCommand,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, enable_raw_mode, disable_raw_mode},
event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, KeyEventState},
};
pub use ::ratatui; pub(crate) use ratatui::{
prelude::{Color, Style, Buffer},
style::Modifier,
backend::{Backend, CrosstermBackend, ClearType},
layout::{Size, Rect},
buffer::Cell
};
pub(crate) use std::sync::{Arc, RwLock, atomic::{AtomicBool, Ordering::*}};
pub(crate) use std::io::{stdout, Stdout};
#[cfg(test)] #[test] fn test_tui_engine () -> Usually<()> {
use crate::*;
//use std::sync::{Arc, RwLock};
struct TestComponent(String);
impl Content<TuiOut> for TestComponent {
fn content (&self) -> impl Draw<TuiOut> {
Some(self.0.as_str())
}
}
impl Handle<TuiIn> for TestComponent {
fn handle (&mut self, _from: &TuiIn) -> Perhaps<bool> {
Ok(None)
}
}
let engine = Tui::new()?;
engine.read().unwrap().exited.store(true, std::sync::atomic::Ordering::Relaxed);
let state = TestComponent("hello world".into());
let _state = std::sync::Arc::new(std::sync::RwLock::new(state));
//engine.run(&state)?;
Ok(())
}
#[cfg(test)] #[test] fn test_parse_key () {
//use KeyModifiers as Mods;
let _test = |x: &str, y|assert_eq!(KeyMatcher::new(x).build(), Some(Event::Key(y)));
//test(":x",
//KeyEvent::new(KeyCode::Char('x'), Mods::NONE));
//test(":ctrl-x",
//KeyEvent::new(KeyCode::Char('x'), Mods::CONTROL));
//test(":alt-x",
//KeyEvent::new(KeyCode::Char('x'), Mods::ALT));
//test(":shift-x",
//KeyEvent::new(KeyCode::Char('x'), Mods::SHIFT));
//test(":ctrl-alt-shift-x",
//KeyEvent::new(KeyCode::Char('x'), Mods::CONTROL | Mods::ALT | Mods::SHIFT ));
}

78
tui/src/tui.rs Normal file
View file

@ -0,0 +1,78 @@
#![feature(type_changing_struct_update)]
#[cfg(test)] mod tui_test;
mod tui_engine; pub use self::tui_engine::*;
mod tui_content; pub use self::tui_content::*;
pub use ::{
tengri_input,
tengri_output,
ratatui,
crossterm,
palette,
better_panic
};
pub(crate) use ::{
tengri_core::*,
tengri_input::*,
tengri_output::*,
atomic_float::AtomicF64,
std::{io::{stdout, Stdout}, sync::{Arc, RwLock, atomic::{AtomicBool, Ordering::*}}},
better_panic::{Settings, Verbosity},
palette::{*, convert::*, okhsl::*},
ratatui::{
prelude::{Color, Style, Buffer},
style::Modifier,
backend::{Backend, CrosstermBackend, ClearType},
layout::{Size, Rect},
buffer::Cell
},
crossterm::{
ExecutableCommand,
terminal::{EnterAlternateScreen, LeaveAlternateScreen, enable_raw_mode, disable_raw_mode},
event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, KeyEventState},
}
};
#[cfg(feature = "dsl")] use tengri_dsl::*;
#[cfg(feature = "dsl")]
pub fn evaluate_output_expression_tui <'a, S> (
state: &S, mut output: &mut TuiOut, expr: impl DslExpr + 'a
) -> Usually<bool> where
S: View<TuiOut, ()>
+ for<'b>DslNs<'b, bool>
+ for<'b>DslNs<'b, u16>
+ for<'b>DslNs<'b, Color>
{
// See `tengri_output::evaluate_output_expression`
let head = expr.head()?;
let mut frags = head.src()?.unwrap_or_default().split("/");
let args = expr.tail();
let arg0 = args.head();
let tail0 = args.tail();
let arg1 = tail0.head();
let tail1 = tail0.tail();
let arg2 = tail1.head();
match frags.next() {
Some("text") => if let Some(src) = args?.src()? { output.place(&src) },
Some("fg") => {
let arg0 = arg0?.expect("fg: expected arg 0 (color)");
output.place(&Tui::fg(
DslNs::<Color>::from(state, arg0)?.unwrap_or_else(||panic!("fg: {arg0:?}: not a color")),
Thunk::new(move|output: &mut TuiOut|state.view(output, &arg1).unwrap()),
))
},
Some("bg") => {
let arg0 = arg0?.expect("bg: expected arg 0 (color)");
output.place(&Tui::bg(
DslNs::<Color>::from(state, arg0)?.unwrap_or_else(||panic!("bg: {arg0:?}: not a color")),
Thunk::new(move|output: &mut TuiOut|state.view(output, &arg1).unwrap()),
))
},
_ => return Ok(false)
};
Ok(true)
}

View file

@ -37,6 +37,14 @@ impl Tui {
}
});
#[macro_export] macro_rules! tui_content ((|$self:ident:$Self:ty|$sexpr:expr)=>{
impl Content<TuiOut> for $Self {
fn content (&$self) -> impl Draw<TuiOut> + Layout<TuiOut> + '_ {
$expr
}
}
});
mod tui_border; pub use self::tui_border::*;
mod tui_button; pub use self::tui_button::*;
mod tui_color; pub use self::tui_color::*;

35
tui/src/tui_test.rs Normal file
View file

@ -0,0 +1,35 @@
use crate::*;
#[test] fn test_tui_engine () -> Usually<()> {
//use std::sync::{Arc, RwLock};
struct TestComponent(String);
impl Content<TuiOut> for TestComponent {
fn content (&self) -> impl Draw<TuiOut> + Layout<TuiOut> {
Some(self.0.as_str())
}
}
impl Handle<TuiIn> for TestComponent {
fn handle (&mut self, _from: &TuiIn) -> Perhaps<bool> {
Ok(None)
}
}
let engine = Tui::new()?;
engine.read().unwrap().exited.store(true, std::sync::atomic::Ordering::Relaxed);
let state = TestComponent("hello world".into());
let _state = std::sync::Arc::new(std::sync::RwLock::new(state));
//engine.run(&state)?;
Ok(())
}
//#[test] fn test_parse_key () {
////use KeyModifiers as Mods;
//let _test = |x: &str, y|assert_eq!(KeyMatcher::new(x).build(), Some(Event::Key(y)));
////test(":x",
////KeyEvent::new(KeyCode::Char('x'), Mods::NONE));
////test(":ctrl-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::CONTROL));
////test(":alt-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::ALT));
////test(":shift-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::SHIFT));
////test(":ctrl-alt-shift-x",
////KeyEvent::new(KeyCode::Char('x'), Mods::CONTROL | Mods::ALT | Mods::SHIFT ));
//}