From 4a04465a3fd7703dbf063c41cc9b758ee4bb430f Mon Sep 17 00:00:00 2001 From: sigoden Date: Thu, 1 Aug 2024 15:01:34 +0800 Subject: feat: rename `config.buffer_editor` to `config.editor` (#773) --- config.example.yaml | 4 ++-- src/config/mod.rs | 18 ++++++++---------- src/repl/mod.rs | 2 +- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index cc0f7d8..6f3e006 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -4,10 +4,10 @@ temperature: null # Set default temperature parameter top_p: null # Set default top-p parameter, range (0, 1) # ---- behavior ---- -stream: true # Use stream style by default +stream: true # Controls whether to use the stream-style API. save: true # Indicates whether to persist the message keybindings: emacs # Choose keybinding style (emacs, vi) -buffer_editor: null # Command used to edit the current input with ctrl+o, env: EDITOR +editor: null # Command used to edit buffer, session.yaml and config.yaml. env: EDITOR wrap: no # Controls text wrapping (no, auto, ) wrap_code: false # Enables or disables wrapping of code blocks diff --git a/src/config/mod.rs b/src/config/mod.rs index 7cd5a17..1c9ae9d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -91,7 +91,7 @@ pub struct Config { pub stream: bool, pub save: bool, pub keybindings: String, - pub buffer_editor: Option, + pub editor: Option, pub wrap: Option, pub wrap_code: bool, @@ -159,7 +159,7 @@ impl Default for Config { stream: true, save: false, keybindings: "emacs".into(), - buffer_editor: None, + editor: None, wrap: None, wrap_code: false, @@ -841,10 +841,7 @@ impl Config { Some(session) => session.name().to_string(), None => bail!("No session"), }; - let editor = match self.buffer_editor() { - Some(editor) => editor, - None => bail!("No editor, please set $EDITOR/$VISUAL."), - }; + let editor = self.editor()?; let session_path = self.session_file(&name)?; self.save_session(Some(&name))?; edit_file(&editor, &session_path).with_context(|| { @@ -1191,10 +1188,11 @@ impl Config { } } - pub fn buffer_editor(&self) -> Option { - self.buffer_editor + pub fn editor(&self) -> Result { + self.editor .clone() .or_else(|| env::var("VISUAL").ok().or_else(|| env::var("EDITOR").ok())) + .ok_or_else(|| anyhow!("No editor, please configure `editor` or set $EDITOR/$VISUAL environment variable.")) } pub fn repl_complete( @@ -1596,8 +1594,8 @@ impl Config { self.keybindings = v; } } - if let Some(v) = read_env_value::("buffer_editor") { - self.buffer_editor = v; + if let Some(v) = read_env_value::("editor") { + self.editor = v; } if let Some(v) = read_env_value::("wrap") { self.wrap = v; diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 18d00f9..490cc89 100644 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -435,7 +435,7 @@ Type ".help" for additional help. .with_validator(Box::new(ReplValidator)) .with_ansi_colors(true); - if let Some(cmd) = config.read().buffer_editor() { + if let Ok(cmd) = config.read().editor() { let temp_file = temp_file("-repl-", ".txt"); let command = process::Command::new(cmd); editor = editor.with_buffer_editor(command, temp_file); -- cgit v1.2.3