From aa43612d91687634b3e6a317e714e6eccb62b88a Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 27 Oct 2023 21:22:24 +0800 Subject: [PATCH] feat: change config.vi_keybindings to config.keybindings (#158) --- README.md | 2 +- src/config/mod.rs | 29 +++++++++++++++++++++++++---- src/repl/init.rs | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3e25ab0f..ab903d43 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ highlight: true # Set false to turn highlight conversation_first: false # If set true, start a conversation immediately upon repl light_theme: false # If set true, use light theme auto_copy: false # Automatically copy the last output to the clipboard -vi_keybindings: false # If set ture, switch repl keybindings from emacs to vi +keybindings: emacs # REPL keybindings, possible values: emacs (default), vi clients: # Setup LLM platforms diff --git a/src/config/mod.rs b/src/config/mod.rs index 47fbeced..8e5157f7 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -58,8 +58,8 @@ pub struct Config { pub light_theme: bool, /// Automatically copy the last output to the clipboard pub auto_copy: bool, - /// Use vi keybindings, overriding the default Emacs keybindings - pub vi_keybindings: bool, + /// REPL keybindings, possible values: emacs (default), vi + pub keybindings: Keybindings, /// Setup LLM platforms pub clients: Vec, /// Predefined roles @@ -86,7 +86,7 @@ impl Default for Config { conversation_first: false, light_theme: false, auto_copy: false, - vi_keybindings: false, + keybindings: Default::default(), roles: vec![], clients: vec![ClientConfig::OpenAI(OpenAIConfig::default())], role: None, @@ -321,7 +321,7 @@ impl Config { ("conversation_first", self.conversation_first.to_string()), ("light_theme", self.light_theme.to_string()), ("dry_run", self.dry_run.to_string()), - ("vi_keybindings", self.vi_keybindings.to_string()), + ("keybindings", self.keybindings.stringify().into()), ]; let mut output = String::new(); for (name, value) in items { @@ -497,6 +497,27 @@ impl Config { } } +#[derive(Debug, Clone, Deserialize, Default)] +pub enum Keybindings { + #[serde(rename = "emacs")] + #[default] + Emacs, + #[serde(rename = "vi")] + Vi, +} + +impl Keybindings { + pub fn is_vi(&self) -> bool { + matches!(self, Keybindings::Vi) + } + pub fn stringify(&self) -> &str { + match self { + Keybindings::Emacs => "emacs", + Keybindings::Vi => "vi", + } + } +} + fn create_config_file(config_path: &Path) -> Result<()> { let ans = Confirm::new("No config file, create a new one?") .with_default(true) diff --git a/src/repl/init.rs b/src/repl/init.rs index 9cef4e52..60fce001 100644 --- a/src/repl/init.rs +++ b/src/repl/init.rs @@ -29,7 +29,7 @@ impl Repl { let highlighter = ReplHighlighter::new(config.clone(), commands); let history = Self::create_history()?; let menu = Self::create_menu(); - let edit_mode: Box = if config.read().vi_keybindings { + let edit_mode: Box = if config.read().keybindings.is_vi() { let mut normal_keybindings = default_vi_normal_keybindings(); let mut insert_keybindings = default_vi_insert_keybindings(); Self::extra_keybindings(&mut normal_keybindings);