Skip to content

Commit

Permalink
feat: suppport vi keybindings (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Oct 24, 2023
1 parent b5e1460 commit f4160ff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub struct Config {
pub connect_timeout: usize,
/// Automatically copy the last output to the clipboard
pub auto_copy: bool,
/// Use vi keybindings, overriding the default Emacs keybindings
pub vi_keybindings: bool,
/// Predefined roles
#[serde(skip)]
pub roles: Vec<Role>,
Expand Down Expand Up @@ -102,6 +104,7 @@ impl Default for Config {
light_theme: false,
connect_timeout: 10,
auto_copy: false,
vi_keybindings: false,
roles: vec![],
role: None,
conversation: None,
Expand Down Expand Up @@ -353,6 +356,7 @@ impl Config {
("light_theme", self.light_theme.to_string()),
("connect_timeout", self.connect_timeout.to_string()),
("dry_run", self.dry_run.to_string()),
("vi_keybindings", self.vi_keybindings.to_string()),
];
let mut output = String::new();
for (name, value) in items {
Expand Down
28 changes: 21 additions & 7 deletions src/repl/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use crate::config::{Config, SharedConfig};

use anyhow::{Context, Result};
use reedline::{
default_emacs_keybindings, ColumnarMenu, DefaultCompleter, Emacs, FileBackedHistory, KeyCode,
KeyModifiers, Keybindings, Reedline, ReedlineEvent, ReedlineMenu,
default_emacs_keybindings, default_vi_insert_keybindings, default_vi_normal_keybindings,
ColumnarMenu, DefaultCompleter, EditMode, Emacs, FileBackedHistory, KeyCode, KeyModifiers,
Keybindings, Reedline, ReedlineEvent, ReedlineMenu, Vi,
};

const MENU_NAME: &str = "completion_menu";
Expand All @@ -26,10 +27,22 @@ impl Repl {

let completer = Self::create_completer(&config, &commands);
let highlighter = ReplHighlighter::new(config.clone(), commands);
let keybindings = Self::create_keybindings();
let history = Self::create_history()?;
let menu = Self::create_menu();
let edit_mode = Box::new(Emacs::new(keybindings));
let edit_mode: Box<dyn EditMode> = if config.read().vi_keybindings {
let mut normal_keybindings = default_vi_normal_keybindings();
let mut insert_keybindings = default_vi_insert_keybindings();
Self::add_menu_keybindings(&mut normal_keybindings);
Self::add_menu_keybindings(&mut insert_keybindings);
Self::add_clear_keybindings(&mut normal_keybindings);
Self::add_clear_keybindings(&mut insert_keybindings);
Box::new(Vi::new(insert_keybindings, normal_keybindings))
} else {
let mut keybindings = default_emacs_keybindings();
Self::add_menu_keybindings(&mut keybindings);
Self::add_clear_keybindings(&mut keybindings);
Box::new(Emacs::new(keybindings))
};
let mut editor = Reedline::create()
.with_completer(Box::new(completer))
.with_highlighter(Box::new(highlighter))
Expand All @@ -54,8 +67,7 @@ impl Repl {
completer
}

fn create_keybindings() -> Keybindings {
let mut keybindings = default_emacs_keybindings();
fn add_menu_keybindings(keybindings: &mut Keybindings) {
keybindings.add_binding(
KeyModifiers::NONE,
KeyCode::Tab,
Expand All @@ -64,12 +76,14 @@ impl Repl {
ReedlineEvent::MenuNext,
]),
);
}

fn add_clear_keybindings(keybindings: &mut Keybindings) {
keybindings.add_binding(
KeyModifiers::CONTROL,
KeyCode::Char('l'),
ReedlineEvent::ExecuteHostCommand(".clear screen".into()),
);
keybindings
}

fn create_menu() -> ReedlineMenu {
Expand Down

0 comments on commit f4160ff

Please sign in to comment.