wip: refactor(output): simplify layout traits
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
same mf who else 2026-02-04 19:46:54 +02:00
parent f3fcf663a8
commit 501782f8fe
18 changed files with 841 additions and 908 deletions

View file

@ -9,7 +9,7 @@ dsl = [ "tengri_output/dsl" ]
bumpalo = [ "dep:bumpalo" ]
[dependencies]
dizzle = { workspace = true }
dizzle = { workspace = true }
tengri_input = { workspace = true }
tengri_output = { workspace = true }

View file

@ -1,123 +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(())
use ::{std::sync::{Arc, RwLock}, ratatui::style::Color,
tengri::{*, input::*, output::*}, tengri_tui::*};
tui_main!(State { cursor: 10, ..Default::default() });
namespace!(State: bool {});
namespace!(State: u16 {});
namespace!(State: Color {});
handle!(TuiIn: |self: State, input|Action::from(input).eval(self).map(|_|None));
from!(Action: |input: &TuiIn| todo!());
view!(State: TuiOut: [ evaluate_output_expression, evaluate_output_expression_tui ]);
draw!(State: TuiOut: [ draw_example ]);
#[derive(Debug, Default)] struct State {
/** Rendered window size */ size: Measure<TuiOut>,
/** Command history (undo/redo) */ history: Vec<Action>,
/** User-controllable value */ cursor: usize,
}
#[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))
}
}
#[derive(Debug)] enum Action {
/** Increment cursor */ Next,
/** Decrement cursor */ Prev
}
fn draw_example (state: &State, to: &mut TuiOut) {
let index = state.cursor + 1;
let wh = state.size.wh();
let src = VIEWS.get(state.cursor).unwrap_or(&"");
let heading = format!("State {}/{} in {:?}", index, 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(state, CstIter::new(src)));
state.size.of(Bsp::s(title, Bsp::n(code, state.view(to, &src).unwrap()))).draw(to)
}
impl Action {
const BINDS: &'static str = stringify! { (@left prev) (@right next) };
fn eval (&self, state: &mut State) -> Perhaps<Self> {
use Action::*;
match self { Next => Self::next(state), Prev => Self::prev(state), }
}
fn next (state: &mut State) -> Perhaps<Self> {
state.cursor = (state.cursor + 1) % VIEWS.len();
Ok(Some(Self::Prev))
}
fn prev (state: &mut State) -> Perhaps<Self> {
state.cursor = if state.cursor > 0 { state.cursor - 1 } else { VIEWS.len() - 1 };
Ok(Some(Self::Next))
}
}
const VIEWS: &'static [&'static str] = &[
impl Draw<TuiOut> for Example {
fn content (&self) -> impl Draw<TuiOut> {
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())
}
}
}
stringify! { :hello-world },
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! {
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/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)))))))
},
];
}
(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)))))))
},
];

View file

@ -1,5 +1,13 @@
#![feature(type_changing_struct_update, trait_alias)]
pub use ::{tengri_input, tengri_output, ratatui, crossterm, palette, better_panic};
pub use ::{
dizzle,
tengri_input,
tengri_output,
ratatui,
crossterm,
palette,
better_panic,
};
pub(crate) use ::{
dizzle::*,
tengri_input::*,
@ -23,13 +31,23 @@ pub(crate) use ::{
};
mod tui_engine; pub use self::tui_engine::*;
mod tui_content; pub use self::tui_content::*;
#[macro_export] macro_rules! tui_main {
($expr:expr) => {
fn main () -> Usually<()> {
let state = Arc::new(RwLock::new($expr));
tengri_tui::Tui::new().unwrap().run(&state)?;
Ok(())
}
};
}
#[cfg(test)] mod tui_test {
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> {
impl HasContent<TuiOut> for TestComponent {
fn content (&self) -> impl Content<TuiOut> {
Some(self.0.as_str())
}
}

View file

@ -40,16 +40,12 @@ pub trait HasColor { fn color (&self) -> ItemColor; }
pub okhsl: Okhsl<f32>,
pub rgb: Color,
}
from!(|okhsl: Okhsl<f32>|ItemColor = Self { okhsl, rgb: okhsl_to_rgb(okhsl) });
from!(ItemColor: |okhsl: Okhsl<f32>|Self { okhsl, rgb: okhsl_to_rgb(okhsl) });
from!(ItemColor: |rgb: Color|Self { rgb, okhsl: rgb_to_okhsl(rgb) });
pub fn okhsl_to_rgb (color: Okhsl<f32>) -> Color {
let Srgb { red, green, blue, .. }: Srgb<f32> = Srgb::from_color_unclamped(color);
Color::Rgb((red * 255.0) as u8, (green * 255.0) as u8, (blue * 255.0) as u8,)
}
from!(|rgb: Color|ItemColor = Self { rgb, okhsl: rgb_to_okhsl(rgb) });
pub fn rgb_to_okhsl (color: Color) -> Okhsl<f32> {
if let Color::Rgb(r, g, b) = color {
Okhsl::from_color(Srgb::new(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0))
@ -98,6 +94,8 @@ impl ItemColor {
pub darkest: ItemColor,
}
from!(ItemTheme: |base: Color| Self::from_tui_color(base));
from!(ItemTheme: |base: ItemColor|Self::from_item_color(base));
impl ItemTheme {
pub const G: [Self;256] = {
let mut builder = konst::array::ArrayBuilder::new();
@ -170,6 +168,3 @@ impl ItemTheme {
}
}
}
from!(|base: Color| ItemTheme = Self::from_tui_color(base));
from!(|base: ItemColor|ItemTheme = Self::from_item_color(base));

View file

@ -41,4 +41,4 @@ impl BigBuffer {
}
}
from!(|size:(usize, usize)| BigBuffer = Self::new(size.0, size.1));
from!(BigBuffer: |size:(usize, usize)| Self::new(size.0, size.1));