summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2025-01-10 18:39:42 +0800
committerGitHub <noreply@github.com>2025-01-10 18:39:42 +0800
commit17fbc46e195d2630650ad2d0c60cb4d197d1d8e4 (patch)
treebfe061fed485575d6e4e267747f75e4b51600b64 /src
parent486ee57bf8123116a326d1433bcb1b36ff88ee67 (diff)
downloadaichat-17fbc46e195d2630650ad2d0c60cb4d197d1d8e4.tar.gz
feat: support default terminal editor (#1082)
Diffstat (limited to 'src')
-rw-r--r--src/config/mod.rs22
1 files changed, 17 insertions, 5 deletions
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<Option<String>> = OnceLock::new();
+
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct Config {
@@ -1640,10 +1642,20 @@ impl Config {
}
pub fn editor(&self) -> Result<String> {
- 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(