add support for Vim motions in explore (#9966)

related to
- https://github.com/nushell/nushell/issues/7819

# Description
this PR does not quite address
https://github.com/nushell/nushell/issues/7819 because it does not
implement configurable keybindings for `explore` but rather only adds
support for Vim-like motions *out of the box*.

# User-Facing Changes
in *view* and *cursor* modes,
- `h`, `j`, `k` and `l` give standard Qwerty-based Vim motions
- `g` and `G` go to the top and the end respectively
- `u` and `d` scroll up and down

> **Note**
> the bindings do not support the use of modifiers for now, so it's not
`c-u` and `c-d` which scroll pages but rather `u` and `d`

# Tests + Formatting

# After Submitting
This commit is contained in:
Antoine Stevan 2023-08-26 14:48:37 +02:00 committed by GitHub
parent 7ebdced256
commit 3d73287ea4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 37 deletions

View File

@ -742,10 +742,6 @@ fn handle_exit_key_event(key: &KeyEvent) -> bool {
matches!( matches!(
key, key,
KeyEvent { KeyEvent {
code: KeyCode::Char('d'),
modifiers: KeyModifiers::CONTROL,
..
} | KeyEvent {
code: KeyCode::Char('z'), code: KeyCode::Char('z'),
modifiers: KeyModifiers::CONTROL, modifiers: KeyModifiers::CONTROL,
.. ..

View File

@ -4,7 +4,7 @@ use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use crossterm::event::{KeyCode, KeyEvent}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use nu_color_config::{get_color_map, StyleComputer}; use nu_color_config::{get_color_map, StyleComputer};
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, engine::{EngineState, Stack},
@ -451,6 +451,36 @@ impl<'a> RecordLayer<'a> {
} }
fn handle_key_event_view_mode(view: &mut RecordView, key: &KeyEvent) -> Option<Transition> { fn handle_key_event_view_mode(view: &mut RecordView, key: &KeyEvent) -> Option<Transition> {
match key {
KeyEvent {
code: KeyCode::Char('u'),
modifiers: KeyModifiers::CONTROL,
..
}
| KeyEvent {
code: KeyCode::PageUp,
..
} => {
view.get_layer_last_mut().cursor.prev_row_page();
return Some(Transition::Ok);
}
KeyEvent {
code: KeyCode::Char('d'),
modifiers: KeyModifiers::CONTROL,
..
}
| KeyEvent {
code: KeyCode::PageDown,
..
} => {
view.get_layer_last_mut().cursor.next_row_page();
return Some(Transition::Ok);
}
_ => {}
}
match key.code { match key.code {
KeyCode::Esc => { KeyCode::Esc => {
if view.layer_stack.len() > 1 { if view.layer_stack.len() > 1 {
@ -473,42 +503,32 @@ fn handle_key_event_view_mode(view: &mut RecordView, key: &KeyEvent) -> Option<T
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Char('e') => Some(Transition::Cmd(String::from("expand"))), KeyCode::Char('e') => Some(Transition::Cmd(String::from("expand"))),
KeyCode::Up => { KeyCode::Up | KeyCode::Char('k') => {
view.get_layer_last_mut().cursor.prev_row_i(); view.get_layer_last_mut().cursor.prev_row_i();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Down => { KeyCode::Down | KeyCode::Char('j') => {
view.get_layer_last_mut().cursor.next_row_i(); view.get_layer_last_mut().cursor.next_row_i();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Left => { KeyCode::Left | KeyCode::Char('h') => {
view.get_layer_last_mut().cursor.prev_column_i(); view.get_layer_last_mut().cursor.prev_column_i();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Right => { KeyCode::Right | KeyCode::Char('l') => {
view.get_layer_last_mut().cursor.next_column_i(); view.get_layer_last_mut().cursor.next_column_i();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::PageUp => { KeyCode::Home | KeyCode::Char('g') => {
view.get_layer_last_mut().cursor.prev_row_page();
Some(Transition::Ok)
}
KeyCode::PageDown => {
view.get_layer_last_mut().cursor.next_row_page();
Some(Transition::Ok)
}
KeyCode::Home => {
view.get_layer_last_mut().cursor.row_move_to_start(); view.get_layer_last_mut().cursor.row_move_to_start();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::End => { KeyCode::End | KeyCode::Char('G') => {
view.get_layer_last_mut().cursor.row_move_to_end(); view.get_layer_last_mut().cursor.row_move_to_end();
Some(Transition::Ok) Some(Transition::Ok)
@ -518,48 +538,68 @@ fn handle_key_event_view_mode(view: &mut RecordView, key: &KeyEvent) -> Option<T
} }
fn handle_key_event_cursor_mode(view: &mut RecordView, key: &KeyEvent) -> Option<Transition> { fn handle_key_event_cursor_mode(view: &mut RecordView, key: &KeyEvent) -> Option<Transition> {
match key {
KeyEvent {
code: KeyCode::Char('u'),
modifiers: KeyModifiers::CONTROL,
..
}
| KeyEvent {
code: KeyCode::PageUp,
..
} => {
view.get_layer_last_mut().cursor.prev_row_page();
return Some(Transition::Ok);
}
KeyEvent {
code: KeyCode::Char('d'),
modifiers: KeyModifiers::CONTROL,
..
}
| KeyEvent {
code: KeyCode::PageDown,
..
} => {
view.get_layer_last_mut().cursor.next_row_page();
return Some(Transition::Ok);
}
_ => {}
}
match key.code { match key.code {
KeyCode::Esc => { KeyCode::Esc => {
view.set_view_mode(); view.set_view_mode();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Up => { KeyCode::Up | KeyCode::Char('k') => {
view.get_layer_last_mut().cursor.prev_row(); view.get_layer_last_mut().cursor.prev_row();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Down => { KeyCode::Down | KeyCode::Char('j') => {
view.get_layer_last_mut().cursor.next_row(); view.get_layer_last_mut().cursor.next_row();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Left => { KeyCode::Left | KeyCode::Char('h') => {
view.get_layer_last_mut().cursor.prev_column(); view.get_layer_last_mut().cursor.prev_column();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::Right => { KeyCode::Right | KeyCode::Char('l') => {
view.get_layer_last_mut().cursor.next_column(); view.get_layer_last_mut().cursor.next_column();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::PageUp => { KeyCode::Home | KeyCode::Char('g') => {
view.get_layer_last_mut().cursor.prev_row_page();
Some(Transition::Ok)
}
KeyCode::PageDown => {
view.get_layer_last_mut().cursor.next_row_page();
Some(Transition::Ok)
}
KeyCode::Home => {
view.get_layer_last_mut().cursor.row_move_to_start(); view.get_layer_last_mut().cursor.row_move_to_start();
Some(Transition::Ok) Some(Transition::Ok)
} }
KeyCode::End => { KeyCode::End | KeyCode::Char('G') => {
view.get_layer_last_mut().cursor.row_move_to_end(); view.get_layer_last_mut().cursor.row_move_to_end();
Some(Transition::Ok) Some(Transition::Ok)