From 17fbc46e195d2630650ad2d0c60cb4d197d1d8e4 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 10 Jan 2025 18:39:42 +0800 Subject: feat: support default terminal editor (#1082) --- src/config/mod.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'src/config') diff --git a/src/config/mod.rs b/src/config/mod.rs index 407d041..fd23eb1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -35,7 +35,7 @@ use std::{ io::Write, path::{Path, PathBuf}, process, - sync::Arc, + sync::{Arc, OnceLock}, }; use syntect::highlighting::ThemeSet; @@ -88,6 +88,8 @@ __INPUT__ const LEFT_PROMPT: &str = "{color.green}{?session {?agent {agent}>}{session}{?role /}}{!session {?agent {agent}>}}{role}{?rag @{rag}}{color.cyan}{?session )}{!session >}{color.reset} "; const RIGHT_PROMPT: &str = "{color.purple}{?session {?consume_tokens {consume_tokens}({consume_percent}%)}{!consume_tokens {consume_tokens}}}{color.reset}"; +static EDITOR: OnceLock> = OnceLock::new(); + #[derive(Debug, Clone, Deserialize)] #[serde(default)] pub struct Config { @@ -1640,10 +1642,20 @@ impl Config { } 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.")) + EDITOR.get_or_init(move || { + let editor = self.editor.clone() + .or_else(|| env::var("VISUAL").ok().or_else(|| env::var("EDITOR").ok())) + .unwrap_or_else(|| { + if cfg!(windows) { + "notepad".to_string() + } else { + "nano".to_string() + } + }); + which::which(&editor).ok().map(|_| editor) + }) + .clone() + .ok_or_else(|| anyhow!("Editor not found. Please add the `editor` configuration or set the $EDITOR or $VISUAL environment variable.")) } pub fn repl_complete( -- cgit v1.2.3