mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-07-17 15:56:57 +02:00
wip: final simplify
This commit is contained in:
parent
90ebae0f26
commit
0b9e9c0696
21 changed files with 607 additions and 544 deletions
118
src/term.rs
118
src/term.rs
|
|
@ -29,7 +29,27 @@ use ::{
|
|||
|
||||
/// Implement standard [main] entrypoint for TUI apps.
|
||||
#[macro_export] macro_rules! tui_main {
|
||||
($state:expr) => { pub fn main () -> Usually<()> { tui_run_main($state) } }
|
||||
($state:expr) => {
|
||||
pub fn main () -> Usually<()> { tui_run_main($state) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable TUI keyboard input for main state struct.
|
||||
#[macro_export] macro_rules! tui_keys {
|
||||
(|$self:ident:$State:ty,$input:ident|$body:block) => {
|
||||
impl Apply<TuiEvent, Usually<()>> for $State {
|
||||
fn apply (&mut $self, $input: &TuiEvent) -> Usually<()> $body
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Enable TUI output for state struct.
|
||||
#[macro_export] macro_rules! tui_view {
|
||||
(|$self:ident: $State:ty|$body:block) => {
|
||||
impl View<Tui> for $State {
|
||||
fn view (&$self) -> impl Draw<Tui> $body
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tui_run_main <T> (state: Arc<RwLock<T>>) -> Usually<()> where
|
||||
|
|
@ -63,15 +83,6 @@ pub fn tui_setup_panic () {
|
|||
}));
|
||||
}
|
||||
|
||||
/// Enable TUI keyboard input for main state struct.
|
||||
#[macro_export] macro_rules! tui_keys {
|
||||
(|$self:ident:$State:ty,$input:ident|$body:block) => {
|
||||
impl Apply<TuiEvent, Usually<()>> for $State {
|
||||
fn apply (&mut $self, $input: &TuiEvent) -> Usually<()> $body
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Spawn the TUI input and output threadsl.
|
||||
pub fn tui_io <
|
||||
T: View<Tui> + Apply<TuiEvent, Usually<()>> + Send + Sync + 'static,
|
||||
|
|
@ -117,15 +128,6 @@ pub fn tui_input <T: Apply<TuiEvent, Usually<()>> + Send + Sync + 'static> (
|
|||
})
|
||||
}
|
||||
|
||||
/// Enable TUI output for state struct.
|
||||
#[macro_export] macro_rules! tui_view {
|
||||
(|$self:ident: $State:ty|$body:block) => {
|
||||
impl View<Tui> for $State {
|
||||
fn view (&$self) -> impl Draw<Tui> $body
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn the TUI output thread which writes colored characters to the terminal.
|
||||
///
|
||||
/// ```
|
||||
|
|
@ -192,37 +194,31 @@ impl Tui {
|
|||
impl Screen for Tui {
|
||||
type Unit = u16;
|
||||
/// Render drawable in subarea specified by `area`
|
||||
fn show <'t, T: Draw<Self>> (
|
||||
&mut self, area: XYWH<u16>, content: T
|
||||
) -> Usually<XYWH<u16>> {
|
||||
fn show <'t, T: Draw<Self>> (&mut self, content: T) -> Perhaps<XYWH<u16>> {
|
||||
let previous_area = self.1;
|
||||
self.1 = area;
|
||||
let result_area = content.draw(self);
|
||||
self.1 = previous_area;
|
||||
result_area
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Tui { type Target = Buffer; fn deref (&self) -> &Buffer { &self.0 } }
|
||||
|
||||
impl DerefMut for Tui { fn deref_mut (&mut self) -> &mut Buffer { &mut self.0 } }
|
||||
|
||||
impl AsMut<Buffer> for Tui {
|
||||
fn as_mut (&mut self) -> &mut Buffer {
|
||||
&mut self.0
|
||||
if let Some(area) = content.layout(self.1)? {
|
||||
self.1 = area;
|
||||
if let Some(result_area) = content.draw(self)? {
|
||||
self.1 = previous_area;
|
||||
result_area
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Tui { type Target = Buffer; fn deref (&self) -> &Buffer { &self.0 } }
|
||||
impl DerefMut for Tui { fn deref_mut (&mut self) -> &mut Buffer { &mut self.0 } }
|
||||
impl AsMut<Buffer> for Tui { fn as_mut (&mut self) -> &mut Buffer { &mut self.0 } }
|
||||
impl Wide<u16> for Tui { fn w (&self) -> u16 { self.1.2 } }
|
||||
impl Tall<u16> for Tui { fn h (&self) -> u16 { self.1.3 } }
|
||||
impl HasOrigin for Tui { fn origin (&self) -> Origin { Origin::NW } }
|
||||
|
||||
impl X<u16> for Tui {
|
||||
impl Xy<u16> for Tui {
|
||||
fn x (&self) -> u16 { self.1.0 }
|
||||
fn w (&self) -> u16 { self.1.2 }
|
||||
}
|
||||
|
||||
impl Y<u16> for Tui {
|
||||
fn y (&self) -> u16 { self.1.1 }
|
||||
fn h (&self) -> u16 { self.1.3 }
|
||||
}
|
||||
|
||||
impl Tui {
|
||||
|
|
@ -520,33 +516,9 @@ pub fn catcher <T: Draw<Tui>> (result: Usually<T>) -> impl Draw<Tui> {
|
|||
})
|
||||
}
|
||||
|
||||
/// TUI buffer sized by `usize` instead of `u16`.
|
||||
#[derive(Default)] pub struct BigBuffer {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub content: Vec<Cell>
|
||||
}
|
||||
|
||||
impl_from!(BigBuffer: |size:(usize, usize)| Self::new(size.0, size.1));
|
||||
|
||||
impl_debug!(BigBuffer |self, f| { write!(f, "[BB {}x{} ({})]", self.width, self.height, self.content.len()) });
|
||||
|
||||
impl BigBuffer {
|
||||
pub fn new (width: usize, height: usize) -> Self {
|
||||
Self { width, height, content: vec![Cell::default(); width*height] }
|
||||
}
|
||||
pub fn get (&self, x: usize, y: usize) -> Option<&Cell> {
|
||||
let i = self.index_of(x, y);
|
||||
self.content.get(i)
|
||||
}
|
||||
pub fn get_mut (&mut self, x: usize, y: usize) -> Option<&mut Cell> {
|
||||
let i = self.index_of(x, y);
|
||||
self.content.get_mut(i)
|
||||
}
|
||||
pub fn index_of (&self, x: usize, y: usize) -> usize {
|
||||
y * self.width + x
|
||||
}
|
||||
}
|
||||
mod event; pub use self::event::*;
|
||||
mod keys; pub use self::keys::*;
|
||||
mod buffer; pub use self::buffer::*;
|
||||
|
||||
use self::colors::*; mod colors {
|
||||
use ratatui::prelude::Color;
|
||||
|
|
@ -572,9 +544,3 @@ use self::colors::*; mod colors {
|
|||
pub const fn tui_title_fg (f: bool) -> Color { if f { tui_ti1() } else { tui_ti2() } }
|
||||
pub const fn tui_yellow () -> Color { Color::Rgb(255,255,0) }
|
||||
}
|
||||
|
||||
#[cfg(feature = "term")] pub mod event;
|
||||
#[cfg(feature = "term")] pub use self::event::*;
|
||||
|
||||
#[cfg(feature = "term")] pub mod keys;
|
||||
#[cfg(feature = "term")] pub use self::keys::*;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue