174 lines
5.4 KiB
Rust
174 lines
5.4 KiB
Rust
// theme resolution: loads theme.toml, picks a base theme (builtin or custom file),
|
|
// then layers user color overrides on top. supports loading .toml themes from the
|
|
// config/theme/ directory or by absolute path.
|
|
|
|
use std::path::Path;
|
|
use std::sync::LazyLock;
|
|
|
|
use ratatui::style::Color;
|
|
use ratatui::widgets::BorderType;
|
|
use ratatui_themekit::{CustomTheme, Theme, resolve_theme};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum BorderStyle {
|
|
#[default]
|
|
Rounded,
|
|
Plain,
|
|
Double,
|
|
Thick,
|
|
}
|
|
|
|
impl BorderStyle {
|
|
pub fn to_border_type(&self) -> BorderType {
|
|
match self {
|
|
Self::Rounded => BorderType::Rounded,
|
|
Self::Plain => BorderType::Plain,
|
|
Self::Double => BorderType::Double,
|
|
Self::Thick => BorderType::Thick,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Default)]
|
|
pub struct ThemeOverrides {
|
|
pub accent: Option<Color>,
|
|
pub accent_dim: Option<Color>,
|
|
pub text: Option<Color>,
|
|
pub text_dim: Option<Color>,
|
|
pub text_bright: Option<Color>,
|
|
pub success: Option<Color>,
|
|
pub error: Option<Color>,
|
|
pub warning: Option<Color>,
|
|
pub info: Option<Color>,
|
|
pub diff_added: Option<Color>,
|
|
pub diff_removed: Option<Color>,
|
|
pub diff_context: Option<Color>,
|
|
pub border: Option<Color>,
|
|
pub surface: Option<Color>,
|
|
pub background: Option<Color>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct ThemeConfig {
|
|
#[serde(default)]
|
|
pub border_style: BorderStyle,
|
|
#[serde(default = "default_theme_name")]
|
|
pub theme: String,
|
|
#[serde(default)]
|
|
pub custom: Option<ThemeOverrides>,
|
|
}
|
|
|
|
fn default_theme_name() -> String {
|
|
"catppuccin".to_owned()
|
|
}
|
|
|
|
impl Default for ThemeConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
border_style: BorderStyle::default(),
|
|
theme: default_theme_name(),
|
|
custom: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn load_theme_config() -> ThemeConfig {
|
|
let path = super::get_config_path().join("theme.toml");
|
|
ensure_theme_exists(&path);
|
|
match std::fs::read_to_string(&path) {
|
|
Ok(content) => toml::from_str(&content).unwrap_or_else(|e| {
|
|
tracing::warn!("Failed to parse theme.toml: {}. Using defaults.", e);
|
|
ThemeConfig::default()
|
|
}),
|
|
Err(_) => ThemeConfig::default(),
|
|
}
|
|
}
|
|
|
|
fn ensure_theme_exists(path: &Path) {
|
|
if path.exists() {
|
|
return;
|
|
}
|
|
if let Some(parent) = path.parent() {
|
|
let _ = std::fs::create_dir_all(parent);
|
|
}
|
|
let _ = std::fs::write(path, include_str!("../../assets/theme.toml"));
|
|
}
|
|
|
|
// start from a base theme, then override individual colors if the user specified any
|
|
fn resolve_app_theme(config: &ThemeConfig) -> Box<dyn Theme> {
|
|
let base = load_base_theme(&config.theme);
|
|
|
|
let Some(overrides) = &config.custom else {
|
|
return base;
|
|
};
|
|
|
|
Box::new(CustomTheme {
|
|
name: format!("{} (customized)", base.name()),
|
|
id: base.id().to_owned(),
|
|
accent: overrides.accent.unwrap_or_else(|| base.accent()),
|
|
accent_dim: overrides.accent_dim.unwrap_or_else(|| base.accent_dim()),
|
|
text: overrides.text.unwrap_or_else(|| base.text()),
|
|
text_dim: overrides.text_dim.unwrap_or_else(|| base.text_dim()),
|
|
text_bright: overrides.text_bright.unwrap_or_else(|| base.text_bright()),
|
|
success: overrides.success.unwrap_or_else(|| base.success()),
|
|
error: overrides.error.unwrap_or_else(|| base.error()),
|
|
warning: overrides.warning.unwrap_or_else(|| base.warning()),
|
|
info: overrides.info.unwrap_or_else(|| base.info()),
|
|
diff_added: overrides.diff_added.unwrap_or_else(|| base.diff_added()),
|
|
diff_removed: overrides
|
|
.diff_removed
|
|
.unwrap_or_else(|| base.diff_removed()),
|
|
diff_context: overrides
|
|
.diff_context
|
|
.unwrap_or_else(|| base.diff_context()),
|
|
border: overrides.border.unwrap_or_else(|| base.border()),
|
|
surface: overrides.surface.unwrap_or_else(|| base.surface()),
|
|
background: overrides.background.unwrap_or_else(|| base.background()),
|
|
})
|
|
}
|
|
|
|
// tries to find the theme: absolute path > config/theme/<name> > config/theme/<name>.toml > builtin
|
|
fn load_base_theme(name: &str) -> Box<dyn Theme> {
|
|
let path = if Path::new(name).is_absolute() {
|
|
Some(std::path::PathBuf::from(name))
|
|
} else {
|
|
let theme_dir = super::get_config_path().join("theme");
|
|
let candidate = theme_dir.join(name);
|
|
if candidate.exists() {
|
|
Some(candidate)
|
|
} else {
|
|
let with_ext = theme_dir.join(format!("{name}.toml"));
|
|
if with_ext.exists() {
|
|
Some(with_ext)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
};
|
|
|
|
if let Some(path) = path
|
|
&& let Ok(content) = std::fs::read_to_string(&path)
|
|
{
|
|
if let Ok(custom) = toml::from_str::<CustomTheme>(&content) {
|
|
return Box::new(custom);
|
|
} else {
|
|
tracing::warn!("Failed to parse theme file: {}", path.display());
|
|
}
|
|
}
|
|
|
|
resolve_theme(name)
|
|
}
|
|
|
|
static THEME_CONFIG: LazyLock<ThemeConfig> = LazyLock::new(load_theme_config);
|
|
|
|
pub static THEME: LazyLock<Box<dyn Theme>> = LazyLock::new(|| resolve_app_theme(&THEME_CONFIG));
|
|
|
|
pub static BORDER_STYLE: LazyLock<BorderStyle> =
|
|
LazyLock::new(|| THEME_CONFIG.border_style.clone());
|
|
|
|
#[cfg(test)]
|
|
#[path = "tests/theme.rs"]
|
|
mod tests;
|