tengri/examples/mode_02.rs
facile pop culture reference 39d3dacf1f fix bsp arg2
2026-08-06 10:18:28 +03:00

174 lines
6.2 KiB
Rust

//! Mode 02: Inline Dizzle config
use ::tengri::{
*,
dizzle::*,
crossterm::event::{Event::*, KeyEvent, KeyCode::*},
ratatui::style::Color,
};
tui_app!(State {
/** Command history (undo/redo). */
history: Vec<Action>,
/** User-controllable value. */
cursor: usize,
/** Rendered window size. */
size: Sizer,
});
tui_view!(self: State {
let index = self.cursor + 1;
let wh = (self.size.w(), self.size.h());
let src = VIEWS.get(self.cursor).unwrap_or(&"");
let heading = format!("Demo [Mode 02]\nExample {}/{}\nSize {:?}", index, VIEWS.len(), &wh);
let title = bg(Color::Rgb(60, 10, 10), heading);
let code = bg(Color::Rgb(10, 60, 10), format!("Source:\n{}", src).max_h(10));
let widget = draw(move|to: &mut Tui|self.interpret(to, &src)); // FIXME this forcefills
let sidebar = bg(Color::Rgb(20, 20, 20), north(code, title)).max_w(40);
let content = bg(Color::Rgb(64, 64, 64), widget);
east(sidebar, ShowSizeOf(content))
//self.size.of(bg(Color::Rgb(10, 10, 10), south(east(sidebar, content), code)).full_w())
//self.size.of(bg(Color::Rgb(10, 10, 10), east(sidebar, content)).full_w())
});
impl_keywords!(Tui, XYWH<u16>, State [
kw_when,
kw_either,
kw_split,
kw_align,
kw_exact,
kw_min,
kw_max,
kw_push,
kw_tui_text,
kw_tui_fg,
kw_tui_bg,
kw_color_g,
kw_color_rgb,
]);
impl Interpret<Tui, Option<XYWH<u16>>> for State {
fn interpret_word (&self, to: &mut Tui, sym: &impl Language) -> Perhaps<XYWH<u16>> {
match sym.src()? {
Some(":foo") => "foo".draw(to),
Some(":bar") => "bar".draw(to),
Some(":foobar") => "FOOBAR".draw(to),
src => todo!("src: {src:?}")
}
}
fn interpret_expr (&self, to: &mut Tui, src: &impl Expression) -> Perhaps<XYWH<u16>> {
if let Some(area) = self.keyword(to, src)? {
Ok(Some(area))
} else {
Err(format!("App::interpret_expr: unexpected: {src:?}").into())
}
}
}
tui_keys!(self: State, input {
Ok(if let Key(KeyEvent { code, .. }) = input.0 {
match code {
Up | Right => { self.next()?.map(|x|self.history.push(x)); },
Down | Left => { self.prev()?.map(|x|self.history.push(x)); },
_ => {}
}
})
});
impl State {
fn next (&mut self) -> Perhaps<Action> {
self.cursor = (self.cursor + 1) % VIEWS.len();
Ok(Some(Action::Prev))
}
fn prev (&mut self) -> Perhaps<Action> {
self.cursor = if self.cursor > 0 { self.cursor - 1 } else { VIEWS.len() - 1 };
Ok(Some(Action::Next))
}
}
#[derive(Debug)]
enum Action {
/** Increment cursor */ Next,
/** Decrement cursor */ Prev,
}
impl Action {
fn eval (&self, state: &mut State) -> Perhaps<Self> {
use Action::*;
match self { Next => state.next(), Prev => state.prev(), }
}
}
const VIEWS: &'static [&'static str] = &[
stringify! { :foobar },
stringify! { (text FOOBAR) },
stringify! { (bg (g 8) :foobar) },
//stringify! { (fill/xy :foobar) },
stringify! { (bsp/s (text foo) (text bar)) },
stringify! { (bsp/s :foo :bar) },
stringify! { (bsp/s (bg (g 16) :foo) (bg (g 32) :bar)) },
stringify! { (bsp/s (max/xy 20 10 (bg (g 16) :foo)) (max/y 5 (bg (g 32) :bar))) },
stringify! { (bsp/e (bg (g 16) :foo) (bg (g 32) :bar)) },
stringify! { (fixed/xy 20 10 :foobar) },
stringify! { (bsp/s (fixed/xy 5 6 :foo) (fixed/xy 7 8 :bar)) },
stringify! { (bsp/e (fixed/xy 5 6 :foo) (fixed/xy 7 8 :bar)) },
stringify! { (bsp/n (fixed/xy 5 6 :foo) (fixed/xy 7 8 :bar)) },
stringify! { (bsp/w (fixed/xy 5 6 :foo) (fixed/xy 7 8 :bar)) },
stringify! { (bsp/a (fixed/xy 5 6 :foo) (fixed/xy 7 8 :bar)) },
stringify! { (bsp/b (fixed/xy 5 6 :foo) (fixed/xy 7 8 :bar)) },
stringify! {
(bsp/s
(bsp/e (align/nw (fixed/xy 5 3 :foo))
(bsp/e (align/n (fixed/xy 5 3 :foo))
(align/ne (fixed/xy 5 3 :foo))))
(bsp/s
(bsp/e (align/w (fixed/xy 5 3 :foo))
(bsp/e (align/c (fixed/xy 5 3 :foo))
(align/e (fixed/xy 5 3 :foo))))
(bsp/e (align/sw (fixed/xy 5 3 :foo))
(bsp/e (align/s (fixed/xy 5 3 :foo))
(align/se (fixed/xy 5 3 :foo))))))
},
stringify! {
(bsp/s
(bsp/e (fixed/xy 8 5 (align/nw :foo))
(bsp/e (fixed/xy 8 5 (align/n :foo))
(fixed/xy 8 5 (align/ne :foo))))
(bsp/s
(bsp/e (fixed/xy 8 5 (align/w :foo))
(bsp/e (fixed/xy 8 5 (align/c :foo))
(fixed/xy 8 5 (align/e :foo))))
(bsp/e (fixed/xy 8 5 (align/sw :foo))
(bsp/e (fixed/xy 8 5 (align/s :foo))
(fixed/xy 8 5 (align/se :foo))))))
},
stringify! {
(bsp/s
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/nw :foo)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/n :foo)))
(grow/xy 1 1 (fixed/xy 8 5 (align/ne :foo)))))
(bsp/s
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/w :foo)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/c :foo)))
(grow/xy 1 1 (fixed/xy 8 5 (align/e :foo)))))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/sw :foo)))
(bsp/e (grow/xy 1 1 (fixed/xy 8 5 (align/s :foo)))
(grow/xy 1 1 (fixed/xy 8 5 (align/se :foo)))))))
},
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)))))))
},
];
//handle!(TuiIn: |self: State, input|Action::from(input).eval(self).map(|_|None));
//view!(State: Tui: [ evaluate_output_expression, evaluate_output_expression_tui ]);
//draw!(State: Tui: [ draw_example ]);
//impl_from!(Action: |input: &TuiIn| todo!());
//fn draw_example (state: &State, to: &mut Tui) {}