summaryrefslogtreecommitdiffstats
path: root/src/state.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-09 16:25:07 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-09 16:25:07 -0500
commit611026dc6ebd8933b624e91c0fa21552848d3306 (patch)
tree31b4949eb5204ab738f84a736b33cf9a8f494bc4 /src/state.rs
parenteb6354c8d2e13e6d9bb8d810bf45e5c1cd491d74 (diff)
downloadnbsh-611026dc6ebd8933b624e91c0fa21552848d3306.tar.gz
nbsh-611026dc6ebd8933b624e91c0fa21552848d3306.zip
simplify
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs123
1 files changed, 33 insertions, 90 deletions
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<usize> {
+ match self.focus {
+ Focus::History(idx) => Some(idx),
+ Focus::Readline => None,
+ Focus::Scrolling(idx) => idx,
+ }
+ }
+
+ fn scroll_up(&self, idx: Option<usize>) -> Option<usize> {
+ idx.map_or_else(
+ || Some(self.history.entry_count() - 1),
+ |idx| Some(idx.saturating_sub(1)),
+ )
+ }
+
+ fn scroll_down(&self, idx: Option<usize>) -> Option<usize> {
+ idx.and_then(|idx| {
+ if idx >= self.history.entry_count() - 1 {
+ None
+ } else {
+ Some(idx + 1)
+ }
+ })
+ }
}
#[derive(Copy, Clone, Debug)]