From e5ed11528855f6dddc5759df3426ff1296aba87e Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 4 Sep 2024 01:20:48 +0200 Subject: [PATCH] Match filter once --- src/list/state.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/list/state.rs b/src/list/state.rs index f932fab..468049a 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -356,16 +356,17 @@ impl<'a> ListState<'a> { return; } - let ind = self - .app_state - .exercises() - .iter() - .filter(|exercise| match self.filter { - Filter::None => true, - Filter::Done => exercise.done, - Filter::Pending => !exercise.done, - }) - .position(|exercise| exercise.name.contains(self.search_query.as_str())); + let is_search_result = |exercise: &Exercise| exercise.name.contains(&self.search_query); + let mut iter = self.app_state.exercises().iter(); + let ind = match self.filter { + Filter::None => iter.position(is_search_result), + Filter::Done => iter + .filter(|exercise| exercise.done) + .position(is_search_result), + Filter::Pending => iter + .filter(|exercise| !exercise.done) + .position(is_search_result), + }; match ind { Some(exercise_ind) => self.scroll_state.set_selected(exercise_ind),