Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the list of exercises searchable, ref #2093 #2098

Merged
merged 16 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably need to draw here to remove the search query. I will test it.

}
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(()),
mo8it marked this conversation as resolved.
Show resolved Hide resolved
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:|");
frroossst marked this conversation as resolved.
Show resolved Hide resolved
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);
frroossst marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create a String here (it allocates). .push_str(" (not found)") is enough.

}
}
}

// 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