Skip to content

Commit da8b3d1

Browse files
committed
Final touches to searching
1 parent 20616ff commit da8b3d1

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/list.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,24 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
3333

3434
list_state.message.clear();
3535

36-
let curr_key = key.code;
37-
3836
if is_searching {
39-
match curr_key {
37+
match key.code {
4038
KeyCode::Esc | KeyCode::Enter => {
4139
is_searching = false;
4240
list_state.search_query.clear();
4341
}
44-
KeyCode::Char(k) => {
45-
list_state.search_query.push(k);
42+
KeyCode::Char(c) => {
43+
list_state.search_query.push(c);
4644
list_state.apply_search_query();
47-
list_state.draw(stdout)?;
4845
}
4946
KeyCode::Backspace => {
5047
list_state.search_query.pop();
5148
list_state.apply_search_query();
52-
list_state.draw(stdout)?;
5349
}
54-
_ => {}
50+
_ => continue,
5551
}
52+
53+
list_state.draw(stdout)?;
5654
continue;
5755
}
5856

@@ -91,8 +89,8 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
9189
}
9290
}
9391
KeyCode::Char('s' | '/') => {
94-
list_state.message.push_str("search:|");
9592
is_searching = true;
93+
list_state.apply_search_query();
9694
}
9795
// Redraw to remove the message.
9896
KeyCode::Esc => (),

src/list/state.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ pub enum Filter {
3737
pub struct ListState<'a> {
3838
/// Footer message to be displayed if not empty.
3939
pub message: String,
40+
pub search_query: String,
4041
app_state: &'a mut AppState,
4142
scroll_state: ScrollState,
4243
name_col_padding: Vec<u8>,
4344
filter: Filter,
4445
term_width: u16,
4546
term_height: u16,
4647
show_footer: bool,
47-
pub search_query: String,
4848
}
4949

5050
impl<'a> ListState<'a> {
@@ -69,6 +69,7 @@ impl<'a> ListState<'a> {
6969

7070
let mut slf = Self {
7171
message: String::with_capacity(128),
72+
search_query: String::new(),
7273
app_state,
7374
scroll_state,
7475
name_col_padding,
@@ -77,7 +78,6 @@ impl<'a> ListState<'a> {
7778
term_width: 0,
7879
term_height: 0,
7980
show_footer: true,
80-
search_query: String::new(),
8181
};
8282

8383
slf.set_term_size(width, height);
@@ -356,25 +356,20 @@ impl<'a> ListState<'a> {
356356
return;
357357
}
358358

359-
let idx = self
359+
let ind = self
360360
.app_state
361361
.exercises()
362362
.iter()
363-
.filter(|exercise| match self.filter() {
363+
.filter(|exercise| match self.filter {
364364
Filter::None => true,
365365
Filter::Done => exercise.done,
366366
Filter::Pending => !exercise.done,
367367
})
368368
.position(|exercise| exercise.name.contains(self.search_query.as_str()));
369369

370-
match idx {
371-
Some(exercise_ind) => {
372-
self.scroll_state.set_selected(exercise_ind);
373-
}
374-
None => {
375-
let msg = String::from(" (not found)");
376-
self.message.push_str(&msg);
377-
}
370+
match ind {
371+
Some(exercise_ind) => self.scroll_state.set_selected(exercise_ind),
372+
None => self.message.push_str(" (not found)"),
378373
}
379374
}
380375

0 commit comments

Comments
 (0)