mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Compare commits
5 commits
1e7fc46406
...
f463cf8662
Author | SHA1 | Date | |
---|---|---|---|
f463cf8662 | |||
e9879eac91 | |||
47148e78a3 | |||
fea917c8f2 | |||
948e16e3c7 |
30
src/list.rs
30
src/list.rs
|
@ -1,4 +1,4 @@
|
|||
use anyhow::{Context, Ok, Result};
|
||||
use anyhow::{Context, Result};
|
||||
use crossterm::{
|
||||
cursor,
|
||||
event::{
|
||||
|
@ -40,40 +40,24 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
|
|||
KeyCode::Esc | KeyCode::Enter => {
|
||||
is_searching = false;
|
||||
list_state.search_query.clear();
|
||||
continue;
|
||||
}
|
||||
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)?;
|
||||
continue;
|
||||
}
|
||||
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)?;
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
match key.code {
|
||||
KeyCode::Char('q') => {
|
||||
return Ok(());
|
||||
}
|
||||
KeyCode::Char('q') => return Ok(()),
|
||||
KeyCode::Down | KeyCode::Char('j') => list_state.select_next(),
|
||||
KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(),
|
||||
KeyCode::Home | KeyCode::Char('g') => list_state.select_first(),
|
||||
|
@ -106,7 +90,7 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
|
|||
return Ok(());
|
||||
}
|
||||
}
|
||||
KeyCode::Char('s') | KeyCode::Char('/') => {
|
||||
KeyCode::Char('s' | '/') => {
|
||||
list_state.message.push_str("search:|");
|
||||
is_searching = true;
|
||||
}
|
||||
|
|
|
@ -347,7 +347,15 @@ 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()
|
||||
|
@ -357,24 +365,14 @@ impl<'a> ListState<'a> {
|
|||
Filter::Done => exercise.done,
|
||||
Filter::Pending => !exercise.done,
|
||||
})
|
||||
.enumerate()
|
||||
.find_map(|(i, s)| {
|
||||
if s.name.contains(self.search_query.as_str()) {
|
||||
Some(i)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
.position(|exercise| exercise.name.contains(self.search_query.as_str()));
|
||||
|
||||
match idx {
|
||||
Some(i) => {
|
||||
let exercise_ind = i;
|
||||
Some(exercise_ind) => {
|
||||
self.scroll_state.set_selected(exercise_ind);
|
||||
self.update_rows();
|
||||
}
|
||||
None => {
|
||||
let msg = String::from("[NOT FOUND]") + &self.message.clone();
|
||||
self.message.clear();
|
||||
let msg = String::from(" (not found)");
|
||||
self.message.push_str(&msg);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue