Skip to content

Commit

Permalink
refactor(tui): deduplicate scroll logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyyxxxx committed Feb 7, 2025
1 parent ad75771 commit f28e085
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ enum SelectedItem {
None,
}

enum ScrollDir {
Up,
Down,
}

impl AppState {
pub fn new(args: Args) -> Self {
#[cfg(unix)]
Expand Down Expand Up @@ -608,7 +613,7 @@ impl AppState {
true
}

fn scroll_down(&mut self) {
fn scroll(&mut self, direction: ScrollDir) {
let Some(selected) = self.selection.selected() else {
return;
};
Expand All @@ -622,34 +627,21 @@ impl AppState {
return;
};

let next_selection = if selected >= list_len - 1 {
0
} else {
selected + 1
let next_selection = match direction {
ScrollDir::Up if selected == 0 => list_len - 1,
ScrollDir::Down if selected >= list_len - 1 => 0,
ScrollDir::Up => selected - 1,
ScrollDir::Down => selected + 1,
};
self.selection.select(Some(next_selection));
}

fn scroll_up(&mut self) {
let Some(selected) = self.selection.selected() else {
return;
};
let list_len = if !self.at_root() {
self.filter.item_list().len() + 1
} else {
self.filter.item_list().len()
};

if list_len == 0 {
return;
};
self.scroll(ScrollDir::Up)
}

let next_selection = if selected == 0 {
list_len - 1
} else {
selected - 1
};
self.selection.select(Some(next_selection));
fn scroll_down(&mut self) {
self.scroll(ScrollDir::Down)
}

fn toggle_multi_select(&mut self) {
Expand Down

0 comments on commit f28e085

Please sign in to comment.