This commit is contained in:
facile pop culture reference 2026-07-31 06:47:30 +03:00
parent 94c26f06cc
commit fc01fd6ad3
40 changed files with 2075 additions and 1807 deletions

View file

@ -1,9 +1,12 @@
//! Mode 02: Inline Dizzle config
use ::std::sync::{Arc, RwLock};
use ::crossterm::event::{Event::*, KeyEvent, KeyCode::*};
use ::ratatui::style::Color;
use ::tengri::{*, lang::*};
use ::tengri::{
*,
lang::*,
crossterm::event::{Event::*, KeyEvent, KeyCode::*},
ratatui::style::Color,
};
tui_app!(State {
/** Command history (undo/redo). */
history: Vec<Action>,
@ -12,47 +15,18 @@ tui_app!(State {
/** Rendered window size. */
size: Sizer,
});
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)); },
_ => {}
}
})
});
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!("State {}/{} in {:?}", index, VIEWS.len(), &wh);
let title = bg(Color::Rgb(60, 10, 10), heading.align_n().push_y(1));
let code = bg(Color::Rgb(10, 60, 10), format!("{}", src).align_n().push_y(2));
let title = bg(Color::Rgb(60, 10, 10), heading.align_n().push_xy(2, 1)).exact_h(3);
let code = bg(Color::Rgb(10, 60, 10), format!("Source:\n{}", src).align_n().push_xy(2, 1)).min_h(4);
let widget = thunk(move|to: &mut Tui|self.interpret(to, &src));
self.size.of(south(title, north(code, widget)))
self.size.of(east(south(title, code).max_w(40), widget))
});
impl Interpret<Tui, Color> for State {
fn interpret_expr (&self, to: &mut Tui, expr: &impl Language) -> Usually<Color> {
let expr = expr.expr()?;
match expr.head()? {
Some("g") if let Some(tail) = expr.tail()? => {
Color::new_g(tail.head()?, try_to_u8)
},
Some("rgb") if let Some(tail) = expr.tail()? => {
Color::new_rgb(tail.head()?, try_to_u8)
},
_ => Err(format!("not a color").into())
}
}
}
fn try_to_u8 (src: Perhaps<&str>) -> Perhaps<u8> {
use std::str::FromStr;
if let Some(src) = src? {
Ok(Some(u8::from_str(src)?))
} else {
Ok(None)
}
}
impl Interpret<Tui, Option<XYWH<u16>>> for State {
fn interpret_word (&self, to: &mut Tui, sym: &impl Language) -> Perhaps<XYWH<u16>> {
match sym.src()? {
@ -65,13 +39,24 @@ impl Interpret<Tui, Option<XYWH<u16>>> for State {
fn interpret_expr (&self, to: &mut Tui, src: &impl Expression) -> Perhaps<XYWH<u16>> {
Ok(Some(if let Some(area) = eval_view(self, to, src)? {
area
} else if let Some(area) = eval_view_tui(self, to, src)? {
} else if let Some(area) = Tui::eval_view(self, to, src)? {
area
} else {
return 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();
@ -82,22 +67,30 @@ impl State {
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)) },
@ -155,6 +148,7 @@ const VIEWS: &'static [&'static str] = &[
(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 ]);