mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 00:00:05 +03:00
replaced enumerate() with position(); converted select_if_matches_search_query to apply_search_query
This commit is contained in:
parent
fea917c8f2
commit
47148e78a3
14
src/list.rs
14
src/list.rs
|
@ -43,22 +43,12 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
|
|||
}
|
||||
KeyCode::Char(k) => {
|
||||
list_state.search_query.push(k);
|
||||
list_state.message.push_str("search:");
|
||||
list_state.message.push_str(&list_state.search_query);
|
||||
list_state.message.push('|');
|
||||
|
||||
list_state.select_if_matches_search_query();
|
||||
|
||||
list_state.apply_search_query();
|
||||
list_state.draw(stdout)?;
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
list_state.search_query.pop();
|
||||
list_state.message.push_str("search:");
|
||||
list_state.message.push_str(&list_state.search_query);
|
||||
list_state.message.push('|');
|
||||
|
||||
list_state.select_if_matches_search_query();
|
||||
|
||||
list_state.apply_search_query();
|
||||
list_state.draw(stdout)?;
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
@ -347,40 +347,36 @@ impl<'a> ListState<'a> {
|
|||
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
|
||||
.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(|(idx, exercise)| {
|
||||
if exercise.name.contains(self.search_query.as_str()) {
|
||||
Some(idx)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
.app_state
|
||||
.exercises()
|
||||
.iter()
|
||||
.filter_map(|exercise| {
|
||||
match self.filter() {
|
||||
Filter::None => Some(exercise),
|
||||
Filter::Done if exercise.done => Some(exercise),
|
||||
Filter::Pending if !exercise.done => Some(exercise),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.position(|exercise| exercise.name.contains(self.search_query.as_str()));
|
||||
|
||||
match idx {
|
||||
Some(exercise_ind) => {
|
||||
self.scroll_state.set_selected(exercise_ind);
|
||||
}
|
||||
None => {
|
||||
let msg = String::from(" (not found)");
|
||||
self.message.push_str(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
match idx {
|
||||
Some(x) => {
|
||||
|
|
Loading…
Reference in a new issue