mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-04-25 13:40:43 +02:00
re-add tui_main, fixing examples
This commit is contained in:
parent
a93fe92a59
commit
6c382e2627
7 changed files with 89 additions and 44 deletions
29
src/draw.rs
29
src/draw.rs
|
|
@ -1,17 +1,5 @@
|
|||
use crate::{*, lang::*, color::*, space::*};
|
||||
|
||||
/// Emit a [Draw]able.
|
||||
///
|
||||
/// Speculative. How to avoid conflicts with [Draw] proper?
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Drawable that supports dynamic dispatch.
|
||||
///
|
||||
/// Drawables are composable, e.g. the [when] and [either] conditionals
|
||||
|
|
@ -42,18 +30,24 @@ impl<T: Screen, V: View<T>> Draw<T> for &V {
|
|||
pub trait Draw<T: Screen> {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>>;
|
||||
}
|
||||
/// Empty draw
|
||||
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 &D {
|
||||
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
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())
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a [Draw]able.
|
||||
///
|
||||
/// Speculative. How to avoid conflicts with [Draw] proper?
|
||||
pub trait View<T: Screen> {
|
||||
fn view (&self) -> impl Draw<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)
|
||||
}
|
||||
|
|
@ -63,7 +57,6 @@ pub struct Thunk<T: Screen, F: FnOnce(&mut T)->Usually<XYWH<T::Unit>>>(
|
|||
pub F,
|
||||
std::marker::PhantomData<T>
|
||||
);
|
||||
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -8,3 +8,10 @@ impl Exit {
|
|||
run(Self(Arc::new(AtomicBool::new(false))))
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<Arc<AtomicBool>> for Exit {
|
||||
fn as_ref (&self) -> &Arc<AtomicBool> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ use crate::task::Task;
|
|||
|
||||
use ::std::sync::{Arc, RwLock, atomic::{AtomicBool, Ordering::*}};
|
||||
use ::std::time::Duration;
|
||||
use ::dizzle::{Usually, Do, impl_from};
|
||||
use ::dizzle::{Usually, Apply, impl_from};
|
||||
use ::crossterm::event::{
|
||||
read, Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, KeyEventState
|
||||
};
|
||||
|
||||
/// Spawn the TUI input thread which reads keys from the terminal.
|
||||
pub fn tui_input <T: Do<TuiEvent, Usually<T>> + Send + Sync + 'static> (
|
||||
pub fn tui_input <T: Apply<TuiEvent, Usually<T>> + Send + Sync + 'static> (
|
||||
exited: &Arc<AtomicBool>, state: &Arc<RwLock<T>>, poll: Duration
|
||||
) -> Result<Task, std::io::Error> {
|
||||
let exited = exited.clone();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::{*, draw::*};
|
||||
#[cfg(test)] use proptest_derive::Arbitrary;
|
||||
|
||||
/// Point with size.
|
||||
///
|
||||
|
|
|
|||
17
src/term.rs
17
src/term.rs
|
|
@ -21,6 +21,23 @@ use ::{
|
|||
event::{poll, read, Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, KeyEventState},
|
||||
}
|
||||
};
|
||||
#[macro_export] macro_rules! tui_main {
|
||||
($state:expr) => {
|
||||
pub fn main () -> Usually<()> {
|
||||
tengri::exit::Exit::run(|exit|{
|
||||
let state = Arc::new(RwLock::new($state));
|
||||
Ok((
|
||||
::tengri::keys::tui_input(
|
||||
exit.as_ref(), &state, std::time::Duration::from_millis(100)
|
||||
)?,
|
||||
::tengri::term::tui_output(
|
||||
stdout(), exit.as_ref(), &state, std::time::Duration::from_millis(10)
|
||||
)?
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct Tui(pub Buffer, pub XYWH<u16>);
|
||||
impl Screen for Tui { type Unit = u16; }
|
||||
impl Deref for Tui { type Target = Buffer; fn deref (&self) -> &Buffer { &self.0 } }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue