Skip to content

Commit 0488be6

Browse files
committed
Add FuzzySelect::enable_vim_mode
Add configuration option to enable vim mode to `FuzzySelect`
1 parent 190e227 commit 0488be6

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/prompts/fuzzy_select.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct FuzzySelect<'a> {
3838
report: bool,
3939
clear: bool,
4040
highlight_matches: bool,
41+
enable_vim_mode: bool,
4142
max_length: Option<usize>,
4243
theme: &'a dyn Theme,
4344
/// Search string that a fuzzy search with start with.
@@ -118,6 +119,17 @@ impl FuzzySelect<'_> {
118119
self
119120
}
120121

122+
/// Indicated whether to allow the use of vim mode
123+
///
124+
/// Vim mode can be entered by pressing Escape.
125+
/// This then allows the user to navigate using hjkl.
126+
///
127+
/// The default is to disable vim mode.
128+
pub fn enable_vim_mode(mut self, val: bool) -> Self {
129+
self.enable_vim_mode = val;
130+
self
131+
}
132+
121133
/// Sets the maximum number of visible options.
122134
///
123135
/// The default is the height of the terminal minus 2.
@@ -243,7 +255,15 @@ impl FuzzySelect<'_> {
243255
term.flush()?;
244256

245257
match (term.read_key()?, sel, vim_mode) {
246-
(Key::Escape, _, false) => {
258+
(Key::Escape, _, _) if allow_quit && !self.enable_vim_mode => {
259+
if self.clear {
260+
render.clear()?;
261+
term.flush()?;
262+
}
263+
term.show_cursor()?;
264+
return Ok(None);
265+
}
266+
(Key::Escape, _, false) if self.enable_vim_mode => {
247267
vim_mode = true;
248268
}
249269
(Key::Char('i' | 'a'), _, true) => {
@@ -356,6 +376,7 @@ impl<'a> FuzzySelect<'a> {
356376
report: true,
357377
clear: true,
358378
highlight_matches: true,
379+
enable_vim_mode: false,
359380
max_length: None,
360381
theme,
361382
initial_text: "".into(),

0 commit comments

Comments
 (0)