Skip to content

Commit

Permalink
feat: Wrap-around selection for TUI. (#9409)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyshew authored Nov 8, 2024
1 parent cdc7ec9 commit bb9871d
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions crates/turborepo-ui/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,24 @@ impl<W> App<W> {
#[tracing::instrument(skip(self))]
pub fn next(&mut self) {
let num_rows = self.tasks_by_status.count_all();
let next_index = (self.selected_task_index + 1).clamp(0, num_rows - 1);
self.selected_task_index = next_index;
self.scroll.select(Some(next_index));
self.has_user_scrolled = true;
if num_rows > 0 {
self.selected_task_index = (self.selected_task_index + 1) % num_rows;
self.scroll.select(Some(self.selected_task_index));
self.has_user_scrolled = true;
}
}

#[tracing::instrument(skip(self))]
pub fn previous(&mut self) {
let i = match self.selected_task_index {
0 => 0,
i => i - 1,
};
self.selected_task_index = i;
self.scroll.select(Some(i));
self.has_user_scrolled = true;
let num_rows = self.tasks_by_status.count_all();
if num_rows > 0 {
self.selected_task_index = self
.selected_task_index
.checked_sub(1)
.unwrap_or(num_rows - 1);
self.scroll.select(Some(self.selected_task_index));
self.has_user_scrolled = true;
}
}

#[tracing::instrument(skip_all)]
Expand Down Expand Up @@ -886,6 +889,8 @@ mod test {
app.next();
assert_eq!(app.scroll.selected(), Some(2), "scroll moves forwards");
app.next();
assert_eq!(app.scroll.selected(), Some(0), "scroll wraps");
app.previous();
assert_eq!(app.scroll.selected(), Some(2), "scroll stays in bounds");
}

Expand Down

0 comments on commit bb9871d

Please sign in to comment.