mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 15:10:26 +00:00
Final touches to searching
This commit is contained in:
parent
20616ff954
commit
da8b3d143a
2 changed files with 14 additions and 21 deletions
16
src/list.rs
16
src/list.rs
|
@ -33,26 +33,24 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
|
|||
|
||||
list_state.message.clear();
|
||||
|
||||
let curr_key = key.code;
|
||||
|
||||
if is_searching {
|
||||
match curr_key {
|
||||
match key.code {
|
||||
KeyCode::Esc | KeyCode::Enter => {
|
||||
is_searching = false;
|
||||
list_state.search_query.clear();
|
||||
}
|
||||
KeyCode::Char(k) => {
|
||||
list_state.search_query.push(k);
|
||||
KeyCode::Char(c) => {
|
||||
list_state.search_query.push(c);
|
||||
list_state.apply_search_query();
|
||||
list_state.draw(stdout)?;
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
list_state.search_query.pop();
|
||||
list_state.apply_search_query();
|
||||
list_state.draw(stdout)?;
|
||||
}
|
||||
_ => {}
|
||||
_ => continue,
|
||||
}
|
||||
|
||||
list_state.draw(stdout)?;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -91,8 +89,8 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
|
|||
}
|
||||
}
|
||||
KeyCode::Char('s' | '/') => {
|
||||
list_state.message.push_str("search:|");
|
||||
is_searching = true;
|
||||
list_state.apply_search_query();
|
||||
}
|
||||
// Redraw to remove the message.
|
||||
KeyCode::Esc => (),
|
||||
|
|
|
@ -37,6 +37,7 @@ pub enum Filter {
|
|||
pub struct ListState<'a> {
|
||||
/// Footer message to be displayed if not empty.
|
||||
pub message: String,
|
||||
pub search_query: String,
|
||||
app_state: &'a mut AppState,
|
||||
scroll_state: ScrollState,
|
||||
name_col_padding: Vec<u8>,
|
||||
|
@ -44,7 +45,6 @@ pub struct ListState<'a> {
|
|||
term_width: u16,
|
||||
term_height: u16,
|
||||
show_footer: bool,
|
||||
pub search_query: String,
|
||||
}
|
||||
|
||||
impl<'a> ListState<'a> {
|
||||
|
@ -69,6 +69,7 @@ impl<'a> ListState<'a> {
|
|||
|
||||
let mut slf = Self {
|
||||
message: String::with_capacity(128),
|
||||
search_query: String::new(),
|
||||
app_state,
|
||||
scroll_state,
|
||||
name_col_padding,
|
||||
|
@ -77,7 +78,6 @@ impl<'a> ListState<'a> {
|
|||
term_width: 0,
|
||||
term_height: 0,
|
||||
show_footer: true,
|
||||
search_query: String::new(),
|
||||
};
|
||||
|
||||
slf.set_term_size(width, height);
|
||||
|
@ -356,25 +356,20 @@ impl<'a> ListState<'a> {
|
|||
return;
|
||||
}
|
||||
|
||||
let idx = self
|
||||
let ind = self
|
||||
.app_state
|
||||
.exercises()
|
||||
.iter()
|
||||
.filter(|exercise| match self.filter() {
|
||||
.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()));
|
||||
|
||||
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 ind {
|
||||
Some(exercise_ind) => self.scroll_state.set_selected(exercise_ind),
|
||||
None => self.message.push_str(" (not found)"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue