From 882c8c4b247f19febf972844632229649e0769c2 Mon Sep 17 00:00:00 2001 From: nyx Date: Wed, 2 Oct 2024 00:49:34 -0400 Subject: [PATCH 1/6] allow scrolling past last entry --- tui/src/state.rs | 71 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index ce724868..7de126f2 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -478,17 +478,9 @@ impl AppState { Focus::TabList => match key.code { KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => self.focus = Focus::List, - KeyCode::Char('j') | KeyCode::Down - if self.current_tab.selected().unwrap() + 1 < self.tabs.len() => - { - self.current_tab.select_next(); - self.refresh_tab(); - } + KeyCode::Char('j') | KeyCode::Down => self.scroll_tab_down(), - KeyCode::Char('k') | KeyCode::Up => { - self.current_tab.select_previous(); - self.refresh_tab(); - } + KeyCode::Char('k') | KeyCode::Up => self.scroll_tab_up(), KeyCode::Char('/') => self.enter_search(), KeyCode::Char('t') => self.theme.next(), @@ -498,8 +490,8 @@ impl AppState { }, Focus::List if key.kind != KeyEventKind::Release => match key.code { - KeyCode::Char('j') | KeyCode::Down => self.selection.select_next(), - KeyCode::Char('k') | KeyCode::Up => self.selection.select_previous(), + KeyCode::Char('j') | KeyCode::Down => self.scroll_down(), + KeyCode::Char('k') | KeyCode::Up => self.scroll_up(), KeyCode::Char('p') | KeyCode::Char('P') => self.enable_preview(), KeyCode::Char('d') | KeyCode::Char('D') => self.enable_description(), KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => self.handle_enter(), @@ -518,6 +510,32 @@ impl AppState { true } + fn scroll_down(&mut self) { + let len = self.filter.item_list().len(); + if len == 0 { + return; + } + let current = self.selection.selected().unwrap_or(0); + let max_index = if self.at_root() { len - 1 } else { len }; + let next = if current + 1 > max_index { + 0 + } else { + current + 1 + }; + self.selection.select(Some(next)); + } + + fn scroll_up(&mut self) { + let len = self.filter.item_list().len(); + if len == 0 { + return; + } + let current = self.selection.selected().unwrap_or(0); + let max_index = if self.at_root() { len - 1 } else { len }; + let next = if current == 0 { max_index } else { current - 1 }; + self.selection.select(Some(next)); + } + fn toggle_multi_select(&mut self) { if self.is_current_tab_multi_selectable() { self.multi_select = !self.multi_select; @@ -554,6 +572,13 @@ impl AppState { self.multi_select = false; self.selected_commands.clear(); } + let len = self.filter.item_list().len(); + if len > 0 { + let current = self.selection.selected().unwrap_or(0); + self.selection.select(Some(current.min(len - 1))); + } else { + self.selection.select(None); + } } /// Checks either the current tree node is the root node (can we go up the tree or no) @@ -733,6 +758,28 @@ impl AppState { 80, ); } + + fn scroll_tab_down(&mut self) { + let len = self.tabs.len(); + if len == 0 { + return; + } + let current = self.current_tab.selected().unwrap_or(0); + let next = if current + 1 >= len { 0 } else { current + 1 }; + self.current_tab.select(Some(next)); + self.refresh_tab(); + } + + fn scroll_tab_up(&mut self) { + let len = self.tabs.len(); + if len == 0 { + return; + } + let current = self.current_tab.selected().unwrap_or(0); + let next = if current == 0 { len - 1 } else { current - 1 }; + self.current_tab.select(Some(next)); + self.refresh_tab(); + } } #[cfg(feature = "tips")] From e3ed8bfa5a90af8a3870958d1446b7d22992b768 Mon Sep 17 00:00:00 2001 From: Nyx <144965845+nnyyxxxx@users.noreply.github.com> Date: Wed, 2 Oct 2024 03:04:49 -0400 Subject: [PATCH 2/6] Update tui/src/state.rs Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com> --- tui/src/state.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index 7de126f2..91688e32 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -772,9 +772,6 @@ impl AppState { fn scroll_tab_up(&mut self) { let len = self.tabs.len(); - if len == 0 { - return; - } let current = self.current_tab.selected().unwrap_or(0); let next = if current == 0 { len - 1 } else { current - 1 }; self.current_tab.select(Some(next)); From 8276d28b8d2a9fb7748de5c564f1f4dcf127b81d Mon Sep 17 00:00:00 2001 From: Nyx <144965845+nnyyxxxx@users.noreply.github.com> Date: Wed, 2 Oct 2024 03:04:57 -0400 Subject: [PATCH 3/6] Update tui/src/state.rs Co-authored-by: Liam <33645555+lj3954@users.noreply.github.com> --- tui/src/state.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index 91688e32..bbee883e 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -761,9 +761,6 @@ impl AppState { fn scroll_tab_down(&mut self) { let len = self.tabs.len(); - if len == 0 { - return; - } let current = self.current_tab.selected().unwrap_or(0); let next = if current + 1 >= len { 0 } else { current + 1 }; self.current_tab.select(Some(next)); From 063d512bc81104d166218295625060477167e747 Mon Sep 17 00:00:00 2001 From: Nyx <144965845+nnyyxxxx@users.noreply.github.com> Date: Wed, 2 Oct 2024 03:17:48 -0400 Subject: [PATCH 4/6] apply changes proposed by adam Co-authored-by: Adam Perkowski --- tui/src/state.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index bbee883e..2554d79f 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -512,9 +512,6 @@ impl AppState { fn scroll_down(&mut self) { let len = self.filter.item_list().len(); - if len == 0 { - return; - } let current = self.selection.selected().unwrap_or(0); let max_index = if self.at_root() { len - 1 } else { len }; let next = if current + 1 > max_index { @@ -527,9 +524,6 @@ impl AppState { fn scroll_up(&mut self) { let len = self.filter.item_list().len(); - if len == 0 { - return; - } let current = self.selection.selected().unwrap_or(0); let max_index = if self.at_root() { len - 1 } else { len }; let next = if current == 0 { max_index } else { current - 1 }; From c12097de0893c59dbf113c1629ba271280f427e7 Mon Sep 17 00:00:00 2001 From: Nyx <144965845+nnyyxxxx@users.noreply.github.com> Date: Wed, 2 Oct 2024 03:19:30 -0400 Subject: [PATCH 5/6] apply changes proposed by adam round 2 Co-authored-by: Adam Perkowski --- tui/src/state.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tui/src/state.rs b/tui/src/state.rs index 2554d79f..9f2a70c1 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -519,6 +519,7 @@ impl AppState { } else { current + 1 }; + self.selection.select(Some(next)); } @@ -527,6 +528,7 @@ impl AppState { let current = self.selection.selected().unwrap_or(0); let max_index = if self.at_root() { len - 1 } else { len }; let next = if current == 0 { max_index } else { current - 1 }; + self.selection.select(Some(next)); } @@ -757,6 +759,7 @@ impl AppState { let len = self.tabs.len(); let current = self.current_tab.selected().unwrap_or(0); let next = if current + 1 >= len { 0 } else { current + 1 }; + self.current_tab.select(Some(next)); self.refresh_tab(); } @@ -765,6 +768,7 @@ impl AppState { let len = self.tabs.len(); let current = self.current_tab.selected().unwrap_or(0); let next = if current == 0 { len - 1 } else { current - 1 }; + self.current_tab.select(Some(next)); self.refresh_tab(); } From 544cbd4035ea685e752ad1a71ac8fb0053b27fa5 Mon Sep 17 00:00:00 2001 From: nyx Date: Wed, 2 Oct 2024 03:39:41 -0400 Subject: [PATCH 6/6] undo changes proposed by adam in scroll up and down funcs --- tui/src/state.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tui/src/state.rs b/tui/src/state.rs index 9f2a70c1..b9690fe5 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -512,6 +512,9 @@ impl AppState { fn scroll_down(&mut self) { let len = self.filter.item_list().len(); + if len == 0 { + return; + } let current = self.selection.selected().unwrap_or(0); let max_index = if self.at_root() { len - 1 } else { len }; let next = if current + 1 > max_index { @@ -525,6 +528,9 @@ impl AppState { fn scroll_up(&mut self) { let len = self.filter.item_list().len(); + if len == 0 { + return; + } let current = self.selection.selected().unwrap_or(0); let max_index = if self.at_root() { len - 1 } else { len }; let next = if current == 0 { max_index } else { current - 1 };