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