diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-10 23:01:49 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-10 23:01:49 -0500 |
commit | 5c6400c5635362ccbb39e0324a4681d4029d3f74 (patch) | |
tree | 4a4166bdbffe11a315fce0b8e39f148b92180070 | |
parent | 58979a8db547a1083778951d6376a27930006cbc (diff) | |
download | nbsh-5c6400c5635362ccbb39e0324a4681d4029d3f74.tar.gz nbsh-5c6400c5635362ccbb39e0324a4681d4029d3f74.zip |
fix some scrolling edge cases
-rw-r--r-- | src/readline.rs | 11 | ||||
-rw-r--r-- | src/state.rs | 25 |
2 files changed, 20 insertions, 16 deletions
diff --git a/src/readline.rs b/src/readline.rs index d396d3d..37c466f 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -19,6 +19,7 @@ impl Readline { pub async fn handle_key( &mut self, key: textmode::Key, + history_size: usize, ) -> Option<crate::action::Action> { match key { textmode::Key::String(s) => self.add_input(&s), @@ -42,9 +43,13 @@ impl Readline { textmode::Key::Left => self.cursor_left(), textmode::Key::Right => self.cursor_right(), textmode::Key::Up => { - return Some(crate::action::Action::UpdateFocus( - crate::action::Focus::Scrolling(Some(usize::MAX)), - )) + if history_size > 0 { + return Some(crate::action::Action::UpdateFocus( + crate::action::Focus::Scrolling(Some( + history_size - 1, + )), + )); + } } _ => {} } diff --git a/src/state.rs b/src/state.rs index 5cc18f3..12317f4 100644 --- a/src/state.rs +++ b/src/state.rs @@ -36,7 +36,9 @@ impl State { } else { match self.focus { crate::action::Focus::Readline => { - self.readline.handle_key(key).await + self.readline + .handle_key(key, self.history.entry_count()) + .await } crate::action::Focus::History(idx) => { self.history.handle_key(key, idx).await; @@ -226,17 +228,7 @@ impl State { self.focus = crate::action::Focus::History(idx); self.hide_readline = true; } - crate::action::Action::UpdateFocus(mut new_focus) => { - match new_focus { - crate::action::Focus::Readline - | crate::action::Focus::Scrolling(None) => {} - crate::action::Focus::History(ref mut idx) - | crate::action::Focus::Scrolling(Some(ref mut idx)) => { - if *idx >= self.history.entry_count() { - *idx = self.history.entry_count() - 1; - } - } - } + crate::action::Action::UpdateFocus(new_focus) => { self.focus = new_focus; self.hide_readline = false; self.scene = self.default_scene(new_focus).await; @@ -290,7 +282,14 @@ impl State { fn scroll_up(&self, idx: Option<usize>) -> Option<usize> { idx.map_or_else( - || Some(self.history.entry_count() - 1), + || { + let count = self.history.entry_count(); + if count == 0 { + None + } else { + Some(count - 1) + } + }, |idx| Some(idx.saturating_sub(1)), ) } |