From 009171812efa815ae6e445178689a08400905c04 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 12 Dec 2021 23:53:01 -0500 Subject: more event loop refactoring --- src/state.rs | 66 +++++++++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 39 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index a3d251b..cdc3cdb 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,5 +1,3 @@ -use textmode::Textmode as _; - #[derive(Copy, Clone, Debug)] enum Focus { Readline, @@ -13,6 +11,13 @@ enum Scene { Fullscreen, } +pub enum Action { + Refresh, + HardRefresh, + Resize(u16, u16), + Quit, +} + pub struct State { readline: crate::readline::Readline, history: crate::history::History, @@ -38,8 +43,7 @@ impl State { pub async fn render( &self, - out: &mut textmode::Output, - hard: bool, + out: &mut impl textmode::Textmode, ) -> anyhow::Result<()> { out.clear(); match self.scene { @@ -119,29 +123,22 @@ impl State { } } } - if hard { - out.hard_refresh().await?; - } else { - out.refresh().await?; - } Ok(()) } pub async fn handle_event( &mut self, event: crate::event::Event, - out: &mut textmode::Output, event_w: &async_std::channel::Sender, - ) { - let mut hard_refresh = false; + ) -> Option { match event { crate::event::Event::Key(key) => { - let (quit, hard) = if self.escape { + return if self.escape { self.escape = false; self.handle_key_escape(key).await } else if key == textmode::Key::Ctrl(b'e') { self.escape = true; - (false, false) + None } else { match self.focus { Focus::Readline => { @@ -150,25 +147,18 @@ impl State { } Focus::History(idx) => { self.handle_key_history(key, idx).await; - (false, false) + None } Focus::Scrolling(_) => { self.handle_key_escape(key).await } } }; - if quit { - event_w.send(crate::event::Event::Quit).await.unwrap(); - } - if hard { - hard_refresh = true; - } } crate::event::Event::Resize(new_size) => { self.readline.resize(new_size).await; self.history.resize(new_size).await; - out.set_size(new_size.0, new_size.1); - hard_refresh = true; + return Some(Action::Resize(new_size.0, new_size.1)); } crate::event::Event::ProcessOutput => { // the number of visible lines may have changed, so make sure @@ -201,28 +191,24 @@ impl State { } } crate::event::Event::ClockTimer => {} - crate::event::Event::Quit => { - // the debouncer should return None in this case - unreachable!(); - } - } - self.render(out, hard_refresh).await.unwrap(); + }; + Some(Action::Refresh) } async fn handle_key_escape( &mut self, key: textmode::Key, - ) -> (bool, bool) { + ) -> Option { match key { textmode::Key::Ctrl(b'd') => { - return (true, false); + return Some(Action::Quit); } textmode::Key::Ctrl(b'e') => { self.set_focus(Focus::Scrolling(self.focus_idx()), None) .await; } textmode::Key::Ctrl(b'l') => { - return (false, true); + return Some(Action::HardRefresh); } textmode::Key::Ctrl(b'm') => { let idx = self.focus_idx(); @@ -293,26 +279,28 @@ impl State { textmode::Key::Char('r') => { self.set_focus(Focus::Readline, None).await; } - _ => {} + _ => { + return None; + } } - (false, false) + Some(Action::Refresh) } async fn handle_key_readline( &mut self, key: textmode::Key, event_w: async_std::channel::Sender, - ) -> (bool, bool) { + ) -> Option { match key { textmode::Key::Char(c) => { self.readline.add_input(&c.to_string()); } textmode::Key::Ctrl(b'c') => self.readline.clear_input(), textmode::Key::Ctrl(b'd') => { - return (true, false); + return Some(Action::Quit); } textmode::Key::Ctrl(b'l') => { - return (false, true); + return Some(Action::HardRefresh); } textmode::Key::Ctrl(b'm') => { let cmd = self.readline.input(); @@ -336,9 +324,9 @@ impl State { .await; } } - _ => {} + _ => return None, } - (false, false) + Some(Action::Refresh) } async fn handle_key_history(&mut self, key: textmode::Key, idx: usize) { -- cgit v1.2.3-54-g00ecf