From bb9871d8a7605f1ca97fd09770136b5e5175ee23 Mon Sep 17 00:00:00 2001 From: Anthony Shew Date: Fri, 8 Nov 2024 07:25:37 -0700 Subject: [PATCH] feat: Wrap-around selection for TUI. (#9409) --- crates/turborepo-ui/src/tui/app.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/turborepo-ui/src/tui/app.rs b/crates/turborepo-ui/src/tui/app.rs index 282867ea93af7..4fe0faaf06cc8 100644 --- a/crates/turborepo-ui/src/tui/app.rs +++ b/crates/turborepo-ui/src/tui/app.rs @@ -142,21 +142,24 @@ impl App { #[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)] @@ -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"); }