From 62fe2477fb8bbbfbca34779b992979f9c1a8ba53 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 11 Dec 2021 17:54:10 -0500 Subject: move key handling into the main event loop --- src/state.rs | 253 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 129 insertions(+), 124 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index 6ecfa07..f086334 100644 --- a/src/state.rs +++ b/src/state.rs @@ -23,130 +23,6 @@ impl State { } } - pub async fn handle_key( - &mut self, - key: textmode::Key, - ) -> Option { - if self.escape { - self.escape = false; - self.handle_key_escape(key).await - } else if key == textmode::Key::Ctrl(b'e') { - self.escape = true; - None - } else { - match self.focus { - crate::action::Focus::Readline => { - self.readline - .handle_key(key, self.history.entry_count()) - .await - } - crate::action::Focus::History(idx) => { - self.history.handle_key(key, idx).await; - None - } - crate::action::Focus::Scrolling(_) => { - self.handle_key_escape(key).await - } - } - } - } - - async fn handle_key_escape( - &mut self, - key: textmode::Key, - ) -> Option { - match key { - textmode::Key::Ctrl(b'd') => { - return Some(crate::action::Action::Quit); - } - textmode::Key::Ctrl(b'e') => { - self.set_focus( - crate::action::Focus::Scrolling(self.focus_idx()), - None, - ) - .await; - } - textmode::Key::Ctrl(b'l') => { - return Some(crate::action::Action::ForceRedraw); - } - textmode::Key::Ctrl(b'm') => { - let idx = self.focus_idx(); - let (focus, entry) = if let Some(idx) = idx { - let entry = self.history.entry(idx).await; - (entry.running(), Some(entry)) - } else { - (true, None) - }; - if focus { - self.set_focus( - idx.map_or(crate::action::Focus::Readline, |idx| { - crate::action::Focus::History(idx) - }), - entry, - ) - .await; - } - } - textmode::Key::Char(' ') => { - if let Some(idx) = self.focus_idx() { - let entry = self.history.entry(idx).await; - self.readline.set_input(&entry.cmd()); - self.set_focus( - crate::action::Focus::Readline, - Some(entry), - ) - .await; - } - } - textmode::Key::Char('e') => { - if let crate::action::Focus::History(idx) = self.focus { - self.history - .handle_key(textmode::Key::Ctrl(b'e'), idx) - .await; - } - } - textmode::Key::Char('f') => { - if let Some(idx) = self.focus_idx() { - let mut entry = self.history.entry(idx).await; - let mut focus = crate::action::Focus::History(idx); - if let crate::action::Focus::Scrolling(_) = self.focus { - entry.set_fullscreen(true); - } else { - entry.toggle_fullscreen(); - if !entry.should_fullscreen() && !entry.running() { - focus = - crate::action::Focus::Scrolling(Some(idx)); - } - } - self.set_focus(focus, Some(entry)).await; - } - } - textmode::Key::Char('j') | textmode::Key::Down => { - self.set_focus( - crate::action::Focus::Scrolling( - self.scroll_down(self.focus_idx()), - ), - None, - ) - .await; - } - textmode::Key::Char('k') | textmode::Key::Up => { - self.set_focus( - crate::action::Focus::Scrolling( - self.scroll_up(self.focus_idx()), - ), - None, - ) - .await; - } - textmode::Key::Char('r') => { - self.set_focus(crate::action::Focus::Readline, None).await; - } - _ => {} - } - Some(crate::action::Action::Render) - } - pub async fn render( &self, out: &mut textmode::Output, @@ -246,6 +122,11 @@ impl State { ) { let mut hard_refresh = false; match action { + crate::action::Action::Key(key) => { + if let Some(action) = self.handle_key(key).await { + action_w.send(action).await.unwrap(); + } + } crate::action::Action::Render => { // for instance, if we are rerendering because of command // output, that output could increase the number of lines of @@ -295,6 +176,130 @@ impl State { self.render(out, hard_refresh).await.unwrap(); } + async fn handle_key( + &mut self, + key: textmode::Key, + ) -> Option { + if self.escape { + self.escape = false; + self.handle_key_escape(key).await + } else if key == textmode::Key::Ctrl(b'e') { + self.escape = true; + None + } else { + match self.focus { + crate::action::Focus::Readline => { + self.readline + .handle_key(key, self.history.entry_count()) + .await + } + crate::action::Focus::History(idx) => { + self.history.handle_key(key, idx).await; + None + } + crate::action::Focus::Scrolling(_) => { + self.handle_key_escape(key).await + } + } + } + } + + async fn handle_key_escape( + &mut self, + key: textmode::Key, + ) -> Option { + match key { + textmode::Key::Ctrl(b'd') => { + return Some(crate::action::Action::Quit); + } + textmode::Key::Ctrl(b'e') => { + self.set_focus( + crate::action::Focus::Scrolling(self.focus_idx()), + None, + ) + .await; + } + textmode::Key::Ctrl(b'l') => { + return Some(crate::action::Action::ForceRedraw); + } + textmode::Key::Ctrl(b'm') => { + let idx = self.focus_idx(); + let (focus, entry) = if let Some(idx) = idx { + let entry = self.history.entry(idx).await; + (entry.running(), Some(entry)) + } else { + (true, None) + }; + if focus { + self.set_focus( + idx.map_or(crate::action::Focus::Readline, |idx| { + crate::action::Focus::History(idx) + }), + entry, + ) + .await; + } + } + textmode::Key::Char(' ') => { + if let Some(idx) = self.focus_idx() { + let entry = self.history.entry(idx).await; + self.readline.set_input(&entry.cmd()); + self.set_focus( + crate::action::Focus::Readline, + Some(entry), + ) + .await; + } + } + textmode::Key::Char('e') => { + if let crate::action::Focus::History(idx) = self.focus { + self.history + .handle_key(textmode::Key::Ctrl(b'e'), idx) + .await; + } + } + textmode::Key::Char('f') => { + if let Some(idx) = self.focus_idx() { + let mut entry = self.history.entry(idx).await; + let mut focus = crate::action::Focus::History(idx); + if let crate::action::Focus::Scrolling(_) = self.focus { + entry.set_fullscreen(true); + } else { + entry.toggle_fullscreen(); + if !entry.should_fullscreen() && !entry.running() { + focus = + crate::action::Focus::Scrolling(Some(idx)); + } + } + self.set_focus(focus, Some(entry)).await; + } + } + textmode::Key::Char('j') | textmode::Key::Down => { + self.set_focus( + crate::action::Focus::Scrolling( + self.scroll_down(self.focus_idx()), + ), + None, + ) + .await; + } + textmode::Key::Char('k') | textmode::Key::Up => { + self.set_focus( + crate::action::Focus::Scrolling( + self.scroll_up(self.focus_idx()), + ), + None, + ) + .await; + } + textmode::Key::Char('r') => { + self.set_focus(crate::action::Focus::Readline, None).await; + } + _ => {} + } + Some(crate::action::Action::Render) + } + async fn default_scene( &self, focus: crate::action::Focus, -- cgit v1.2.3-54-g00ecf