diff --git a/rm-main/src/tui.rs b/rm-main/src/tui.rs index 492e2bb..7149f03 100644 --- a/rm-main/src/tui.rs +++ b/rm-main/src/tui.rs @@ -1,9 +1,9 @@ -use std::time::Duration; +use std::{io, time::Duration}; use anyhow::Result; use crossterm::{ cursor, - event::{Event as CrosstermEvent, KeyEventKind}, + event::{Event, KeyEventKind}, terminal::{EnterAlternateScreen, LeaveAlternateScreen}, }; use futures::{FutureExt, StreamExt}; @@ -14,11 +14,9 @@ use tokio::{ }; use tokio_util::sync::CancellationToken; -use rm_shared::event::Event; - pub struct Tui { pub terminal: ratatui::Terminal>, - pub task: JoinHandle<()>, + pub task: JoinHandle>, pub cancellation_token: CancellationToken, pub event_rx: UnboundedReceiver, pub event_tx: UnboundedSender, @@ -29,7 +27,7 @@ impl Tui { let terminal = ratatui::Terminal::new(Backend::new(std::io::stdout()))?; let (event_tx, event_rx) = mpsc::unbounded_channel(); let cancellation_token = CancellationToken::new(); - let task = tokio::spawn(async {}); + let task = tokio::spawn(async { Ok(()) }); Ok(Self { terminal, task, @@ -39,7 +37,7 @@ impl Tui { }) } - pub fn start(&mut self) { + pub fn start(&mut self) -> Result<()> { self.cancel(); self.cancellation_token = CancellationToken::new(); let cancellation_token = self.cancellation_token.clone(); @@ -51,26 +49,29 @@ impl Tui { let crossterm_event = reader.next().fuse(); tokio::select! { _ = cancellation_token.cancelled() => break, - event = crossterm_event => Self::handle_crossterm_event(event, &event_tx), + event = crossterm_event => Self::handle_crossterm_event::(event, &event_tx)?, } } + Ok(()) }); + Ok(()) } fn handle_crossterm_event( - event: Option>, + event: Option>, event_tx: &UnboundedSender, - ) { + ) -> Result<()> { match event { - Some(Ok(CrosstermEvent::Key(key))) => { + Some(Ok(Event::Key(key))) => { if key.kind == KeyEventKind::Press { event_tx.send(Event::Key(key)).unwrap(); } } - Some(Ok(CrosstermEvent::Resize(_, _))) => event_tx.send(Event::Render).unwrap(), - Some(Err(_)) => event_tx.send(Event::Error).unwrap(), + Some(Ok(Event::Resize(x, y))) => event_tx.send(Event::Resize(x, y)).unwrap(), + Some(Err(e)) => Err(e)?, _ => (), } + Ok(()) } pub(crate) fn stop(&self) { @@ -91,7 +92,7 @@ impl Tui { pub(crate) fn enter(&mut self) -> Result<()> { crossterm::terminal::enable_raw_mode()?; crossterm::execute!(std::io::stdout(), EnterAlternateScreen, cursor::Hide)?; - self.start(); + self.start()?; Ok(()) } diff --git a/rm-shared/src/action.rs b/rm-shared/src/action.rs index 9772cd8..afbee34 100644 --- a/rm-shared/src/action.rs +++ b/rm-shared/src/action.rs @@ -1,7 +1,6 @@ -use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; +use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use indexmap::IndexMap; -use crate::event::Event; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Action { HardQuit, @@ -77,11 +76,9 @@ pub fn event_to_action( } match event { - Event::Quit => Some(A::Quit), - Event::Error => todo!(), - Event::Render => Some(A::Render), Event::Key(key) if mode == Mode::Input => Some(A::Input(key)), Event::Key(key) => key_event_to_action(key, keymap), + _ => None, } } diff --git a/rm-shared/src/event.rs b/rm-shared/src/event.rs deleted file mode 100644 index f1d22d8..0000000 --- a/rm-shared/src/event.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crossterm::event::KeyEvent; - -#[derive(Clone, Debug)] -pub enum Event { - Quit, - Error, - Render, - Key(KeyEvent), -} diff --git a/rm-shared/src/lib.rs b/rm-shared/src/lib.rs index 9aeb93a..e9a6726 100644 --- a/rm-shared/src/lib.rs +++ b/rm-shared/src/lib.rs @@ -1,2 +1 @@ pub mod action; -pub mod event;