summaryrefslogtreecommitdiffstats
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
parent486ee57bf8123116a326d1433bcb1b36ff88ee67 (diff)
downloadaichat-17fbc46e195d2630650ad2d0c60cb4d197d1d8e4.tar.gz
feat: support default terminal editor (#1082)
-rw-r--r--Cargo.lock25
-rw-r--r--Cargo.toml1
-rw-r--r--src/config/mod.rs22
3 files changed, 43 insertions, 5 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5ef45db..3a64cd0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -101,6 +101,7 @@ dependencies = [
"unicode-width 0.2.0",
"urlencoding",
"uuid",
+ "which",
]
[[package]]
@@ -889,6 +890,12 @@ dependencies = [
]
[[package]]
+name = "env_home"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe"
+
+[[package]]
name = "env_logger"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3934,6 +3941,18 @@ dependencies = [
]
[[package]]
+name = "which"
+version = "7.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fb4a9e33648339dc1642b0e36e21b3385e6148e289226f657c809dee59df5028"
+dependencies = [
+ "either",
+ "env_home",
+ "rustix",
+ "winsafe",
+]
+
+[[package]]
name = "widestring"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -4212,6 +4231,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
+name = "winsafe"
+version = "0.0.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"
+
+[[package]]
name = "wl-clipboard-rs"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 5042b8f..1af8aa3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -65,6 +65,7 @@ html_to_markdown = "0.1.0"
rust-embed = "8.5.0"
os_info = { version = "3.8.2", default-features = false }
bm25 = { version = "2.0.1", features = ["parallelism"] }
+which = "7.0.1"
[dependencies.reqwest]
version = "0.12.0"
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(