From 3d73287ea43db2e4430ba2be8e62332e4e232f2f Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Sat, 26 Aug 2023 14:48:37 +0200 Subject: [PATCH] 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 --- crates/nu-explore/src/pager/mod.rs | 4 - crates/nu-explore/src/views/record/mod.rs | 106 +++++++++++++++------- 2 files changed, 73 insertions(+), 37 deletions(-) diff --git a/crates/nu-explore/src/pager/mod.rs b/crates/nu-explore/src/pager/mod.rs index 41d4f3510..e71e8ca5d 100644 --- a/crates/nu-explore/src/pager/mod.rs +++ b/crates/nu-explore/src/pager/mod.rs @@ -742,10 +742,6 @@ fn handle_exit_key_event(key: &KeyEvent) -> bool { matches!( key, KeyEvent { - code: KeyCode::Char('d'), - modifiers: KeyModifiers::CONTROL, - .. - } | KeyEvent { code: KeyCode::Char('z'), modifiers: KeyModifiers::CONTROL, .. diff --git a/crates/nu-explore/src/views/record/mod.rs b/crates/nu-explore/src/views/record/mod.rs index 20e5b47bb..cfd640aca 100644 --- a/crates/nu-explore/src/views/record/mod.rs +++ b/crates/nu-explore/src/views/record/mod.rs @@ -4,7 +4,7 @@ use std::borrow::Cow; 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_protocol::{ engine::{EngineState, Stack}, @@ -451,6 +451,36 @@ impl<'a> RecordLayer<'a> { } fn handle_key_event_view_mode(view: &mut RecordView, key: &KeyEvent) -> Option { + 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 { KeyCode::Esc => { if view.layer_stack.len() > 1 { @@ -473,42 +503,32 @@ fn handle_key_event_view_mode(view: &mut RecordView, key: &KeyEvent) -> Option Some(Transition::Cmd(String::from("expand"))), - KeyCode::Up => { + KeyCode::Up | KeyCode::Char('k') => { view.get_layer_last_mut().cursor.prev_row_i(); Some(Transition::Ok) } - KeyCode::Down => { + KeyCode::Down | KeyCode::Char('j') => { view.get_layer_last_mut().cursor.next_row_i(); Some(Transition::Ok) } - KeyCode::Left => { + KeyCode::Left | KeyCode::Char('h') => { view.get_layer_last_mut().cursor.prev_column_i(); Some(Transition::Ok) } - KeyCode::Right => { + KeyCode::Right | KeyCode::Char('l') => { view.get_layer_last_mut().cursor.next_column_i(); Some(Transition::Ok) } - KeyCode::PageUp => { - 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 => { + KeyCode::Home | KeyCode::Char('g') => { view.get_layer_last_mut().cursor.row_move_to_start(); Some(Transition::Ok) } - KeyCode::End => { + KeyCode::End | KeyCode::Char('G') => { view.get_layer_last_mut().cursor.row_move_to_end(); Some(Transition::Ok) @@ -518,48 +538,68 @@ fn handle_key_event_view_mode(view: &mut RecordView, key: &KeyEvent) -> Option Option { + 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 { KeyCode::Esc => { view.set_view_mode(); Some(Transition::Ok) } - KeyCode::Up => { + KeyCode::Up | KeyCode::Char('k') => { view.get_layer_last_mut().cursor.prev_row(); Some(Transition::Ok) } - KeyCode::Down => { + KeyCode::Down | KeyCode::Char('j') => { view.get_layer_last_mut().cursor.next_row(); Some(Transition::Ok) } - KeyCode::Left => { + KeyCode::Left | KeyCode::Char('h') => { view.get_layer_last_mut().cursor.prev_column(); Some(Transition::Ok) } - KeyCode::Right => { + KeyCode::Right | KeyCode::Char('l') => { view.get_layer_last_mut().cursor.next_column(); Some(Transition::Ok) } - KeyCode::PageUp => { - 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 => { + KeyCode::Home | KeyCode::Char('g') => { view.get_layer_last_mut().cursor.row_move_to_start(); Some(Transition::Ok) } - KeyCode::End => { + KeyCode::End | KeyCode::Char('G') => { view.get_layer_last_mut().cursor.row_move_to_end(); Some(Transition::Ok)