Compare commits

...

5 commits

2 changed files with 19 additions and 37 deletions

View file

@ -1,4 +1,4 @@
use anyhow::{Context, Ok, Result}; use anyhow::{Context, Result};
use crossterm::{ use crossterm::{
cursor, cursor,
event::{ event::{
@ -40,40 +40,24 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
KeyCode::Esc | KeyCode::Enter => { KeyCode::Esc | KeyCode::Enter => {
is_searching = false; is_searching = false;
list_state.search_query.clear(); list_state.search_query.clear();
continue;
} }
KeyCode::Char(k) => { KeyCode::Char(k) => {
list_state.search_query.push(k); list_state.search_query.push(k);
list_state.message.push_str("search:"); list_state.apply_search_query();
list_state.message.push_str(&list_state.search_query);
list_state.message.push('|');
list_state.select_if_matches_search_query();
list_state.draw(stdout)?; list_state.draw(stdout)?;
continue;
} }
KeyCode::Backspace => { KeyCode::Backspace => {
list_state.search_query.pop(); list_state.search_query.pop();
list_state.message.push_str("search:"); list_state.apply_search_query();
list_state.message.push_str(&list_state.search_query);
list_state.message.push('|');
list_state.select_if_matches_search_query();
list_state.draw(stdout)?; list_state.draw(stdout)?;
}
_ => {}
}
continue; continue;
} }
_ => {
continue;
}
}
}
match key.code { match key.code {
KeyCode::Char('q') => { KeyCode::Char('q') => return Ok(()),
return Ok(());
}
KeyCode::Down | KeyCode::Char('j') => list_state.select_next(), KeyCode::Down | KeyCode::Char('j') => list_state.select_next(),
KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(), KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(),
KeyCode::Home | KeyCode::Char('g') => list_state.select_first(), 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(()); return Ok(());
} }
} }
KeyCode::Char('s') | KeyCode::Char('/') => { KeyCode::Char('s' | '/') => {
list_state.message.push_str("search:|"); list_state.message.push_str("search:|");
is_searching = true; is_searching = true;
} }

View file

@ -347,7 +347,15 @@ impl<'a> ListState<'a> {
Ok(()) 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 let idx = self
.app_state .app_state
.exercises() .exercises()
@ -357,24 +365,14 @@ impl<'a> ListState<'a> {
Filter::Done => exercise.done, Filter::Done => exercise.done,
Filter::Pending => !exercise.done, Filter::Pending => !exercise.done,
}) })
.enumerate() .position(|exercise| exercise.name.contains(self.search_query.as_str()));
.find_map(|(i, s)| {
if s.name.contains(self.search_query.as_str()) {
Some(i)
} else {
None
}
});
match idx { match idx {
Some(i) => { Some(exercise_ind) => {
let exercise_ind = i;
self.scroll_state.set_selected(exercise_ind); self.scroll_state.set_selected(exercise_ind);
self.update_rows();
} }
None => { None => {
let msg = String::from("[NOT FOUND]") + &self.message.clone(); let msg = String::from(" (not found)");
self.message.clear();
self.message.push_str(&msg); self.message.push_str(&msg);
} }
} }