From f3e694d3d630bbde2b46d955250db596625d3fd6 Mon Sep 17 00:00:00 2001 From: veeso Date: Tue, 28 Sep 2021 12:59:43 +0200 Subject: [PATCH] File list can now be rewinded --- CHANGELOG.md | 3 ++- src/ui/components/file_list.rs | 35 +++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64cbef5..cf4fe03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,9 +50,10 @@ Released on ?? - Added a new key in themes: `misc_info_dialog`: if your theme won't load, just reload it. If you're using a customised theme, you can add to it the missing key via a text editor. Just edit the `theme.toml` in your `$CONFIG_DIR/termscp/theme.toml` and add `misc_info_dialog` (Read more in manual at Themes). - Enhancements: - Reuse mounts in UI, in order to reduce executable size + - File list can now be "rewinded", which means that moving with arrows will now allow you to go from top to bottom of the list pressing `` and viceversa pressing ``. - Dependencies: - - Added `rust-s3 0.27-rc4` - Added `notify_rust 4.5.3` + - Added `rust-s3 0.27-rc4` - Added `self_update 0.27.0` - Updated `argh` to `0.1.6` - Updated `dirs` to `4.0.0` diff --git a/src/ui/components/file_list.rs b/src/ui/components/file_list.rs index 9600659..5982973 100644 --- a/src/ui/components/file_list.rs +++ b/src/ui/components/file_list.rs @@ -188,21 +188,27 @@ impl OwnStates { /// ### incr_list_index /// - /// Incremenet list index - pub fn incr_list_index(&mut self) { + /// Incremenet list index. + /// If `can_rewind` is `true` the index rewinds when boundary is reached + pub fn incr_list_index(&mut self, can_rewind: bool) { // Check if index is at last element if self.list_index + 1 < self.list_len() { self.list_index += 1; + } else if can_rewind { + self.list_index = 0; } } /// ### decr_list_index /// /// Decrement list index - pub fn decr_list_index(&mut self) { + /// If `can_rewind` is `true` the index rewinds when boundary is reached + pub fn decr_list_index(&mut self, can_rewind: bool) { // Check if index is bigger than 0 if self.list_index > 0 { self.list_index -= 1; + } else if self.list_len() > 0 && can_rewind { + self.list_index = self.list_len() - 1; } } @@ -388,25 +394,25 @@ impl Component for FileList { match key.code { KeyCode::Down => { // Update states - self.states.incr_list_index(); + self.states.incr_list_index(true); Msg::None } KeyCode::Up => { // Update states - self.states.decr_list_index(); + self.states.decr_list_index(true); Msg::None } KeyCode::PageDown => { // Update states for _ in 0..8 { - self.states.incr_list_index(); + self.states.incr_list_index(false); } Msg::None } KeyCode::PageUp => { // Update states for _ in 0..8 { - self.states.decr_list_index(); + self.states.decr_list_index(false); } Msg::None } @@ -525,14 +531,21 @@ mod tests { assert_eq!(states.selected[0], 4); // Index states.init_list_states(2); - states.incr_list_index(); + // Incr + states.incr_list_index(false); assert_eq!(states.list_index(), 1); - states.incr_list_index(); + states.incr_list_index(false); assert_eq!(states.list_index(), 1); - states.decr_list_index(); + states.incr_list_index(true); assert_eq!(states.list_index(), 0); - states.decr_list_index(); + // Decr + states.list_index = 1; + states.decr_list_index(false); assert_eq!(states.list_index(), 0); + states.decr_list_index(false); + assert_eq!(states.list_index(), 0); + states.decr_list_index(true); + assert_eq!(states.list_index(), 1); // Try fixing index states.init_list_states(5); states.list_index = 4;