Skip to content

Commit 17fbc46

Browse files
authored
feat: support default terminal editor (#1082)
1 parent 486ee57 commit 17fbc46

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ html_to_markdown = "0.1.0"
6565
rust-embed = "8.5.0"
6666
os_info = { version = "3.8.2", default-features = false }
6767
bm25 = { version = "2.0.1", features = ["parallelism"] }
68+
which = "7.0.1"
6869

6970
[dependencies.reqwest]
7071
version = "0.12.0"

src/config/mod.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::{
3535
io::Write,
3636
path::{Path, PathBuf},
3737
process,
38-
sync::Arc,
38+
sync::{Arc, OnceLock},
3939
};
4040
use syntect::highlighting::ThemeSet;
4141

@@ -88,6 +88,8 @@ __INPUT__
8888
const LEFT_PROMPT: &str = "{color.green}{?session {?agent {agent}>}{session}{?role /}}{!session {?agent {agent}>}}{role}{?rag @{rag}}{color.cyan}{?session )}{!session >}{color.reset} ";
8989
const RIGHT_PROMPT: &str = "{color.purple}{?session {?consume_tokens {consume_tokens}({consume_percent}%)}{!consume_tokens {consume_tokens}}}{color.reset}";
9090

91+
static EDITOR: OnceLock<Option<String>> = OnceLock::new();
92+
9193
#[derive(Debug, Clone, Deserialize)]
9294
#[serde(default)]
9395
pub struct Config {
@@ -1640,10 +1642,20 @@ impl Config {
16401642
}
16411643

16421644
pub fn editor(&self) -> Result<String> {
1643-
self.editor
1644-
.clone()
1645-
.or_else(|| env::var("VISUAL").ok().or_else(|| env::var("EDITOR").ok()))
1646-
.ok_or_else(|| anyhow!("No editor, please configure `editor` or set $EDITOR/$VISUAL environment variable."))
1645+
EDITOR.get_or_init(move || {
1646+
let editor = self.editor.clone()
1647+
.or_else(|| env::var("VISUAL").ok().or_else(|| env::var("EDITOR").ok()))
1648+
.unwrap_or_else(|| {
1649+
if cfg!(windows) {
1650+
"notepad".to_string()
1651+
} else {
1652+
"nano".to_string()
1653+
}
1654+
});
1655+
which::which(&editor).ok().map(|_| editor)
1656+
})
1657+
.clone()
1658+
.ok_or_else(|| anyhow!("Editor not found. Please add the `editor` configuration or set the $EDITOR or $VISUAL environment variable."))
16471659
}
16481660

16491661
pub fn repl_complete(

0 commit comments

Comments
 (0)