summaryrefslogtreecommitdiffstats
path: root/src/repl/init.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-11 18:53:36 +0800
committerGitHub <noreply@github.com>2023-03-11 18:53:36 +0800
commitcfb6ce6958751da5e13683ea07f42a5dbc4eae01 (patch)
treec7ec60ba7c1d2e37225ae6b1476057a18e2eb987 /src/repl/init.rs
parentdc09cb38d480342c9fe781a19fe72d2bb892b70c (diff)
downloadaichat-cfb6ce6958751da5e13683ea07f42a5dbc4eae01.tar.gz
feat: support light theme (#65)
There are two ways to enable the light theme: - add `light_theme: true` to config.yaml - use `AICHAT_LIGHT_THEME=true` env var
Diffstat (limited to 'src/repl/init.rs')
-rw-r--r--src/repl/init.rs33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/repl/init.rs b/src/repl/init.rs
index 7d0c2bb..ff75eff 100644
--- a/src/repl/init.rs
+++ b/src/repl/init.rs
@@ -3,12 +3,17 @@ use super::REPL_COMMANDS;
use crate::config::{Config, SharedConfig};
use anyhow::{Context, Result};
+use nu_ansi_term::Color;
use reedline::{
default_emacs_keybindings, ColumnarMenu, DefaultCompleter, DefaultValidator, Emacs,
- FileBackedHistory, KeyCode, KeyModifiers, Keybindings, Reedline, ReedlineEvent, ReedlineMenu,
+ ExampleHighlighter, FileBackedHistory, KeyCode, KeyModifiers, Keybindings, Reedline,
+ ReedlineEvent, ReedlineMenu,
};
const MENU_NAME: &str = "completion_menu";
+const MATCH_COLOR: Color = Color::Green;
+const NEUTRAL_COLOR: Color = Color::White;
+const NEUTRAL_COLOR_LIGHT: Color = Color::Black;
pub struct Repl {
pub editor: Reedline,
@@ -16,13 +21,20 @@ pub struct Repl {
impl Repl {
pub fn init(config: SharedConfig) -> Result<Self> {
- let completer = Self::create_completer(config);
+ let commands: Vec<String> = REPL_COMMANDS
+ .into_iter()
+ .map(|(v, _)| v.to_string())
+ .collect();
+
+ let completer = Self::create_completer(config.clone(), &commands);
+ let highlighter = Self::create_highlighter(config, &commands);
let keybindings = Self::create_keybindings();
let history = Self::create_history()?;
let menu = Self::create_menu();
let edit_mode = Box::new(Emacs::new(keybindings));
let editor = Reedline::create()
.with_completer(Box::new(completer))
+ .with_highlighter(Box::new(highlighter))
.with_history(history)
.with_menu(menu)
.with_edit_mode(edit_mode)
@@ -33,17 +45,24 @@ impl Repl {
Ok(Self { editor })
}
- fn create_completer(config: SharedConfig) -> DefaultCompleter {
- let mut completion: Vec<String> = REPL_COMMANDS
- .into_iter()
- .map(|(v, _)| v.to_string())
- .collect();
+ fn create_completer(config: SharedConfig, commands: &[String]) -> DefaultCompleter {
+ let mut completion = commands.to_vec();
completion.extend(config.lock().repl_completions());
let mut completer = DefaultCompleter::with_inclusions(&['.', '-', '_']).set_min_word_len(2);
completer.insert(completion.clone());
completer
}
+ fn create_highlighter(config: SharedConfig, commands: &[String]) -> ExampleHighlighter {
+ let mut highlighter = ExampleHighlighter::new(commands.to_vec());
+ if config.lock().light_theme {
+ highlighter.change_colors(MATCH_COLOR, NEUTRAL_COLOR_LIGHT, NEUTRAL_COLOR_LIGHT);
+ } else {
+ highlighter.change_colors(MATCH_COLOR, NEUTRAL_COLOR, NEUTRAL_COLOR);
+ }
+ highlighter
+ }
+
fn create_keybindings() -> Keybindings {
let mut keybindings = default_emacs_keybindings();
keybindings.add_binding(