Skip to content

Commit 578bd87

Browse files
committed
Implement Searchable for list module search
1 parent 12235df commit 578bd87

File tree

24 files changed

+1700
-1088
lines changed

24 files changed

+1700
-1088
lines changed

src/components/search_bar.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,23 @@ impl SearchBar {
5454
}
5555
}
5656

57-
pub(crate) const fn read_event(&self, event: Event) -> Option<Event> {
57+
pub(crate) fn read_event(&self, event: Event) -> Option<Event> {
5858
match self.state {
5959
State::Deactivated | State::Searching => None,
60-
State::Editing => Some(event),
60+
State::Editing => {
61+
let evt = match event {
62+
Event::Key(KeyEvent {
63+
code: KeyCode::Enter,
64+
modifiers: KeyModifiers::NONE,
65+
}) => Event::from(StandardEvent::SearchFinish),
66+
Event::Key(KeyEvent {
67+
code: KeyCode::Esc,
68+
modifiers: KeyModifiers::NONE,
69+
}) => Event::from(StandardEvent::SearchCancel),
70+
_ => event,
71+
};
72+
Some(evt)
73+
},
6174
}
6275
}
6376

@@ -72,11 +85,7 @@ impl SearchBar {
7285
Event::Standard(StandardEvent::SearchPrevious) => {
7386
SearchBarAction::Previous(String::from(self.editable_line.get_content()))
7487
},
75-
Event::Standard(StandardEvent::SearchFinish)
76-
| Event::Key(KeyEvent {
77-
code: KeyCode::Enter,
78-
modifiers: KeyModifiers::NONE,
79-
}) => {
88+
Event::Standard(StandardEvent::SearchFinish) => {
8089
self.editable_line.set_read_only(true);
8190
self.state = State::Searching;
8291
SearchBarAction::Start(String::from(self.editable_line.get_content()))
@@ -85,10 +94,7 @@ impl SearchBar {
8594
self.state = State::Deactivated;
8695
SearchBarAction::Cancel
8796
},
88-
Event::Key(KeyEvent {
89-
code: KeyCode::Esc,
90-
modifiers: KeyModifiers::NONE,
91-
}) => {
97+
Event::Standard(StandardEvent::SearchCancel) => {
9298
self.reset();
9399
SearchBarAction::Cancel
94100
},
@@ -121,10 +127,6 @@ impl SearchBar {
121127
self.state == State::Editing
122128
}
123129

124-
pub(crate) fn is_searching(&self) -> bool {
125-
self.state == State::Searching
126-
}
127-
128130
pub(crate) fn build_view_line(&self) -> ViewLine {
129131
ViewLine::from(self.editable_line.line_segments())
130132
}

src/components/search_bar/options.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/components/search_bar/tests.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,32 @@ fn read_event_searching() {
8787
}
8888

8989
#[test]
90-
fn read_event_editing() {
90+
fn read_event_editing_other() {
9191
let mut search_bar = SearchBar::new();
9292
search_bar.state = State::Editing;
9393
assert_some_eq!(search_bar.read_event(Event::from('a')), Event::from('a'));
9494
}
9595

96+
#[test]
97+
fn read_event_editing_with_enter() {
98+
let mut search_bar = SearchBar::new();
99+
search_bar.state = State::Editing;
100+
assert_some_eq!(
101+
search_bar.read_event(Event::from(KeyCode::Enter)),
102+
Event::from(StandardEvent::SearchFinish)
103+
);
104+
}
105+
106+
#[test]
107+
fn read_event_editing_ith_esc() {
108+
let mut search_bar = SearchBar::new();
109+
search_bar.state = State::Editing;
110+
assert_some_eq!(
111+
search_bar.read_event(Event::from(KeyCode::Esc)),
112+
Event::from(StandardEvent::SearchCancel)
113+
);
114+
}
115+
96116
#[test]
97117
fn handle_event_inactive() {
98118
let mut search_bar = SearchBar::new();
@@ -137,21 +157,10 @@ fn handle_event_search_finish() {
137157
}
138158

139159
#[test]
140-
fn handle_event_search_finish_with_enter() {
141-
let mut search_bar = SearchBar::new();
142-
search_bar.start_search(Some("foo"));
143-
let event = Event::from(KeyCode::Enter);
144-
assert_eq!(
145-
search_bar.handle_event(event),
146-
SearchBarAction::Start(String::from("foo"))
147-
);
148-
}
149-
150-
#[test]
151-
fn handle_event_search_cancel_with_esc() {
160+
fn handle_event_search_cancel_with_search_cancel() {
152161
let mut search_bar = SearchBar::new();
153162
search_bar.start_search(Some("foo"));
154-
let event = Event::from(KeyCode::Esc);
163+
let event = Event::from(StandardEvent::SearchCancel);
155164
assert_eq!(search_bar.handle_event(event), SearchBarAction::Cancel);
156165
}
157166

@@ -244,17 +253,6 @@ fn is_editing() {
244253
assert!(!search_bar.is_editing());
245254
}
246255

247-
#[test]
248-
fn is_searching() {
249-
let mut search_bar = SearchBar::new();
250-
search_bar.state = State::Editing;
251-
assert!(!search_bar.is_searching());
252-
search_bar.state = State::Deactivated;
253-
assert!(!search_bar.is_searching());
254-
search_bar.state = State::Searching;
255-
assert!(search_bar.is_searching());
256-
}
257-
258256
#[test]
259257
fn build_view_line() {
260258
assert_rendered_output!(

src/input/event_handler.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ impl EventHandler {
120120
e if key_bindings.search_next.contains(&e) => Some(Event::from(StandardEvent::SearchNext)),
121121
e if key_bindings.search_previous.contains(&e) => Some(Event::from(StandardEvent::SearchPrevious)),
122122
e if key_bindings.search_start.contains(&e) => Some(Event::from(StandardEvent::SearchStart)),
123+
Event::Key(KeyEvent {
124+
code: KeyCode::Esc,
125+
modifiers: KeyModifiers::NONE,
126+
}) => Some(Event::from(StandardEvent::SearchCancel)),
127+
Event::Key(KeyEvent {
128+
code: KeyCode::Enter,
129+
modifiers: KeyModifiers::NONE,
130+
}) => Some(Event::from(StandardEvent::SearchFinish)),
123131
_ => None,
124132
}
125133
}
@@ -256,6 +264,8 @@ mod tests {
256264
#[case::search_next(Event::from('n'), Event::from(StandardEvent::SearchNext))]
257265
#[case::search_previous(Event::from('N'), Event::from(StandardEvent::SearchPrevious))]
258266
#[case::search_start(Event::from('/'), Event::from(StandardEvent::SearchStart))]
267+
#[case::esc(Event::from(KeyCode::Esc), Event::from(StandardEvent::SearchCancel))]
268+
#[case::enter(Event::from(KeyCode::Enter), Event::from(StandardEvent::SearchFinish))]
259269
#[case::other(Event::from('a'), Event::from(KeyCode::Null))]
260270
fn search_inputs(#[case] event: Event, #[case] expected: Event) {
261271
let event_handler = EventHandler::new(create_test_keybindings());

src/input/standard_event.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub(crate) enum StandardEvent {
3434
SearchNext,
3535
/// Previous search result meta event.
3636
SearchPrevious,
37+
/// Cancel search mode meta event.
38+
SearchCancel,
3739
/// Finish search mode meta event.
3840
SearchFinish,
3941
/// The abort meta event.

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
#![cfg_attr(allow_unknown_lints, allow(unknown_lints))]
33
#![cfg_attr(allow_unknown_lints, allow(renamed_and_removed_lints))]
44
// enable all rustc's built-in lints
5-
#![deny(
5+
#![warn(
66
future_incompatible,
77
nonstandard_style,
88
rust_2018_compatibility,
99
rust_2018_idioms,
1010
rust_2021_compatibility,
11-
unused,
12-
warnings
11+
unused
1312
)]
1413
// rustc's additional allowed by default lints
1514
#![deny(
@@ -112,6 +111,7 @@
112111
clippy::panic,
113112
clippy::shadow_reuse,
114113
clippy::shadow_unrelated,
114+
clippy::struct_field_names,
115115
clippy::undocumented_unsafe_blocks,
116116
clippy::unimplemented,
117117
clippy::unreachable

0 commit comments

Comments
 (0)