Stateful closing and io input
This commit is contained in:
+39
-16
@@ -11,25 +11,15 @@ pub mod widget;
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait App {
|
||||
// State
|
||||
async fn init(&mut self, ctx: state::Context);
|
||||
async fn update(&mut self, event: Box<dyn Any>);
|
||||
async fn init(&mut self, ctx: &state::Context);
|
||||
async fn update(&mut self, ctx: &state::Context, event: Box<dyn Any + Send + Sync>);
|
||||
|
||||
// Rendering
|
||||
async fn layout(&self) -> layout::LayoutItem;
|
||||
async fn render(&mut self, allocation: layout::Allocation);
|
||||
|
||||
async fn refresh(&mut self) {
|
||||
crossterm::execute!(
|
||||
std::io::stdout(),
|
||||
crossterm::terminal::Clear(crossterm::terminal::ClearType::All)
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
crossterm::execute!(
|
||||
std::io::stdout(),
|
||||
crossterm::terminal::Clear(crossterm::terminal::ClearType::Purge)
|
||||
)
|
||||
.unwrap();
|
||||
clear();
|
||||
|
||||
crossterm::execute!(std::io::stdout(), crossterm::cursor::Hide).unwrap();
|
||||
|
||||
@@ -41,18 +31,35 @@ pub trait App {
|
||||
pub async fn run<A: App>(create_app: impl FnOnce(&state::Context) -> A) {
|
||||
let (tx, mut rx) = mpsc::channel(100);
|
||||
|
||||
let ctx = state::Context { tx };
|
||||
let ctx = state::Context { tx: tx.clone() };
|
||||
|
||||
crossterm::terminal::enable_raw_mode().unwrap();
|
||||
|
||||
tokio::spawn(async move {
|
||||
tx.send(Box::new(crossterm::event::read().unwrap()))
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
let mut app = create_app(&ctx);
|
||||
|
||||
app.init(ctx).await;
|
||||
app.init(&ctx).await;
|
||||
|
||||
app.refresh().await;
|
||||
|
||||
while let Some(event) = rx.recv().await {
|
||||
app.update(event).await;
|
||||
if let Some(state::Close) = event.downcast_ref() {
|
||||
break;
|
||||
}
|
||||
|
||||
app.update(&ctx, event).await;
|
||||
app.refresh().await;
|
||||
}
|
||||
|
||||
crossterm::terminal::disable_raw_mode().unwrap();
|
||||
crossterm::execute!(std::io::stdout(), crossterm::cursor::Show).unwrap();
|
||||
|
||||
clear();
|
||||
}
|
||||
|
||||
pub fn get_screen() -> unit::Rect {
|
||||
@@ -65,3 +72,19 @@ pub fn get_screen() -> unit::Rect {
|
||||
height,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear() {
|
||||
crossterm::execute!(
|
||||
std::io::stdout(),
|
||||
crossterm::terminal::Clear(crossterm::terminal::ClearType::All)
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
crossterm::execute!(
|
||||
std::io::stdout(),
|
||||
crossterm::terminal::Clear(crossterm::terminal::ClearType::Purge)
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
crossterm::execute!(std::io::stdout(), crossterm::cursor::MoveTo(0, 0)).unwrap();
|
||||
}
|
||||
|
||||
+11
-4
@@ -3,25 +3,26 @@ use std::{any::Any, sync::Arc};
|
||||
use tokio::sync::{Mutex, MutexGuard, mpsc::Sender};
|
||||
|
||||
pub struct Refresh;
|
||||
pub struct Close;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Context {
|
||||
pub(crate) tx: Sender<Box<dyn Any>>,
|
||||
pub(crate) tx: Sender<Box<dyn Any + Send + Sync>>,
|
||||
}
|
||||
|
||||
pub struct State<T> {
|
||||
pub value: Arc<Mutex<T>>,
|
||||
tx: Sender<Box<dyn Any>>,
|
||||
tx: Sender<Box<dyn Any + Send + Sync>>,
|
||||
}
|
||||
|
||||
pub struct StateGuard<'a, T> {
|
||||
is_mutated: bool,
|
||||
value: MutexGuard<'a, T>,
|
||||
tx: &'a Sender<Box<dyn Any>>,
|
||||
tx: &'a Sender<Box<dyn Any + Send + Sync>>,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn use_state<T>(&self, v: T) -> State<T> {
|
||||
pub fn use_state<T: Send + Sync>(&self, v: T) -> State<T> {
|
||||
State {
|
||||
value: Arc::new(Mutex::new(v)),
|
||||
tx: self.tx.clone(),
|
||||
@@ -62,3 +63,9 @@ impl<'a, T> Drop for StateGuard<'a, T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub async fn close(&self) {
|
||||
self.tx.send(Box::new(Close)).await.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user