Skip to content

Commit a1a157c

Browse files
authored
fix(tui): don't panic when search result is empty and up is pressed (#2395)
In the event there are no results and up is pressed, saturating_sub() should be used to avoid underflowing the usize. This was already present on scroll_down(). fixes #2393
1 parent c7447e8 commit a1a157c

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

crates/atuin/src/command/client/search/interactive.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ impl State {
534534

535535
fn scroll_up(&mut self, scroll_len: usize) {
536536
let i = self.results_state.selected() + scroll_len;
537-
self.results_state.select(i.min(self.results_len - 1));
537+
self.results_state
538+
.select(i.min(self.results_len.saturating_sub(1)));
538539
}
539540

540541
#[allow(clippy::cast_possible_truncation)]
@@ -1211,8 +1212,15 @@ fn set_clipboard(_s: String) {}
12111212

12121213
#[cfg(test)]
12131214
mod tests {
1215+
use atuin_client::database::Context;
12141216
use atuin_client::history::History;
1215-
use atuin_client::settings::{Preview, PreviewStrategy, Settings};
1217+
use atuin_client::settings::{
1218+
FilterMode, KeymapMode, Preview, PreviewStrategy, SearchMode, Settings,
1219+
};
1220+
use time::OffsetDateTime;
1221+
1222+
use crate::command::client::search::engines::{self, SearchState};
1223+
use crate::command::client::search::history_list::ListState;
12161224

12171225
use super::State;
12181226

@@ -1368,4 +1376,38 @@ mod tests {
13681376
assert_eq!(preview_static_limit_at_4, 4 + border_space);
13691377
assert_eq!(settings_preview_fixed, 15 + border_space);
13701378
}
1379+
1380+
// Test when there's no results, scrolling up or down doesn't underflow
1381+
#[test]
1382+
fn state_scroll_up_underflow() {
1383+
let mut state = State {
1384+
history_count: 0,
1385+
update_needed: None,
1386+
results_state: ListState::default(),
1387+
switched_search_mode: false,
1388+
search_mode: SearchMode::Fuzzy,
1389+
results_len: 0,
1390+
accept: false,
1391+
keymap_mode: KeymapMode::Auto,
1392+
prefix: false,
1393+
current_cursor: None,
1394+
tab_index: 0,
1395+
search: SearchState {
1396+
input: String::new().into(),
1397+
filter_mode: FilterMode::Directory,
1398+
context: Context {
1399+
session: String::new(),
1400+
cwd: String::new(),
1401+
hostname: String::new(),
1402+
host_id: String::new(),
1403+
git_root: None,
1404+
},
1405+
},
1406+
engine: engines::engine(SearchMode::Fuzzy),
1407+
now: Box::new(OffsetDateTime::now_utc),
1408+
};
1409+
1410+
state.scroll_up(1);
1411+
state.scroll_down(1);
1412+
}
13711413
}

0 commit comments

Comments
 (0)