use crate::theme::{
apply_color_overrides, built_in_theme_names_display, is_dark_mode, resolve_theme_name, Theme,
};
use serde::Deserialize;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default)]
pub struct AppConfig {
pub theme: Option<String>,
pub theme_dark: Option<String>,
pub theme_light: Option<String>,
pub colors: HashMap<String, String>,
}
pub fn load_config(ca_dir: &Path) -> (AppConfig, Vec<String>) {
let path = ca_dir.join("config.toml");
let Ok(contents) = std::fs::read_to_string(&path) else {
return (AppConfig::default(), Vec::new());
};
match toml::from_str(&contents) {
Ok(config) => (config, Vec::new()),
Err(err) => (
AppConfig::default(),
vec![format!(
"Warning: Failed to parse '{}': {err}, using defaults",
path.display()
)],
),
}
}
pub fn resolve_theme(
cli_theme: Option<&str>,
config: &AppConfig,
ca_dir: &Path,
) -> (Theme, Vec<String>) {
let themes_dir = ca_dir.join("themes");
let requested = cli_theme.or(config.theme.as_deref());
let (mut theme, mut warnings) = match requested {
None => (Theme::default(), Vec::new()),
Some("system") => resolve_system_theme(config, &themes_dir),
Some(name) => resolve_named_theme(name, &themes_dir),
};
warnings.extend(apply_color_overrides(&mut theme, &config.colors));
(theme, warnings)
}
fn resolve_named_theme(name: &str, themes_dir: &Path) -> (Theme, Vec<String>) {
match resolve_theme_name(name, themes_dir) {
Ok(Some((theme, warnings))) => (theme, warnings),
Ok(None) => (
Theme::default(),
vec![format!(
"Warning: Unknown theme '{name}'. Bundled themes: {}, system. Local themes are loaded from {}",
built_in_theme_names_display(),
themes_dir.display()
)],
),
Err(err) => (Theme::default(), vec![format!("Warning: {err}, using default theme")]),
}
}
fn resolve_system_theme(config: &AppConfig, themes_dir: &Path) -> (Theme, Vec<String>) {
let name = if is_dark_mode() {
config.theme_dark.as_deref().unwrap_or("default")
} else {
config.theme_light.as_deref().unwrap_or("default")
};
resolve_named_theme(name, themes_dir)
}
#[allow(dead_code)]
pub fn config_path(ca_dir: &Path) -> PathBuf {
ca_dir.join("config.toml")
}
#[allow(dead_code)]
pub fn themes_dir(ca_dir: &Path) -> PathBuf {
ca_dir.join("themes")
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
#[test]
fn missing_config_file_is_not_an_error() {
let dir = std::env::temp_dir().join(format!("proxelar-test-{}", std::process::id()));
let (config, warnings) = load_config(&dir);
assert!(config.theme.is_none());
assert!(warnings.is_empty());
}
#[test]
fn malformed_config_file_warns_and_falls_back() {
let dir = std::env::temp_dir().join(format!(
"proxelar-test-malformed-{}-{}",
std::process::id(),
line!()
));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("config.toml");
let mut file = std::fs::File::create(&path).unwrap();
writeln!(file, "this is not valid = = toml").unwrap();
let (config, warnings) = load_config(&dir);
assert!(config.theme.is_none());
assert_eq!(warnings.len(), 1);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn cli_theme_wins_over_config_theme() {
let dir = Path::new("/nonexistent");
let config = AppConfig {
theme: Some("cyberdream-light".to_string()),
..Default::default()
};
let (theme, warnings) = resolve_theme(Some("cyberdream"), &config, dir);
assert!(warnings.is_empty());
assert_ne!(
theme.table_header.0,
crate::theme::Theme::default().table_header.0
);
}
#[test]
fn unknown_cli_theme_warns_and_falls_back_to_default() {
let dir = Path::new("/nonexistent");
let config = AppConfig::default();
let (theme, warnings) = resolve_theme(Some("not-a-theme"), &config, dir);
assert_eq!(warnings.len(), 1);
assert!(warnings[0].contains("Unknown theme"));
assert_eq!(
theme.table_header.0,
crate::theme::Theme::default().table_header.0
);
}
#[test]
fn no_theme_specified_uses_default() {
let dir = Path::new("/nonexistent");
let config = AppConfig::default();
let (theme, warnings) = resolve_theme(None, &config, dir);
assert!(warnings.is_empty());
assert_eq!(
theme.table_header.0,
crate::theme::Theme::default().table_header.0
);
}
#[test]
fn dark_alias_matches_default() {
let dir = Path::new("/nonexistent");
let config = AppConfig::default();
let (theme, warnings) = resolve_theme(Some("dark"), &config, dir);
assert!(warnings.is_empty());
assert_eq!(
theme.table_header.0,
crate::theme::Theme::default().table_header.0
);
}
#[test]
fn system_theme_uses_theme_dark_or_theme_light() {
let _guard = crate::theme::test_support::ColorfgbgGuard::new();
let dir = Path::new("/nonexistent");
let config = AppConfig {
theme: Some("system".to_string()),
theme_dark: Some("cyberdream".to_string()),
theme_light: Some("cyberdream-light".to_string()),
..Default::default()
};
std::env::set_var("COLORFGBG", "15;0"); let (dark_resolved, warnings) = resolve_theme(None, &config, dir);
assert!(warnings.is_empty());
std::env::set_var("COLORFGBG", "0;15"); let (light_resolved, warnings) = resolve_theme(None, &config, dir);
assert!(warnings.is_empty());
let (cyberdream, _) = resolve_named_theme("cyberdream", &themes_dir(dir));
let (cyberdream_light, _) = resolve_named_theme("cyberdream-light", &themes_dir(dir));
assert_eq!(dark_resolved, cyberdream);
assert_eq!(light_resolved, cyberdream_light);
assert_ne!(dark_resolved, light_resolved);
}
#[test]
fn system_without_dark_light_config_is_appearance_independent() {
let _guard = crate::theme::test_support::ColorfgbgGuard::new();
let dir = Path::new("/nonexistent");
let config = AppConfig {
theme: Some("system".to_string()),
..Default::default()
};
std::env::set_var("COLORFGBG", "15;0"); let (dark_resolved, warnings) = resolve_theme(None, &config, dir);
assert!(warnings.is_empty());
std::env::set_var("COLORFGBG", "0;15"); let (light_resolved, warnings) = resolve_theme(None, &config, dir);
assert!(warnings.is_empty());
assert_eq!(dark_resolved, light_resolved);
assert_eq!(dark_resolved, crate::theme::Theme::default());
}
#[test]
fn theme_dark_only_overrides_specified_colors() {
let _guard = crate::theme::test_support::ColorfgbgGuard::new();
let dir = std::env::temp_dir().join(format!(
"proxelar-test-partial-dark-{}-{}",
std::process::id(),
line!()
));
let themes_dir = dir.join("themes");
std::fs::create_dir_all(&themes_dir).unwrap();
std::fs::write(
themes_dir.join("my-dark.toml"),
r##"status_bar_bg = "#101010""##,
)
.unwrap();
let config = AppConfig {
theme: Some("system".to_string()),
theme_dark: Some("my-dark".to_string()),
..Default::default()
};
std::env::set_var("COLORFGBG", "15;0"); let (theme, warnings) = resolve_theme(None, &config, &dir);
assert!(warnings.is_empty());
assert_eq!(
theme.status_bar_bg.0,
ratatui::style::Color::Rgb(0x10, 0x10, 0x10)
);
assert_eq!(
theme.table_header.0,
crate::theme::Theme::default().table_header.0
);
assert_eq!(
theme.method_get.0,
crate::theme::Theme::default().method_get.0
);
std::fs::remove_dir_all(&dir).ok();
}
#[test]
fn color_overrides_apply_on_top_of_resolved_theme() {
let dir = Path::new("/nonexistent");
let mut colors = HashMap::new();
colors.insert("status_bar_bg".to_string(), "#334455".to_string());
let config = AppConfig {
colors,
..Default::default()
};
let (theme, warnings) = resolve_theme(None, &config, dir);
assert!(warnings.is_empty());
assert_eq!(
theme.status_bar_bg.0,
ratatui::style::Color::Rgb(0x33, 0x44, 0x55)
);
assert_eq!(
theme.table_header.0,
crate::theme::Theme::default().table_header.0
);
}
}