mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-05 01:33:30 +02:00
added a way to search through list, ref #2093
This commit is contained in:
@@ -46,7 +46,7 @@ impl ScrollState {
|
||||
self.selected
|
||||
}
|
||||
|
||||
fn set_selected(&mut self, selected: usize) {
|
||||
pub fn set_selected(&mut self, selected: usize) {
|
||||
self.selected = Some(selected);
|
||||
self.update_offset();
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ pub struct ListState<'a> {
|
||||
term_height: u16,
|
||||
separator_line: Vec<u8>,
|
||||
show_footer: bool,
|
||||
pub search_query: String,
|
||||
}
|
||||
|
||||
impl<'a> ListState<'a> {
|
||||
@@ -78,6 +79,7 @@ impl<'a> ListState<'a> {
|
||||
term_height: 0,
|
||||
separator_line: Vec::new(),
|
||||
show_footer: true,
|
||||
search_query: String::new(),
|
||||
};
|
||||
|
||||
slf.set_term_size(width, height);
|
||||
@@ -356,6 +358,41 @@ impl<'a> ListState<'a> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn select_if_matches_search_query(&mut self) {
|
||||
eprintln!("search query: {:?}", self.search_query);
|
||||
|
||||
let idx = self
|
||||
.app_state
|
||||
.exercises()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find_map(|(i, s)| {
|
||||
if s.name.contains(self.search_query.as_str()) {
|
||||
Some(i)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
eprintln!("idx: {:?}", idx);
|
||||
|
||||
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);
|
||||
eprintln!("exercise_ind: {:?}", exercise_ind);
|
||||
self.update_rows();
|
||||
}
|
||||
None => {
|
||||
let msg = String::from("[NOT FOUND]") + &self.message.clone();
|
||||
self.message.clear();
|
||||
self.message.push_str(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Return `true` if there was something to select.
|
||||
pub fn selected_to_current_exercise(&mut self) -> Result<bool> {
|
||||
|
||||
Reference in New Issue
Block a user