mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-07-17 15:56:57 +02:00
and exits, weirdly. wonder what's up here
This commit is contained in:
parent
b0fb9f013d
commit
b44dc02f33
9 changed files with 110 additions and 80 deletions
92
src/term.rs
92
src/term.rs
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{*, lang::*, draw::*, space::{*, Split::*}, color::*, text::*, task::*};
|
||||
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
|
||||
use rand::distributions::uniform::UniformSampler;
|
||||
//use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
|
||||
//use rand::distributions::uniform::UniformSampler;
|
||||
use ::{
|
||||
std::{
|
||||
io::{stdout, Write},
|
||||
|
|
@ -18,26 +18,72 @@ use ::{
|
|||
crossterm::{
|
||||
ExecutableCommand,
|
||||
terminal::{EnterAlternateScreen, LeaveAlternateScreen, enable_raw_mode, disable_raw_mode},
|
||||
event::{poll, read, Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, KeyEventState},
|
||||
//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)
|
||||
)?
|
||||
))
|
||||
let input = ::tengri::keys::tui_input(
|
||||
exit.as_ref(),
|
||||
&state,
|
||||
::std::time::Duration::from_millis(100)
|
||||
)?;
|
||||
let output = ::tengri::term::tui_output(
|
||||
stdout(),
|
||||
exit.as_ref(),
|
||||
&state,
|
||||
::std::time::Duration::from_millis(10)
|
||||
)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! tui_keys {
|
||||
(|$self:ident:$State:ty,$input:ident|$body:block) => {
|
||||
impl Apply<TuiEvent, Usually<Self>> for $State {
|
||||
fn apply (&mut $self, $input: &TuiEvent) -> Usually<Self> $body
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[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.
|
||||
pub fn tui_output <W: Write + Send + Sync + 'static, T: View<Tui> + Send + Sync + 'static> (
|
||||
output: W,
|
||||
exited: &Arc<AtomicBool>,
|
||||
state: &Arc<RwLock<T>>,
|
||||
sleep: Duration
|
||||
) -> Usually<Task> {
|
||||
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| {
|
||||
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 });
|
||||
tui_redraw(&mut backend, &mut buffer_a, &mut buffer_b);
|
||||
}
|
||||
let timer = format!("{:>3.3}ms", perf.used.load(Relaxed));
|
||||
buffer_a.set_string(0, 0, &timer, Style::default());
|
||||
})?)
|
||||
}
|
||||
|
||||
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 } }
|
||||
|
|
@ -520,30 +566,6 @@ fn y_scroll () -> impl Draw<Tui> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Spawn the TUI output thread which writes colored characters to the terminal.
|
||||
pub fn tui_output <W: Write + Send + Sync + 'static, T: Draw<Tui> + Send + Sync + 'static> (
|
||||
output: W,
|
||||
exited: &Arc<AtomicBool>,
|
||||
state: &Arc<RwLock<T>>,
|
||||
sleep: Duration
|
||||
) -> Usually<Task> {
|
||||
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| {
|
||||
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 });
|
||||
tui_redraw(&mut backend, &mut buffer_a, &mut buffer_b);
|
||||
}
|
||||
let timer = format!("{:>3.3}ms", perf.used.load(Relaxed));
|
||||
buffer_a.set_string(0, 0, &timer, Style::default());
|
||||
})?)
|
||||
}
|
||||
|
||||
pub fn tui_redraw <'b, W: Write> (
|
||||
backend: &mut CrosstermBackend<W>,
|
||||
mut prev_buffer: &'b mut Buffer,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue