Skip to content

Commit 190e227

Browse files
committed
Add vim mode to FuzzySelect
1 parent d77d5cd commit 190e227

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/prompts/fuzzy_select.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ impl FuzzySelect<'_> {
209209

210210
term.hide_cursor()?;
211211

212+
let mut vim_mode = false;
213+
212214
loop {
213215
render.clear()?;
214216
render.fuzzy_select_prompt(self.prompt.as_str(), &search_term, position)?;
@@ -240,16 +242,16 @@ impl FuzzySelect<'_> {
240242
}
241243
term.flush()?;
242244

243-
match (term.read_key()?, sel) {
244-
(Key::Escape, _) if allow_quit => {
245-
if self.clear {
246-
render.clear()?;
247-
term.flush()?;
248-
}
249-
term.show_cursor()?;
250-
return Ok(None);
245+
match (term.read_key()?, sel, vim_mode) {
246+
(Key::Escape, _, false) => {
247+
vim_mode = true;
248+
}
249+
(Key::Char('i' | 'a'), _, true) => {
250+
vim_mode = false;
251251
}
252-
(Key::ArrowUp | Key::BackTab, _) if !filtered_list.is_empty() => {
252+
(Key::ArrowUp | Key::BackTab, _, _) | (Key::Char('k'), _, true)
253+
if !filtered_list.is_empty() =>
254+
{
253255
if sel == Some(0) {
254256
starting_row =
255257
filtered_list.len().max(visible_term_rows) - visible_term_rows;
@@ -266,7 +268,9 @@ impl FuzzySelect<'_> {
266268
};
267269
term.flush()?;
268270
}
269-
(Key::ArrowDown | Key::Tab, _) if !filtered_list.is_empty() => {
271+
(Key::ArrowDown | Key::Tab, _, _) | (Key::Char('j'), _, true)
272+
if !filtered_list.is_empty() =>
273+
{
270274
sel = match sel {
271275
None => Some(0),
272276
Some(sel) => {
@@ -280,15 +284,17 @@ impl FuzzySelect<'_> {
280284
}
281285
term.flush()?;
282286
}
283-
(Key::ArrowLeft, _) if position > 0 => {
287+
(Key::ArrowLeft, _, _) | (Key::Char('h'), _, true) if position > 0 => {
284288
position -= 1;
285289
term.flush()?;
286290
}
287-
(Key::ArrowRight, _) if position < search_term.len() => {
291+
(Key::ArrowRight, _, _) | (Key::Char('l'), _, true)
292+
if position < search_term.len() =>
293+
{
288294
position += 1;
289295
term.flush()?;
290296
}
291-
(Key::Enter, Some(sel)) if !filtered_list.is_empty() => {
297+
(Key::Enter, Some(sel), _) if !filtered_list.is_empty() => {
292298
if self.clear {
293299
render.clear()?;
294300
}
@@ -305,17 +311,18 @@ impl FuzzySelect<'_> {
305311
term.show_cursor()?;
306312
return Ok(sel_string_pos_in_items);
307313
}
308-
(Key::Backspace, _) if position > 0 => {
314+
(Key::Backspace, _, _) if position > 0 => {
309315
position -= 1;
310316
search_term.remove(position);
311317
term.flush()?;
312318
}
313-
(Key::Char(chr), _) if !chr.is_ascii_control() => {
319+
(Key::Char(chr), _, _) if !chr.is_ascii_control() => {
314320
search_term.insert(position, chr);
315321
position += 1;
316322
term.flush()?;
317323
sel = Some(0);
318324
starting_row = 0;
325+
vim_mode = false;
319326
}
320327

321328
_ => {}

0 commit comments

Comments
 (0)