summaryrefslogtreecommitdiffstats
path: root/src/repl
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-10-08 12:38:31 +0800
committerGitHub <noreply@github.com>2024-10-08 12:38:31 +0800
commit4bc74f7e9006fc7f182c9dc6ea309ea83c5bb4f9 (patch)
tree1ed8ef0b37879ea543fe1ba2060757e4079c90ce /src/repl
parentc211d1e43a02266226fb8d04856e703a4e6a625d (diff)
downloadaichat-4bc74f7e9006fc7f182c9dc6ea309ea83c5bb4f9.tar.gz
refactor: NO_COLOR, highlight and render_error (#915)
- If NO_COLOR is enabled, AIChat will not output any ANSI color codes. - `config.highlight` will only affect rendering markdown - improve rendering error
Diffstat (limited to 'src/repl')
-rw-r--r--src/repl/highlighter.rs34
-rw-r--r--src/repl/mod.rs2
2 files changed, 15 insertions, 21 deletions
diff --git a/src/repl/highlighter.rs b/src/repl/highlighter.rs
index 65cb1cc..ab6174c 100644
--- a/src/repl/highlighter.rs
+++ b/src/repl/highlighter.rs
@@ -1,34 +1,28 @@
use super::REPL_COMMANDS;
-use crate::config::GlobalConfig;
+use crate::{config::GlobalConfig, utils::NO_COLOR};
use nu_ansi_term::{Color, Style};
use reedline::{Highlighter, StyledText};
-pub struct ReplHighlighter {
- config: GlobalConfig,
-}
+const DEFAULT_COLOR: Color = Color::Default;
+const MATCH_COLOR: Color = Color::Green;
+
+pub struct ReplHighlighter;
impl ReplHighlighter {
- pub fn new(config: &GlobalConfig) -> Self {
- Self {
- config: config.clone(),
- }
+ pub fn new(_config: &GlobalConfig) -> Self {
+ Self
}
}
impl Highlighter for ReplHighlighter {
fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
- let color = Color::Default;
- let match_color = if self.config.read().highlight {
- Color::Green
- } else {
- color
- };
-
let mut styled_text = StyledText::new();
- if REPL_COMMANDS.iter().any(|cmd| line.contains(cmd.name)) {
+ if *NO_COLOR {
+ styled_text.push((Style::default(), line.to_string()));
+ } else if REPL_COMMANDS.iter().any(|cmd| line.contains(cmd.name)) {
let matches: Vec<&str> = REPL_COMMANDS
.iter()
.filter(|cmd| line.contains(cmd.name))
@@ -43,11 +37,11 @@ impl Highlighter for ReplHighlighter {
});
let buffer_split: Vec<&str> = line.splitn(2, &longest_match).collect();
- styled_text.push((Style::new().fg(color), buffer_split[0].to_string()));
- styled_text.push((Style::new().fg(match_color), longest_match));
- styled_text.push((Style::new().fg(color), buffer_split[1].to_string()));
+ styled_text.push((Style::new().fg(DEFAULT_COLOR), buffer_split[0].to_string()));
+ styled_text.push((Style::new().fg(MATCH_COLOR), longest_match));
+ styled_text.push((Style::new().fg(DEFAULT_COLOR), buffer_split[1].to_string()));
} else {
- styled_text.push((Style::new().fg(color), line.to_string()));
+ styled_text.push((Style::new().fg(DEFAULT_COLOR), line.to_string()));
}
styled_text
diff --git a/src/repl/mod.rs b/src/repl/mod.rs
index 5a99ae9..a7b2573 100644
--- a/src/repl/mod.rs
+++ b/src/repl/mod.rs
@@ -211,7 +211,7 @@ impl Repl {
}
}
Err(err) => {
- render_error(err, self.config.read().highlight);
+ render_error(err);
println!()
}
}