From abf1228a0a837e71d744f5f2881f386387802cc7 Mon Sep 17 00:00:00 2001 From: Adhyan Date: Mon, 2 Sep 2024 10:59:23 -0600 Subject: [PATCH] search now filters the list first --- src/list.rs | 2 +- src/list/state.rs | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/list.rs b/src/list.rs index b41c8913..069cdda0 100644 --- a/src/list.rs +++ b/src/list.rs @@ -38,7 +38,7 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> if is_searching { match curr_key { KeyCode::Esc | KeyCode::Enter => { - is_searching = false; // not sure why rust analyzer thinks this is unused + is_searching = false; list_state.search_query.clear(); continue; } diff --git a/src/list/state.rs b/src/list/state.rs index be05f3b7..8d5bf5b8 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -352,6 +352,27 @@ impl<'a> ListState<'a> { .app_state .exercises() .iter() + .filter_map(|exercise| { + match self.filter() { + Filter::None => { + Some(exercise) + }, + Filter::Done => { + if exercise.done { + Some(exercise) + } else { + None + } + }, + Filter::Pending => { + if !exercise.done { + Some(exercise) + } else { + None + } + } + } + }) .enumerate() .find_map(|(i, s)| { if s.name.contains(self.search_query.as_str()) { @@ -363,8 +384,6 @@ impl<'a> ListState<'a> { match idx { Some(i) => { - // ? do we need this function call? - // let exercise_ind = self.selected_to_exercise_ind(i).unwrap(); let exercise_ind = i; self.scroll_state.set_selected(exercise_ind); self.update_rows();