From 611026dc6ebd8933b624e91c0fa21552848d3306 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 9 Dec 2021 16:25:07 -0500 Subject: simplify --- src/state.rs | 123 ++++++++++++++++------------------------------------------- 1 file changed, 33 insertions(+), 90 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index 75f7dfd..58f4a1f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -48,56 +48,13 @@ impl State { } } textmode::Key::Char('j') => { - let new_focus = match self.focus { - Focus::History(idx) => { - if idx >= self.history.entry_count() - 1 { - Focus::Scrolling(None) - } else { - Focus::Scrolling(Some(idx + 1)) - } - } - Focus::Readline => Focus::Scrolling(None), - Focus::Scrolling(idx) => { - idx.map_or(Focus::Scrolling(None), |idx| { - if idx >= self.history.entry_count() - 1 { - Focus::Scrolling(None) - } else { - Focus::Scrolling(Some(idx + 1)) - } - }) - } - }; return Some(crate::action::Action::UpdateFocus( - new_focus, + Focus::Scrolling(self.scroll_down(self.focus_idx())), )); } textmode::Key::Char('k') => { - let new_focus = match self.focus { - Focus::History(idx) => { - if idx == 0 { - Focus::Scrolling(Some(0)) - } else { - Focus::Scrolling(Some(idx - 1)) - } - } - Focus::Readline => Focus::Scrolling(Some( - self.history.entry_count() - 1, - )), - Focus::Scrolling(idx) => idx.map_or( - Focus::Scrolling(Some( - self.history.entry_count() - 1, - )), - |idx| { - if idx == 0 { - Focus::Scrolling(Some(0)) - } else { - Focus::Scrolling(Some(idx - 1)) - } - }, - ), - }; return Some(crate::action::Action::UpdateFocus( - new_focus, + Focus::Scrolling(self.scroll_up(self.focus_idx())), )); } textmode::Key::Char('r') => { @@ -139,53 +96,14 @@ impl State { } } textmode::Key::Char('j') => { - let new_focus = match self.focus { - Focus::History(idx) => { - if idx >= self.history.entry_count() - 1 { - Focus::Scrolling(None) - } else { - Focus::Scrolling(Some(idx + 1)) - } - } - Focus::Readline => Focus::Scrolling(None), - Focus::Scrolling(idx) => { - idx.map_or(Focus::Scrolling(None), |idx| { - if idx >= self.history.entry_count() - 1 { - Focus::Scrolling(None) - } else { - Focus::Scrolling(Some(idx + 1)) - } - }) - } - }; - Some(crate::action::Action::UpdateFocus(new_focus)) + Some(crate::action::Action::UpdateFocus( + Focus::Scrolling(self.scroll_down(self.focus_idx())), + )) } textmode::Key::Char('k') => { - let new_focus = match self.focus { - Focus::History(idx) => { - if idx == 0 { - Focus::Scrolling(Some(0)) - } else { - Focus::Scrolling(Some(idx - 1)) - } - } - Focus::Readline => Focus::Scrolling(Some( - self.history.entry_count() - 1, - )), - Focus::Scrolling(idx) => idx.map_or( - Focus::Scrolling(Some( - self.history.entry_count() - 1, - )), - |idx| { - if idx == 0 { - Focus::Scrolling(Some(0)) - } else { - Focus::Scrolling(Some(idx - 1)) - } - }, - ), - }; - Some(crate::action::Action::UpdateFocus(new_focus)) + Some(crate::action::Action::UpdateFocus( + Focus::Scrolling(self.scroll_up(self.focus_idx())), + )) } _ => None, }, @@ -319,6 +237,31 @@ impl State { } } } + + fn focus_idx(&self) -> Option { + match self.focus { + Focus::History(idx) => Some(idx), + Focus::Readline => None, + Focus::Scrolling(idx) => idx, + } + } + + fn scroll_up(&self, idx: Option) -> Option { + idx.map_or_else( + || Some(self.history.entry_count() - 1), + |idx| Some(idx.saturating_sub(1)), + ) + } + + fn scroll_down(&self, idx: Option) -> Option { + idx.and_then(|idx| { + if idx >= self.history.entry_count() - 1 { + None + } else { + Some(idx + 1) + } + }) + } } #[derive(Copy, Clone, Debug)] -- cgit v1.2.3-54-g00ecf