Skip to content

Commit

Permalink
get rid of crate's event
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Jun 25, 2024
1 parent 0680e22 commit 5f7941e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
29 changes: 15 additions & 14 deletions rm-main/src/tui.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -14,11 +14,9 @@ use tokio::{
};
use tokio_util::sync::CancellationToken;

use rm_shared::event::Event;

pub struct Tui {
pub terminal: ratatui::Terminal<Backend<std::io::Stdout>>,
pub task: JoinHandle<()>,
pub task: JoinHandle<Result<()>>,
pub cancellation_token: CancellationToken,
pub event_rx: UnboundedReceiver<Event>,
pub event_tx: UnboundedSender<Event>,
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -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::<io::Error>(event, &event_tx)?,
}
}
Ok(())
});
Ok(())
}

fn handle_crossterm_event<T>(
event: Option<Result<CrosstermEvent, T>>,
event: Option<Result<Event, io::Error>>,
event_tx: &UnboundedSender<Event>,
) {
) -> 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) {
Expand All @@ -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(())
}

Expand Down
7 changes: 2 additions & 5 deletions rm-shared/src/action.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
}
}

Expand Down
9 changes: 0 additions & 9 deletions rm-shared/src/event.rs

This file was deleted.

1 change: 0 additions & 1 deletion rm-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod action;
pub mod event;

0 comments on commit 5f7941e

Please sign in to comment.