Skip to content

Commit

Permalink
Merge pull request #2098 from frroossst/main
Browse files Browse the repository at this point in the history
Made the list of exercises searchable, ref #2093
  • Loading branch information
mo8it committed Sep 3, 2024
2 parents f696d98 + f463cf8 commit 20616ff
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod state;

fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> {
let mut list_state = ListState::new(app_state, stdout)?;
let mut is_searching = false;

loop {
match event::read().context("Failed to read terminal event")? {
Expand All @@ -32,6 +33,29 @@ 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 {
KeyCode::Esc | KeyCode::Enter => {
is_searching = false;
list_state.search_query.clear();
}
KeyCode::Char(k) => {
list_state.search_query.push(k);
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;
}

match key.code {
KeyCode::Char('q') => return Ok(()),
KeyCode::Down | KeyCode::Char('j') => list_state.select_next(),
Expand Down Expand Up @@ -66,6 +90,10 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
return Ok(());
}
}
KeyCode::Char('s' | '/') => {
list_state.message.push_str("search:|");
is_searching = true;
}
// Redraw to remove the message.
KeyCode::Esc => (),
_ => continue,
Expand Down
2 changes: 1 addition & 1 deletion src/list/scroll_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ScrollState {
self.selected
}

fn set_selected(&mut self, selected: usize) {
pub fn set_selected(&mut self, selected: usize) {
self.selected = Some(selected);
self.update_offset();
}
Expand Down
33 changes: 33 additions & 0 deletions src/list/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct ListState<'a> {
term_width: u16,
term_height: u16,
show_footer: bool,
pub search_query: String,
}

impl<'a> ListState<'a> {
Expand Down Expand Up @@ -76,6 +77,7 @@ impl<'a> ListState<'a> {
term_width: 0,
term_height: 0,
show_footer: true,
search_query: String::new(),
};

slf.set_term_size(width, height);
Expand Down Expand Up @@ -345,6 +347,37 @@ impl<'a> ListState<'a> {
Ok(())
}

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()
.iter()
.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);
}
}
}

// Return `true` if there was something to select.
pub fn selected_to_current_exercise(&mut self) -> Result<bool> {
let Some(selected) = self.scroll_state.selected() else {
Expand Down

0 comments on commit 20616ff

Please sign in to comment.