diff options
author | sushi-shi <47691267+sushi-shi@users.noreply.github.com> | 2022-02-23 21:03:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-23 13:03:12 -0500 |
commit | 06c7c8e452f12cc87a1b00b2151ab9f5c8b0b2e1 (patch) | |
tree | 78fa4f7179dd51ed9395628c606a1d9419fbffa9 /src | |
parent | fe3308c75da696b37c970c12ef9cefe942fa8377 (diff) |
Fix overflows in viewport (#141)
Diffstat (limited to 'src')
-rw-r--r-- | src/fs/dirlist.rs | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/fs/dirlist.rs b/src/fs/dirlist.rs index a2d4bb8..a3770f7 100644 --- a/src/fs/dirlist.rs +++ b/src/fs/dirlist.rs @@ -77,12 +77,16 @@ impl JoshutoDirList { // calculate viewport let viewport_end = self.viewport_index + height; - if (viewport_end as i16 - ix as i16 - 1) < scroll_offset as i16 { + let new_viewport_end = scroll_offset + ix + 1; + if self.len() < new_viewport_end { + // cursor at the end + self.viewport_index = self.len().saturating_sub(height); + } else if viewport_end < new_viewport_end { // cursor too low - self.viewport_index = (ix + scroll_offset - height + 1) as usize; - } else if (ix as i16 - self.viewport_index as i16) < scroll_offset as i16 { - // cursor too high - self.viewport_index = cmp::max(ix as i16 - scroll_offset as i16, 0) as usize; + self.viewport_index = new_viewport_end - height; + } else if ix.saturating_sub(self.viewport_index) < scroll_offset { + // cursor too high or at the beginning + self.viewport_index = ix.saturating_sub(scroll_offset); } } else { self.viewport_index = 0; |