Skip to content

Commit 47148e7

Browse files
committed
replaced enumerate() with position(); converted select_if_matches_search_query to apply_search_query
1 parent fea917c commit 47148e7

File tree

2 files changed

+31
-45
lines changed

2 files changed

+31
-45
lines changed

src/list.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,12 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
4343
}
4444
KeyCode::Char(k) => {
4545
list_state.search_query.push(k);
46-
list_state.message.push_str("search:");
47-
list_state.message.push_str(&list_state.search_query);
48-
list_state.message.push('|');
49-
50-
list_state.select_if_matches_search_query();
51-
46+
list_state.apply_search_query();
5247
list_state.draw(stdout)?;
5348
}
5449
KeyCode::Backspace => {
5550
list_state.search_query.pop();
56-
list_state.message.push_str("search:");
57-
list_state.message.push_str(&list_state.search_query);
58-
list_state.message.push('|');
59-
60-
list_state.select_if_matches_search_query();
61-
51+
list_state.apply_search_query();
6252
list_state.draw(stdout)?;
6353
}
6454
_ => {}

src/list/state.rs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -347,40 +347,36 @@ impl<'a> ListState<'a> {
347347
Ok(())
348348
}
349349

350-
pub fn select_if_matches_search_query(&mut self) {
350+
pub fn apply_search_query(&mut self) {
351+
self.message.push_str("search:");
352+
self.message.push_str(&self.search_query);
353+
self.message.push('|');
354+
355+
if self.search_query.is_empty() { return; }
356+
351357
let idx = self
352-
.app_state
353-
.exercises()
354-
.iter()
355-
.filter_map(|exercise| {
356-
match self.filter() {
357-
Filter::None => {
358-
Some(exercise)
359-
},
360-
Filter::Done => {
361-
if exercise.done {
362-
Some(exercise)
363-
} else {
364-
None
365-
}
366-
},
367-
Filter::Pending => {
368-
if !exercise.done {
369-
Some(exercise)
370-
} else {
371-
None
372-
}
373-
}
374-
}
375-
})
376-
.enumerate()
377-
.find_map(|(idx, exercise)| {
378-
if exercise.name.contains(self.search_query.as_str()) {
379-
Some(idx)
380-
} else {
381-
None
382-
}
383-
});
358+
.app_state
359+
.exercises()
360+
.iter()
361+
.filter_map(|exercise| {
362+
match self.filter() {
363+
Filter::None => Some(exercise),
364+
Filter::Done if exercise.done => Some(exercise),
365+
Filter::Pending if !exercise.done => Some(exercise),
366+
_ => None,
367+
}
368+
})
369+
.position(|exercise| exercise.name.contains(self.search_query.as_str()));
370+
371+
match idx {
372+
Some(exercise_ind) => {
373+
self.scroll_state.set_selected(exercise_ind);
374+
}
375+
None => {
376+
let msg = String::from(" (not found)");
377+
self.message.push_str(&msg);
378+
}
379+
}
384380

385381
match idx {
386382
Some(x) => {

0 commit comments

Comments
 (0)