mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-04-03 13:30:44 +02:00
This commit is contained in:
parent
c0d8c5f1bb
commit
a06ea2ac13
5 changed files with 67 additions and 58 deletions
26
src/eval.rs
26
src/eval.rs
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::*;
|
use crate::{*, term::*, draw::*, ratatui::prelude::*};
|
||||||
|
use dizzle::*;
|
||||||
|
|
||||||
/// Interpret layout operation.
|
/// Interpret layout operation.
|
||||||
///
|
///
|
||||||
|
|
@ -165,7 +166,7 @@ use crate::*;
|
||||||
/// impl Understand<Tui, ()> for State {}
|
/// impl Understand<Tui, ()> for State {}
|
||||||
/// # fn main () -> tengri::Usually<()> {
|
/// # fn main () -> tengri::Usually<()> {
|
||||||
/// let state = State;
|
/// let state = State;
|
||||||
/// let mut out = TuiOut::default();
|
/// let mut out = Tui::default();
|
||||||
/// tengri::eval_view_tui(&state, &mut out, "")?;
|
/// tengri::eval_view_tui(&state, &mut out, "")?;
|
||||||
/// tengri::eval_view_tui(&state, &mut out, "text Hello world!")?;
|
/// tengri::eval_view_tui(&state, &mut out, "text Hello world!")?;
|
||||||
/// tengri::eval_view_tui(&state, &mut out, "fg (g 0) (text Hello world!)")?;
|
/// tengri::eval_view_tui(&state, &mut out, "fg (g 0) (text Hello world!)")?;
|
||||||
|
|
@ -174,9 +175,9 @@ use crate::*;
|
||||||
/// # Ok(()) }
|
/// # Ok(()) }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn eval_view_tui <'a, S> (
|
pub fn eval_view_tui <'a, S> (
|
||||||
state: &S, output: &mut Buffer, expr: impl Expression + 'a
|
state: &S, output: &mut Tui, expr: impl Expression + 'a
|
||||||
) -> Usually<bool> where
|
) -> Usually<bool> where
|
||||||
S: Understand<TuiOut, ()>
|
S: Understand<Tui, ()>
|
||||||
+ for<'b>Namespace<'b, bool>
|
+ for<'b>Namespace<'b, bool>
|
||||||
+ for<'b>Namespace<'b, u16>
|
+ for<'b>Namespace<'b, u16>
|
||||||
+ for<'b>Namespace<'b, Color>
|
+ for<'b>Namespace<'b, Color>
|
||||||
|
|
@ -199,18 +200,21 @@ pub fn eval_view_tui <'a, S> (
|
||||||
Some("fg") => {
|
Some("fg") => {
|
||||||
let arg0 = arg0?.expect("fg: expected arg 0 (color)");
|
let arg0 = arg0?.expect("fg: expected arg 0 (color)");
|
||||||
let color = Namespace::namespace(state, arg0)?.unwrap_or_else(||panic!("fg: {arg0:?}: not a color"));
|
let color = Namespace::namespace(state, arg0)?.unwrap_or_else(||panic!("fg: {arg0:?}: not a color"));
|
||||||
let thunk = Thunk::new(move|output: &mut Buffer|state.understand(output, &arg1).unwrap());
|
output.place(&fg(color, thunk(move|output: &mut Tui|{
|
||||||
output.place(&TuiOut::fg(color, thunk))
|
state.understand(output, &arg1)?;
|
||||||
|
// FIXME?: don't max out the used area?
|
||||||
|
Ok(output.area().into())
|
||||||
|
})))
|
||||||
},
|
},
|
||||||
|
|
||||||
Some("bg") => {
|
Some("bg") => {
|
||||||
//panic!("expr: {expr:?}\nhead: {head:?}\nfrags: {frags:?}\nargs: {args:?}\narg0: {arg0:?}\ntail0: {tail0:?}\narg1: {arg1:?}\ntail1: {tail1:?}\narg2: {arg2:?}");
|
|
||||||
//panic!("head: {head:?}\narg0: {arg0:?}\narg1: {arg1:?}\narg2: {arg2:?}");;
|
|
||||||
//panic!("head: {head:?}\narg0: {arg0:?}\narg1: {arg1:?}\narg2: {arg2:?}");
|
|
||||||
let arg0 = arg0?.expect("bg: expected arg 0 (color)");
|
let arg0 = arg0?.expect("bg: expected arg 0 (color)");
|
||||||
let color = Namespace::namespace(state, arg0)?.unwrap_or_else(||panic!("bg: {arg0:?}: not a color"));
|
let color = Namespace::namespace(state, arg0)?.unwrap_or_else(||panic!("bg: {arg0:?}: not a color"));
|
||||||
let thunk = Thunk::new(move|output: &mut Buffer|state.understand(output, &arg1).unwrap());
|
output.place(&bg(color, thunk(move|output: &mut Tui|{
|
||||||
output.place(&TuiOut::bg(color, thunk))
|
state.understand(output, &arg1)?;
|
||||||
|
// FIXME?: don't max out the used area?
|
||||||
|
Ok(output.area().into())
|
||||||
|
})))
|
||||||
},
|
},
|
||||||
|
|
||||||
_ => return Ok(false)
|
_ => return Ok(false)
|
||||||
|
|
|
||||||
43
src/lib.rs
43
src/lib.rs
|
|
@ -25,10 +25,10 @@ pub(crate) use ::{
|
||||||
|
|
||||||
#[cfg(feature = "lang")] pub extern crate dizzle as lang;
|
#[cfg(feature = "lang")] pub extern crate dizzle as lang;
|
||||||
#[cfg(feature = "lang")] pub use ::dizzle::{self, Usually, Perhaps, impl_default};
|
#[cfg(feature = "lang")] pub use ::dizzle::{self, Usually, Perhaps, impl_default};
|
||||||
|
#[cfg(feature = "lang")] pub mod eval;
|
||||||
|
|
||||||
#[cfg(feature = "time")] pub mod time;
|
#[cfg(feature = "time")] pub mod time;
|
||||||
|
|
||||||
#[cfg(feature = "play")] pub mod play;
|
|
||||||
#[cfg(feature = "play")] pub mod exit;
|
#[cfg(feature = "play")] pub mod exit;
|
||||||
#[cfg(feature = "play")] pub mod task;
|
#[cfg(feature = "play")] pub mod task;
|
||||||
|
|
||||||
|
|
@ -39,6 +39,7 @@ pub(crate) use ::{
|
||||||
#[cfg(feature = "draw")] pub mod draw;
|
#[cfg(feature = "draw")] pub mod draw;
|
||||||
#[cfg(feature = "draw")] pub mod space;
|
#[cfg(feature = "draw")] pub mod space;
|
||||||
#[cfg(feature = "draw")] pub mod color;
|
#[cfg(feature = "draw")] pub mod color;
|
||||||
|
|
||||||
#[cfg(feature = "text")] pub mod text;
|
#[cfg(feature = "text")] pub mod text;
|
||||||
#[cfg(feature = "term")] pub mod term;
|
#[cfg(feature = "term")] pub mod term;
|
||||||
#[cfg(feature = "term")] pub mod keys;
|
#[cfg(feature = "term")] pub mod keys;
|
||||||
|
|
@ -75,3 +76,43 @@ pub(crate) use ::{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// Define an enum containing commands, and implement [Command] trait for over given `State`.
|
||||||
|
#[macro_export] macro_rules! def_command (
|
||||||
|
($Command:ident: |$state:ident: $State:ty| {
|
||||||
|
// FIXME: support attrs (docstrings)
|
||||||
|
$($Variant:ident$({$($arg:ident:$Arg:ty),+ $(,)?})?=>$body:expr),* $(,)?
|
||||||
|
})=>{
|
||||||
|
#[derive(Debug)] pub enum $Command {
|
||||||
|
// FIXME: support attrs (docstrings)
|
||||||
|
$($Variant $({ $($arg: $Arg),* })?),*
|
||||||
|
}
|
||||||
|
impl ::tengri::dizzle::Act<$State> for $Command {
|
||||||
|
fn act (&self, $state: &mut $State) -> Perhaps<Self> {
|
||||||
|
match self {
|
||||||
|
$(Self::$Variant $({ $($arg),* })? => $body,)*
|
||||||
|
_ => unimplemented!("Act<{}>: {self:?}", stringify!($State)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Implement [Handle] for given `State` and `handler`.
|
||||||
|
#[macro_export] macro_rules! impl_handle {
|
||||||
|
//(|$self:ident:$State:ty,$input:ident|$handler:expr) => {
|
||||||
|
//impl<E: Engine> ::tengri::Handle<E> for $State {
|
||||||
|
//fn handle (&mut $self, $input: &E) -> Perhaps<E::Handled> {
|
||||||
|
//$handler
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
//};
|
||||||
|
($E:ty: |$self:ident:$State:ty,$input:ident|$handler:expr) => {
|
||||||
|
//impl ::tengri::Handle<$E> for $State {
|
||||||
|
//fn handle (&mut $self, $input: &$E) ->
|
||||||
|
//Perhaps<<$E as ::tengri::Input>::Handled>
|
||||||
|
//{
|
||||||
|
//$handler
|
||||||
|
//}
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
42
src/play.rs
42
src/play.rs
|
|
@ -1,42 +0,0 @@
|
||||||
use crate::{*, time::*, lang::*};
|
|
||||||
use ::std::{thread::JoinHandle, time::Duration};
|
|
||||||
|
|
||||||
/// Define an enum containing commands, and implement [Command] trait for over given `State`.
|
|
||||||
#[macro_export] macro_rules! def_command (
|
|
||||||
($Command:ident: |$state:ident: $State:ty| {
|
|
||||||
// FIXME: support attrs (docstrings)
|
|
||||||
$($Variant:ident$({$($arg:ident:$Arg:ty),+ $(,)?})?=>$body:expr),* $(,)?
|
|
||||||
})=>{
|
|
||||||
#[derive(Debug)] pub enum $Command {
|
|
||||||
// FIXME: support attrs (docstrings)
|
|
||||||
$($Variant $({ $($arg: $Arg),* })?),*
|
|
||||||
}
|
|
||||||
impl ::tengri::dizzle::Act<$State> for $Command {
|
|
||||||
fn act (&self, $state: &mut $State) -> Perhaps<Self> {
|
|
||||||
match self {
|
|
||||||
$(Self::$Variant $({ $($arg),* })? => $body,)*
|
|
||||||
_ => unimplemented!("Act<{}>: {self:?}", stringify!($State)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/// Implement [Handle] for given `State` and `handler`.
|
|
||||||
#[macro_export] macro_rules! impl_handle {
|
|
||||||
(|$self:ident:$State:ty,$input:ident|$handler:expr) => {
|
|
||||||
impl<E: Engine> ::tengri::Handle<E> for $State {
|
|
||||||
fn handle (&mut $self, $input: &E) -> Perhaps<E::Handled> {
|
|
||||||
$handler
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
($E:ty: |$self:ident:$State:ty,$input:ident|$handler:expr) => {
|
|
||||||
impl ::tengri::Handle<$E> for $State {
|
|
||||||
fn handle (&mut $self, $input: &$E) ->
|
|
||||||
Perhaps<<$E as ::tengri::Input>::Handled>
|
|
||||||
{
|
|
||||||
$handler
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
12
src/space.rs
12
src/space.rs
|
|
@ -77,6 +77,15 @@ impl<T: Screen, U: Draw<T>> Draw<T> for Anchor<U> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn origin_c <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
||||||
|
Anchor(Origin::C, a)
|
||||||
|
}
|
||||||
|
pub const fn origin_x <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
||||||
|
Anchor(Origin::X, a)
|
||||||
|
}
|
||||||
|
pub const fn origin_y <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
||||||
|
Anchor(Origin::Y, a)
|
||||||
|
}
|
||||||
pub const fn origin_nw <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
pub const fn origin_nw <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
||||||
Anchor(Origin::NW, a)
|
Anchor(Origin::NW, a)
|
||||||
}
|
}
|
||||||
|
|
@ -89,9 +98,6 @@ pub const fn origin_ne <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
||||||
pub const fn origin_w <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
pub const fn origin_w <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
||||||
Anchor(Origin::W, a)
|
Anchor(Origin::W, a)
|
||||||
}
|
}
|
||||||
pub const fn origin_c <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
|
||||||
Anchor(Origin::C, a)
|
|
||||||
}
|
|
||||||
pub const fn origin_e <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
pub const fn origin_e <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
|
||||||
Anchor(Origin::E, a)
|
Anchor(Origin::E, a)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{*, lang::*, play::*, draw::*, space::{*, Split::*}, color::*, text::*, task::*};
|
use crate::{*, lang::*, draw::*, space::{*, Split::*}, color::*, text::*, task::*};
|
||||||
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
|
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
|
||||||
use rand::distributions::uniform::UniformSampler;
|
use rand::distributions::uniform::UniformSampler;
|
||||||
use ::{
|
use ::{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue