From 0eaff13c4ca55a3f842c68f6ccc478b0c0c7a6df Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 9 Dec 2021 16:39:57 -0500 Subject: refactor --- src/state.rs | 142 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 79 insertions(+), 63 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index 58f4a1f..205edcf 100644 --- a/src/state.rs +++ b/src/state.rs @@ -33,80 +33,96 @@ impl State { ) -> Option { if self.escape { self.escape = false; - let mut fallthrough = false; - match key { - textmode::Key::Ctrl(b'e') => { - fallthrough = true; - } - textmode::Key::Ctrl(b'l') => { - return Some(crate::action::Action::ForceRedraw); - } - textmode::Key::Char('f') => { - if let Focus::History(idx) = self.focus { - self.history.toggle_fullscreen(idx).await; - return Some(crate::action::Action::CheckUpdateScene); - } - } - textmode::Key::Char('j') => { - return Some(crate::action::Action::UpdateFocus( - Focus::Scrolling(self.scroll_down(self.focus_idx())), - )); - } - textmode::Key::Char('k') => { - return Some(crate::action::Action::UpdateFocus( - Focus::Scrolling(self.scroll_up(self.focus_idx())), - )); + self.handle_key_escape(key).await + } else if key == textmode::Key::Ctrl(b'e') { + self.escape = true; + None + } else { + match self.focus { + Focus::Readline => self.readline.handle_key(key).await, + Focus::History(idx) => { + self.history.handle_key(key, idx).await; + None } - textmode::Key::Char('r') => { - return Some(crate::action::Action::UpdateFocus( - Focus::Readline, - )); + Focus::Scrolling(idx) => { + self.handle_key_scrolling(key, idx).await } - _ => {} - } - if !fallthrough { - return None; } - } else if key == textmode::Key::Ctrl(b'e') { - self.escape = true; - return None; } + } - match self.focus { - Focus::Readline => self.readline.handle_key(key).await, - Focus::History(idx) => { - self.history.handle_key(key, idx).await; + async fn handle_key_escape( + &mut self, + key: textmode::Key, + ) -> Option { + match key { + textmode::Key::Ctrl(b'e') => { + if let Focus::History(idx) = self.focus { + self.history.handle_key(key, idx).await; + } None } - Focus::Scrolling(idx) => match key { - textmode::Key::Ctrl(b'm') => { - let focus = if let Some(idx) = idx { - self.history.running(idx).await - } else { - true - }; - if focus { - Some(crate::action::Action::UpdateFocus( - idx.map_or(Focus::Readline, |idx| { - Focus::History(idx) - }), - )) - } else { - None - } - } - textmode::Key::Char('j') => { - Some(crate::action::Action::UpdateFocus( - Focus::Scrolling(self.scroll_down(self.focus_idx())), - )) + textmode::Key::Ctrl(b'l') => { + Some(crate::action::Action::ForceRedraw) + } + textmode::Key::Char('f') => { + if let Focus::History(idx) = self.focus { + self.history.toggle_fullscreen(idx).await; + Some(crate::action::Action::CheckUpdateScene) + } else { + None } - textmode::Key::Char('k') => { + } + textmode::Key::Char('j') => { + Some(crate::action::Action::UpdateFocus(Focus::Scrolling( + self.scroll_down(self.focus_idx()), + ))) + } + textmode::Key::Char('k') => { + Some(crate::action::Action::UpdateFocus(Focus::Scrolling( + self.scroll_up(self.focus_idx()), + ))) + } + textmode::Key::Char('r') => { + Some(crate::action::Action::UpdateFocus(Focus::Readline)) + } + _ => None, + } + } + + async fn handle_key_scrolling( + &mut self, + key: textmode::Key, + idx: Option, + ) -> Option { + match key { + textmode::Key::Ctrl(b'm') => { + let focus = if let Some(idx) = idx { + self.history.running(idx).await + } else { + true + }; + if focus { Some(crate::action::Action::UpdateFocus( - Focus::Scrolling(self.scroll_up(self.focus_idx())), + idx.map_or(Focus::Readline, |idx| { + Focus::History(idx) + }), )) + } else { + None } - _ => None, - }, + } + textmode::Key::Char('j') => { + Some(crate::action::Action::UpdateFocus(Focus::Scrolling( + self.scroll_down(self.focus_idx()), + ))) + } + textmode::Key::Char('k') => { + Some(crate::action::Action::UpdateFocus(Focus::Scrolling( + self.scroll_up(self.focus_idx()), + ))) + } + _ => None, } } -- cgit v1.2.3-54-g00ecf