summaryrefslogtreecommitdiffstats
path: root/src/state.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-09 16:39:57 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-09 16:39:57 -0500
commit0eaff13c4ca55a3f842c68f6ccc478b0c0c7a6df (patch)
treebf20c221afb4da774f07358ca1dc7d1e62eb6914 /src/state.rs
parent611026dc6ebd8933b624e91c0fa21552848d3306 (diff)
downloadnbsh-0eaff13c4ca55a3f842c68f6ccc478b0c0c7a6df.tar.gz
nbsh-0eaff13c4ca55a3f842c68f6ccc478b0c0c7a6df.zip
refactor
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs142
1 files changed, 79 insertions, 63 deletions
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<crate::action::Action> {
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<crate::action::Action> {
+ 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<usize>,
+ ) -> Option<crate::action::Action> {
+ 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,
}
}