summaryrefslogtreecommitdiffstats
path: root/src/config
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-10-31 09:06:41 +0800
committerGitHub <noreply@github.com>2023-10-31 09:06:41 +0800
commit680670e834ff57cf6eb15c373870d1fd478f8edb (patch)
tree5b3ef0436532560c35da9959d3e603397a1d8c0d /src/config
parent84004fd5767abc94a5360b742e7fd0c8a79c6c55 (diff)
downloadaichat-680670e834ff57cf6eb15c373870d1fd478f8edb.tar.gz
feat: support custom theme (#187)
Diffstat (limited to 'src/config')
-rw-r--r--src/config/mod.rs28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 9f4a11a..c84a6fb 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -25,6 +25,11 @@ use std::{
process::exit,
sync::Arc,
};
+use syntect::highlighting::ThemeSet;
+
+/// Monokai Extended
+const DARK_THEME: &[u8] = include_bytes!("../../assets/monokai-extended.theme.bin");
+const LIGHT_THEME: &[u8] = include_bytes!("../../assets/monokai-extended-light.theme.bin");
const CONFIG_FILE_NAME: &str = "config.yaml";
const ROLES_FILE_NAME: &str = "roles.yaml";
@@ -522,13 +527,32 @@ impl Config {
}
}
- pub fn get_render_options(&self) -> RenderOptions {
+ pub fn get_render_options(&self) -> Result<RenderOptions> {
+ let theme = if self.highlight {
+ let theme_mode = if self.light_theme { "light" } else { "dark" };
+ let theme_filename = format!("{theme_mode}.tmTheme");
+ let theme_path = Self::local_path(&theme_filename)?;
+ if theme_path.exists() {
+ let theme = ThemeSet::get_theme(&theme_path)
+ .with_context(|| format!("Invalid theme at {}", theme_path.display()))?;
+ Some(theme)
+ } else {
+ let theme = if self.light_theme {
+ bincode::deserialize_from(LIGHT_THEME).expect("Invalid builtin light theme")
+ } else {
+ bincode::deserialize_from(DARK_THEME).expect("Invalid builtin dark theme")
+ };
+ Some(theme)
+ }
+ } else {
+ None
+ };
let wrap = if stdout().is_terminal() {
self.wrap.clone()
} else {
None
};
- RenderOptions::new(self.highlight, self.light_theme, wrap, self.wrap_code)
+ Ok(RenderOptions::new(theme, wrap, self.wrap_code))
}
pub fn maybe_print_send_tokens(&self, input: &str) {