mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-04-03 21:40:44 +02:00
Compare commits
No commits in common. "0af46acf3634ec2e2a44330adcf070868367589f" and "bea88ac58d404f9af65286663bbbf18205bde14b" have entirely different histories.
0af46acf36
...
bea88ac58d
10 changed files with 85 additions and 135 deletions
31
src/draw.rs
31
src/draw.rs
|
|
@ -1,12 +1,6 @@
|
|||
use crate::{*, lang::*, color::*, space::*};
|
||||
|
||||
/// Drawable that supports dynamic dispatch.
|
||||
///
|
||||
/// Drawables are composable, e.g. the [when] and [either] conditionals
|
||||
/// or the layout constraints.
|
||||
///
|
||||
/// Drawables are consumable, i.e. the [Draw::draw] method receives an
|
||||
/// owned `self` and does not return it, consuming the drawable.
|
||||
///
|
||||
/// ```
|
||||
/// use tengri::draw::*;
|
||||
|
|
@ -26,29 +20,25 @@ use crate::{*, lang::*, color::*, space::*};
|
|||
pub trait Draw<T: Screen> {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>>;
|
||||
}
|
||||
|
||||
impl<T: Screen> Draw<T> for () {
|
||||
fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Screen, D: Draw<T>> Draw<T> for Option<D> {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
Ok(self.map(|draw|draw.draw(to)).transpose()?.unwrap_or_default())
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn thunk <T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>> (draw: F) -> Thunk<T, F> {
|
||||
Thunk(draw, std::marker::PhantomData)
|
||||
}
|
||||
|
||||
/// Because we can't implement [Draw] for `F: FnOnce...` without conflicts.
|
||||
pub struct Thunk<T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>>(
|
||||
pub F,
|
||||
std::marker::PhantomData<T>
|
||||
);
|
||||
|
||||
pub const fn thunk <T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>> (draw: F) -> Thunk<T, F> {
|
||||
Thunk(draw, std::marker::PhantomData)
|
||||
}
|
||||
impl<T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>> Draw<T> for Thunk<T, F> {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
(self.0)(to)
|
||||
|
|
@ -86,8 +76,8 @@ pub const fn either <T: Screen> (condition: bool, a: impl Draw<T>, b: impl Draw<
|
|||
///
|
||||
/// impl Screen for TestOut {
|
||||
/// type Unit = u16;
|
||||
/// fn place_at <T: Draw<Self> + ?Sized> (&mut self, area: impl TwoD<u16>, _: &T) {
|
||||
/// println!("placed: {area:?}");
|
||||
/// fn place <T: Draw<Self> + ?Sized> (&mut self, area: impl TwoD<u16>, _: &T) {
|
||||
/// println!("place: {area:?}");
|
||||
/// ()
|
||||
/// }
|
||||
/// }
|
||||
|
|
@ -111,14 +101,3 @@ pub trait Screen: Space<Self::Unit> + Send + Sync + Sized {
|
|||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a [Draw]able.
|
||||
pub trait View<T: Screen> {
|
||||
fn view (&self) -> impl Draw<T>;
|
||||
}
|
||||
|
||||
impl<T: Screen, V: View<T>> Draw<T> for &V {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
self.view().draw(to)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
src/eval.rs
26
src/eval.rs
|
|
@ -1,5 +1,4 @@
|
|||
use crate::{*, term::*, draw::*, ratatui::prelude::*};
|
||||
use dizzle::*;
|
||||
use crate::*;
|
||||
|
||||
/// Interpret layout operation.
|
||||
///
|
||||
|
|
@ -166,7 +165,7 @@ use dizzle::*;
|
|||
/// impl Understand<Tui, ()> for State {}
|
||||
/// # fn main () -> tengri::Usually<()> {
|
||||
/// let state = State;
|
||||
/// let mut out = Tui::default();
|
||||
/// let mut out = TuiOut::default();
|
||||
/// tengri::eval_view_tui(&state, &mut out, "")?;
|
||||
/// tengri::eval_view_tui(&state, &mut out, "text Hello world!")?;
|
||||
/// tengri::eval_view_tui(&state, &mut out, "fg (g 0) (text Hello world!)")?;
|
||||
|
|
@ -175,9 +174,9 @@ use dizzle::*;
|
|||
/// # Ok(()) }
|
||||
/// ```
|
||||
pub fn eval_view_tui <'a, S> (
|
||||
state: &S, output: &mut Tui, expr: impl Expression + 'a
|
||||
state: &S, output: &mut Buffer, expr: impl Expression + 'a
|
||||
) -> Usually<bool> where
|
||||
S: Understand<Tui, ()>
|
||||
S: Understand<TuiOut, ()>
|
||||
+ for<'b>Namespace<'b, bool>
|
||||
+ for<'b>Namespace<'b, u16>
|
||||
+ for<'b>Namespace<'b, Color>
|
||||
|
|
@ -200,21 +199,18 @@ pub fn eval_view_tui <'a, S> (
|
|||
Some("fg") => {
|
||||
let arg0 = arg0?.expect("fg: expected arg 0 (color)");
|
||||
let color = Namespace::namespace(state, arg0)?.unwrap_or_else(||panic!("fg: {arg0:?}: not a color"));
|
||||
output.place(&fg(color, thunk(move|output: &mut Tui|{
|
||||
state.understand(output, &arg1)?;
|
||||
// FIXME?: don't max out the used area?
|
||||
Ok(output.area().into())
|
||||
})))
|
||||
let thunk = Thunk::new(move|output: &mut Buffer|state.understand(output, &arg1).unwrap());
|
||||
output.place(&TuiOut::fg(color, thunk))
|
||||
},
|
||||
|
||||
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 color = Namespace::namespace(state, arg0)?.unwrap_or_else(||panic!("bg: {arg0:?}: not a color"));
|
||||
output.place(&bg(color, thunk(move|output: &mut Tui|{
|
||||
state.understand(output, &arg1)?;
|
||||
// FIXME?: don't max out the used area?
|
||||
Ok(output.area().into())
|
||||
})))
|
||||
let thunk = Thunk::new(move|output: &mut Buffer|state.understand(output, &arg1).unwrap());
|
||||
output.place(&TuiOut::bg(color, thunk))
|
||||
},
|
||||
|
||||
_ => return Ok(false)
|
||||
|
|
|
|||
10
src/exit.rs
10
src/exit.rs
|
|
@ -1,10 +0,0 @@
|
|||
use std::sync::{Arc, atomic::AtomicBool};
|
||||
use crate::Usually;
|
||||
|
||||
#[derive(Clone)] pub struct Exit(Arc<AtomicBool>);
|
||||
|
||||
impl Exit {
|
||||
pub fn run <T> (run: impl Fn(Self)->Usually<T>) -> Usually<T> {
|
||||
run(Self(Arc::new(AtomicBool::new(false))))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::task::Task;
|
||||
use crate::play::Thread;
|
||||
|
||||
use ::std::sync::{Arc, RwLock, atomic::{AtomicBool, Ordering::*}};
|
||||
use ::std::time::Duration;
|
||||
|
|
@ -10,10 +10,10 @@ use ::crossterm::event::{
|
|||
/// Spawn the TUI input thread which reads keys from the terminal.
|
||||
pub fn tui_input <T: Do<TuiEvent, Usually<T>> + Send + Sync + 'static> (
|
||||
exited: &Arc<AtomicBool>, state: &Arc<RwLock<T>>, poll: Duration
|
||||
) -> Result<Task, std::io::Error> {
|
||||
) -> Result<Thread, std::io::Error> {
|
||||
let exited = exited.clone();
|
||||
let state = state.clone();
|
||||
Task::new_poll(exited.clone(), poll, move |_| {
|
||||
Thread::new_poll(exited.clone(), poll, move |_| {
|
||||
let event = read().unwrap();
|
||||
match event {
|
||||
|
||||
|
|
|
|||
|
|
@ -25,14 +25,9 @@ pub(crate) use ::{
|
|||
|
||||
#[cfg(feature = "lang")] pub extern crate dizzle as lang;
|
||||
#[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 = "play")] pub mod play;
|
||||
#[cfg(feature = "play")] pub mod exit;
|
||||
#[cfg(feature = "play")] pub mod task;
|
||||
|
||||
#[cfg(feature = "sing")] pub extern crate jack;
|
||||
#[cfg(feature = "sing")] pub mod sing;
|
||||
#[cfg(feature = "sing")] pub use ::jack::{*, contrib::{*, ClosureProcessHandler}};
|
||||
|
|
|
|||
62
src/play.rs
62
src/play.rs
|
|
@ -1,5 +1,65 @@
|
|||
use crate::{*, time::*, lang::*};
|
||||
use ::std::{thread::JoinHandle, time::Duration};
|
||||
#[cfg(feature = "term")] use ::crossterm::event::poll;
|
||||
|
||||
#[derive(Clone)] pub struct Exit(Arc<AtomicBool>);
|
||||
|
||||
impl Exit {
|
||||
pub fn run <T> (run: impl Fn(Self)->Usually<T>) -> Usually<T> {
|
||||
run(Self(Arc::new(AtomicBool::new(false))))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)] pub struct Thread {
|
||||
/// Exit flag.
|
||||
pub exit: Arc<AtomicBool>,
|
||||
/// Performance counter.
|
||||
pub perf: Arc<PerfModel>,
|
||||
/// Use this to wait for the thread to finish.
|
||||
pub join: JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl Thread {
|
||||
/// Spawn a TUI thread that runs `callt least one, then repeats until `exit`.
|
||||
pub fn new <F> (exit: Arc<AtomicBool>, mut call: F) -> Result<Self, std::io::Error>
|
||||
where F: FnMut(&PerfModel)->() + Send + Sync + 'static
|
||||
{
|
||||
let perf = Arc::new(PerfModel::default());
|
||||
Ok(Self {
|
||||
exit: exit.clone(),
|
||||
perf: perf.clone(),
|
||||
join: std::thread::Builder::new().name("tengri tui output".into()).spawn(move || {
|
||||
while !exit.fetch_and(true, Relaxed) {
|
||||
let _ = perf.cycle(&mut call);
|
||||
}
|
||||
})?.into()
|
||||
})
|
||||
}
|
||||
|
||||
/// Spawn a thread that runs `call` least one, then repeats
|
||||
/// until `exit`, sleeping for `time` msec after every iteration.
|
||||
pub fn new_sleep <F> (
|
||||
exit: Arc<AtomicBool>, time: Duration, mut call: F
|
||||
) -> Result<Self, std::io::Error>
|
||||
where F: FnMut(&PerfModel)->() + Send + Sync + 'static
|
||||
{
|
||||
Self::new(exit, move |perf| { let _ = call(perf); std::thread::sleep(time); })
|
||||
}
|
||||
|
||||
/// Spawn a thread that uses [crossterm::event::poll]
|
||||
/// to run `call` every `time` msec.
|
||||
#[cfg(feature = "term")] pub fn new_poll <F> (
|
||||
exit: Arc<AtomicBool>, time: Duration, mut call: F
|
||||
) -> Result<Self, std::io::Error>
|
||||
where F: FnMut(&PerfModel)->() + Send + Sync + 'static
|
||||
{
|
||||
Self::new(exit, move |perf| { if poll(time).is_ok() { let _ = call(perf); } })
|
||||
}
|
||||
|
||||
pub fn join (self) -> Result<(), Box<dyn std::any::Any + Send>> {
|
||||
self.join.join()
|
||||
}
|
||||
}
|
||||
|
||||
/// Define an enum containing commands, and implement [Command] trait for over given `State`.
|
||||
#[macro_export] macro_rules! def_command (
|
||||
|
|
@ -22,7 +82,7 @@ use ::std::{thread::JoinHandle, time::Duration};
|
|||
});
|
||||
|
||||
/// Implement [Handle] for given `State` and `handler`.
|
||||
#[macro_export] macro_rules! impl_handle {
|
||||
#[macro_export] macro_rules! 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> {
|
||||
|
|
|
|||
|
|
@ -11,11 +11,6 @@ use crate::{*, draw::*};
|
|||
///
|
||||
#[cfg_attr(test, derive(Arbitrary))] #[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub struct XYWH<N: Coord>(pub N, pub N, pub N, pub N);
|
||||
impl From<&ratatui::prelude::Rect> for XYWH<u16> {
|
||||
fn from (rect: &ratatui::prelude::Rect) -> Self {
|
||||
Self(rect.x, rect.y, rect.width, rect.height)
|
||||
}
|
||||
}
|
||||
impl<N: Coord> X<N> for XYWH<N> { fn x (&self) -> N { self.0 } fn w (&self) -> N { self.2 } }
|
||||
impl<N: Coord> Y<N> for XYWH<N> { fn y (&self) -> N { self.0 } fn h (&self) -> N { self.2 } }
|
||||
impl<N: Coord> XYWH<N> {
|
||||
|
|
|
|||
65
src/task.rs
65
src/task.rs
|
|
@ -1,65 +0,0 @@
|
|||
use std::{
|
||||
time::Duration,
|
||||
sync::{Arc, atomic::{AtomicBool, Ordering::*}},
|
||||
thread::{Builder, JoinHandle, sleep},
|
||||
};
|
||||
#[cfg(feature = "term")] use ::crossterm::event::poll;
|
||||
use crate::time::PerfModel;
|
||||
|
||||
#[derive(Debug)] pub struct Task {
|
||||
/// Exit flag.
|
||||
pub exit: Arc<AtomicBool>,
|
||||
/// Performance counter.
|
||||
pub perf: Arc<PerfModel>,
|
||||
/// Use this to wait for the thread to finish.
|
||||
pub join: JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl Task {
|
||||
/// Spawn a TUI thread that runs `callt least one, then repeats until `exit`.
|
||||
pub fn new <F> (exit: Arc<AtomicBool>, mut call: F) -> Result<Self, std::io::Error>
|
||||
where F: FnMut(&PerfModel)->() + Send + Sync + 'static
|
||||
{
|
||||
let perf = Arc::new(PerfModel::default());
|
||||
Ok(Self {
|
||||
exit: exit.clone(),
|
||||
perf: perf.clone(),
|
||||
join: Builder::new().name("tengri tui output".into()).spawn(move || {
|
||||
while !exit.fetch_and(true, Relaxed) {
|
||||
let _ = perf.cycle(&mut call);
|
||||
}
|
||||
})?.into()
|
||||
})
|
||||
}
|
||||
|
||||
/// Spawn a thread that runs `call` least one, then repeats
|
||||
/// until `exit`, sleeping for `time` msec after every iteration.
|
||||
pub fn new_sleep <F> (
|
||||
exit: Arc<AtomicBool>, time: Duration, mut call: F
|
||||
) -> Result<Self, std::io::Error>
|
||||
where F: FnMut(&PerfModel)->() + Send + Sync + 'static
|
||||
{
|
||||
Self::new(exit, move |perf| {
|
||||
let _ = call(perf);
|
||||
sleep(time);
|
||||
})
|
||||
}
|
||||
|
||||
/// Spawn a thread that uses [crossterm::event::poll]
|
||||
/// to run `call` every `time` msec.
|
||||
#[cfg(feature = "term")] pub fn new_poll <F> (
|
||||
exit: Arc<AtomicBool>, time: Duration, mut call: F
|
||||
) -> Result<Self, std::io::Error>
|
||||
where F: FnMut(&PerfModel)->() + Send + Sync + 'static
|
||||
{
|
||||
Self::new(exit, move |perf| {
|
||||
if poll(time).is_ok() {
|
||||
let _ = call(perf);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn join (self) -> Result<(), Box<dyn std::any::Any + Send>> {
|
||||
self.join.join()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{*, lang::*, play::*, draw::*, space::{*, Split::*}, color::*, text::*, task::*};
|
||||
use crate::{*, lang::*, play::*, draw::*, space::{*, Split::*}, color::*, text::*};
|
||||
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
|
||||
use rand::distributions::uniform::UniformSampler;
|
||||
use ::{
|
||||
|
|
@ -509,14 +509,14 @@ pub fn tui_output <W: Write + Send + Sync + 'static, T: Draw<Tui> + Send + Sync
|
|||
exited: &Arc<AtomicBool>,
|
||||
state: &Arc<RwLock<T>>,
|
||||
sleep: Duration
|
||||
) -> Usually<Task> {
|
||||
) -> Usually<Thread> {
|
||||
let state = state.clone();
|
||||
tui_setup()?;
|
||||
let mut backend = CrosstermBackend::new(output);
|
||||
let Size { width, height } = backend.size().expect("get size failed");
|
||||
let mut buffer_a = Buffer::empty(Rect { x: 0, y: 0, width, height });
|
||||
let mut buffer_b = Buffer::empty(Rect { x: 0, y: 0, width, height });
|
||||
Ok(Task::new_sleep(exited.clone(), sleep, move |perf| {
|
||||
Ok(Thread::new_sleep(exited.clone(), sleep, move |perf| {
|
||||
let Size { width, height } = backend.size().expect("get size failed");
|
||||
if let Ok(state) = state.try_read() {
|
||||
tui_resize(&mut backend, &mut buffer_a, Rect { x: 0, y: 0, width, height });
|
||||
|
|
|
|||
|
|
@ -17,12 +17,12 @@ pub(crate) use ::unicode_width::*;
|
|||
self.as_str().draw(to)
|
||||
}
|
||||
}
|
||||
impl Draw<Tui> for std::sync::Arc<str> {
|
||||
impl Draw<Tui> for &std::sync::Arc<str> {
|
||||
fn draw (self, to: &mut Tui) -> Usually<XYWH<u16>> {
|
||||
self.as_ref().draw(to)
|
||||
}
|
||||
}
|
||||
impl Draw<Tui> for &std::sync::Arc<str> {
|
||||
impl Draw<Tui> for std::sync::Arc<str> {
|
||||
fn draw (self, to: &mut Tui) -> Usually<XYWH<u16>> {
|
||||
self.as_ref().draw(to)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue