mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-08-07 22:17:07 +02:00
more entrypoint macros
This commit is contained in:
parent
4cfe8d087c
commit
66ac2bcbb6
18 changed files with 696 additions and 663 deletions
3
examples/mode_00.rs
Normal file
3
examples/mode_00.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
//! Mode 0: Direct draw
|
||||
use ::{std::sync::{Arc, RwLock}, ratatui::style::Color, tengri::*};
|
||||
fn main () {}
|
||||
128
examples/mode_01.rs
Normal file
128
examples/mode_01.rs
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
//! Mode 01
|
||||
use ::std::sync::{Arc, RwLock};
|
||||
use ::crossterm::event::{Event::*, KeyEvent, KeyCode::*};
|
||||
use ::ratatui::style::Color;
|
||||
use ::tengri::{*, lang::*};
|
||||
tui_app!(State {
|
||||
/** Command history (undo/redo). */
|
||||
history: Vec<Action>,
|
||||
/** User-controllable value. */
|
||||
cursor: usize,
|
||||
/** 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 widget = thunk(move|to: &mut Tui|self.interpret(to, &src));
|
||||
self.size.of(south(title, north(code, widget)))
|
||||
});
|
||||
tui_ns!(self: State, to, src {
|
||||
match src.src()? {
|
||||
Some(":foo") => "foo".draw(to),
|
||||
Some(":bar") => "bar".draw(to),
|
||||
Some(":foobar") => "FOOBAR".draw(to),
|
||||
_ => todo!()
|
||||
}
|
||||
});
|
||||
#[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(), }
|
||||
}
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
const VIEWS: &'static [&'static str] = &[
|
||||
stringify! { :foobar },
|
||||
stringify! { (bg (g 8) :foobar) },
|
||||
stringify! { (fill/xy :foobar) },
|
||||
stringify! { (bsp/s :foo :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) {}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
//! Mode 02
|
||||
use ::{std::sync::{Arc, RwLock}, ratatui::style::Color, tengri::*};
|
||||
fn main () {}
|
||||
|
||||
|
|
@ -1,180 +0,0 @@
|
|||
use ::{
|
||||
std::{io::stdout, sync::{Arc, RwLock}},
|
||||
ratatui::style::Color,
|
||||
tengri::{*, lang::*},
|
||||
};
|
||||
|
||||
tui_main!(State {
|
||||
cursor: 0,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
tui_keys!(|self: State, input| {
|
||||
Ok(())
|
||||
});
|
||||
|
||||
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 widget = thunk(move|to: &mut Tui|self.interpret(to, &src));
|
||||
self.size.of(south(title, north(code, widget)))
|
||||
});
|
||||
|
||||
#[dizzle::namespace(bool)]
|
||||
#[dizzle::namespace(u16)]
|
||||
#[dizzle::namespace(Option<u16>)]
|
||||
#[dizzle::namespace(Color)]
|
||||
#[derive(Debug, Default)]
|
||||
struct State {
|
||||
/** Command history (undo/redo). */
|
||||
history: Vec<Action>,
|
||||
/** User-controllable value. */
|
||||
cursor: usize,
|
||||
/** Rendered window size. */
|
||||
size: Sizer,
|
||||
}
|
||||
|
||||
impl Interpret<Tui, Option<XYWH<u16>>> for State {
|
||||
fn interpret_word <'a> (&'a self, to: &mut Tui, lang: &'a impl Symbol) -> Drawn<u16> {
|
||||
match lang.src()? {
|
||||
Some(":hello") => "hello".draw(to),
|
||||
Some(":world") => "world".draw(to),
|
||||
Some(":hello-world") => "Hello World!".draw(to),
|
||||
_ => todo!()
|
||||
}
|
||||
}
|
||||
fn interpret_expr <'a> (&'a self, to: &mut Tui, lang: &'a impl Expression) -> Drawn<u16> {
|
||||
Ok(Some(if let Some(area) = eval_view(self, to, lang)? {
|
||||
area
|
||||
} else if let Some(area) = eval_view_tui(self, to, lang)? {
|
||||
area
|
||||
} else {
|
||||
return Err(format!("App::interpret_expr: unexpected: {lang:?}").into())
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)] enum Action {
|
||||
/** Increment cursor */
|
||||
Next,
|
||||
/** Decrement cursor */
|
||||
Prev
|
||||
}
|
||||
|
||||
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] = &[
|
||||
|
||||
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)))))))
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
//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) {
|
||||
//}
|
||||
Loading…
Add table
Add a link
Reference in a new issue