From 26bb4e54e9669b487817d09ebfa36836293d741d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 17 Nov 2021 21:45:25 -0500 Subject: make action handling the main task --- src/action.rs | 6 ++++-- src/history.rs | 7 +------ src/main.rs | 12 +++++------- src/readline.rs | 5 ++--- src/state.rs | 16 ++++++++++------ 5 files changed, 22 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/action.rs b/src/action.rs index 2e27393..c7f437c 100644 --- a/src/action.rs +++ b/src/action.rs @@ -5,6 +5,7 @@ pub enum Action { Run(String), UpdateFocus(crate::state::Focus), Resize((u16, u16)), + Quit, } pub struct Debouncer { @@ -46,7 +47,8 @@ impl Pending { } fn has_event(&self) -> bool { - self.render.is_some() + self.done + || self.render.is_some() || self.force_redraw.is_some() || !self.run.is_empty() || self.focus.is_some() @@ -85,7 +87,7 @@ impl Pending { Some(Action::Run(cmd)) => self.run.push_back(cmd.to_string()), Some(Action::UpdateFocus(focus)) => self.focus = Some(*focus), Some(Action::Resize(size)) => self.size = Some(*size), - None => self.done = true, + Some(Action::Quit) | None => self.done = true, } } } diff --git a/src/history.rs b/src/history.rs index 3c15625..8549ab7 100644 --- a/src/history.rs +++ b/src/history.rs @@ -122,16 +122,11 @@ impl History { Ok(self.entries.len() - 1) } - pub async fn handle_key( - &mut self, - key: textmode::Key, - idx: usize, - ) -> bool { + pub async fn handle_key(&mut self, key: textmode::Key, idx: usize) { let entry = self.entries[idx].lock_arc().await; if entry.running() { entry.input.send(key.into_bytes()).await.unwrap(); } - false } pub async fn render( diff --git a/src/main.rs b/src/main.rs index 6247d9f..05b889c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,17 +47,15 @@ async fn async_main() -> anyhow::Result<()> { { let state = async_std::sync::Arc::clone(&state); async_std::task::spawn(async move { - let debouncer = crate::action::debounce(action_r); - while let Some(action) = debouncer.recv().await { - state.lock_arc().await.handle_action(action).await; + while let Some(key) = input.read_key().await.unwrap() { + state.lock_arc().await.handle_input(key).await; } }); } - while let Some(key) = input.read_key().await.unwrap() { - if state.lock_arc().await.handle_input(key).await { - break; - } + let debouncer = crate::action::debounce(action_r); + while let Some(action) = debouncer.recv().await { + state.lock_arc().await.handle_action(action).await; } Ok(()) diff --git a/src/readline.rs b/src/readline.rs index 9713a8a..093de3d 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -22,7 +22,7 @@ impl Readline { } } - pub async fn handle_key(&mut self, key: textmode::Key) -> bool { + pub async fn handle_key(&mut self, key: textmode::Key) { match key { textmode::Key::String(s) => self.add_input(&s), textmode::Key::Char(c) => { @@ -30,7 +30,7 @@ impl Readline { } textmode::Key::Ctrl(b'c') => self.clear_input(), textmode::Key::Ctrl(b'd') => { - return true; + self.action.send(crate::action::Action::Quit).await.unwrap(); } textmode::Key::Ctrl(b'l') => { self.action @@ -55,7 +55,6 @@ impl Readline { .send(crate::action::Action::Render) .await .unwrap(); - false } pub fn lines(&self) -> usize { diff --git a/src/state.rs b/src/state.rs index f12fb8a..13fd3fd 100644 --- a/src/state.rs +++ b/src/state.rs @@ -90,15 +90,19 @@ impl State { self.output.hard_refresh().await.unwrap(); self.render(false).await.unwrap(); } + crate::action::Action::Quit => { + // the debouncer should return None in this case + unreachable!(); + } } } - pub async fn handle_input(&mut self, key: textmode::Key) -> bool { + pub async fn handle_input(&mut self, key: textmode::Key) { if self.escape { - let mut ret = true; + let mut fallthrough = false; match key { textmode::Key::Ctrl(b'e') => { - ret = false; // fall through and handle normally + fallthrough = true; } textmode::Key::Ctrl(b'l') => { self.render(true).await.unwrap(); @@ -149,12 +153,12 @@ impl State { _ => {} } self.escape = false; - if ret { - return false; + if !fallthrough { + return; } } else if key == textmode::Key::Ctrl(b'e') { self.escape = true; - return false; + return; } match self.focus { -- cgit v1.2.3-54-g00ecf