Skip to content

Commit

Permalink
feat: support default terminal editor (#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jan 10, 2025
1 parent 486ee57 commit 17fbc46
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 17 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::{
io::Write,
path::{Path, PathBuf},
process,
sync::Arc,
sync::{Arc, OnceLock},
};
use syntect::highlighting::ThemeSet;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 17fbc46

Please sign in to comment.