replaced enumerate() with position(); converted select_if_matches_search_query to apply_search_query

This commit is contained in:
Adhyan 2024-09-03 15:03:25 -06:00
parent fea917c8f2
commit 47148e78a3
2 changed files with 31 additions and 45 deletions

View file

@ -43,22 +43,12 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
} }
KeyCode::Char(k) => { KeyCode::Char(k) => {
list_state.search_query.push(k); list_state.search_query.push(k);
list_state.message.push_str("search:"); list_state.apply_search_query();
list_state.message.push_str(&list_state.search_query);
list_state.message.push('|');
list_state.select_if_matches_search_query();
list_state.draw(stdout)?; list_state.draw(stdout)?;
} }
KeyCode::Backspace => { KeyCode::Backspace => {
list_state.search_query.pop(); list_state.search_query.pop();
list_state.message.push_str("search:"); list_state.apply_search_query();
list_state.message.push_str(&list_state.search_query);
list_state.message.push('|');
list_state.select_if_matches_search_query();
list_state.draw(stdout)?; list_state.draw(stdout)?;
} }
_ => {} _ => {}

View file

@ -347,40 +347,36 @@ impl<'a> ListState<'a> {
Ok(()) Ok(())
} }
pub fn select_if_matches_search_query(&mut self) { pub fn apply_search_query(&mut self) {
self.message.push_str("search:");
self.message.push_str(&self.search_query);
self.message.push('|');
if self.search_query.is_empty() { return; }
let idx = self let idx = self
.app_state .app_state
.exercises() .exercises()
.iter() .iter()
.filter_map(|exercise| { .filter_map(|exercise| {
match self.filter() { match self.filter() {
Filter::None => { Filter::None => Some(exercise),
Some(exercise) Filter::Done if exercise.done => Some(exercise),
}, Filter::Pending if !exercise.done => Some(exercise),
Filter::Done => { _ => None,
if exercise.done { }
Some(exercise) })
} else { .position(|exercise| exercise.name.contains(self.search_query.as_str()));
None
} match idx {
}, Some(exercise_ind) => {
Filter::Pending => { self.scroll_state.set_selected(exercise_ind);
if !exercise.done { }
Some(exercise) None => {
} else { let msg = String::from(" (not found)");
None self.message.push_str(&msg);
} }
} }
}
})
.enumerate()
.find_map(|(idx, exercise)| {
if exercise.name.contains(self.search_query.as_str()) {
Some(idx)
} else {
None
}
});
match idx { match idx {
Some(x) => { Some(x) => {