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::{
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;
}
}
}
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;
}

View file

@ -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);
}
}