refactor: merge theme into config, remove dead code

This commit is contained in:
objz committed 2025-08-09 14:52:44 +02:00
1 parent cc97c174c1
commit 6ff8ae64d5
10 files changed
+128 -208

No files matched your search

+1
View File
@@ -90,6 +90,7 @@ pub static SETTINGS: Lazy<Config> = Lazy::new(|| {
general: types::General::default(),
paths: types::Paths::default(),
defaults: types::Defaults::default(),
colors: types::Colors::default(),
}
}
}
+111 -1
View File
@@ -1,4 +1,6 @@
use serde::Deserialize;
use dirs_next;
use ratatui::style::Color;
use serde::{Deserialize, Deserializer};
#[derive(Debug, Deserialize)]
pub struct General {
@@ -32,6 +34,25 @@ impl Default for Paths {
}
}
impl Paths {
pub fn resolve_instances_dir(&self) -> std::path::PathBuf {
let raw = &self.instances_dir;
if let Some(stripped) = raw.strip_prefix("~/") {
return match dirs_next::home_dir() {
Some(home) => home.join(stripped),
None => std::path::PathBuf::from(raw),
};
}
if raw == "~" {
return match dirs_next::home_dir() {
Some(home) => home,
None => std::path::PathBuf::from(raw),
};
}
std::path::PathBuf::from(raw)
}
}
#[derive(Debug, Deserialize)]
pub struct Defaults {
#[serde(default = "default_memory_min")]
@@ -57,6 +78,93 @@ impl Default for Defaults {
}
}
#[derive(Debug, Deserialize, Clone)]
pub struct Colors {
#[serde(deserialize_with = "deserialize_color")]
pub background: Color,
#[serde(deserialize_with = "deserialize_color")]
pub foreground: Color,
#[serde(deserialize_with = "deserialize_color")]
pub border_focused: Color,
#[serde(deserialize_with = "deserialize_color")]
pub border_unfocused: Color,
#[serde(deserialize_with = "deserialize_color")]
pub row_highlight: Color,
#[serde(deserialize_with = "deserialize_color")]
pub row_background: Color,
#[serde(deserialize_with = "deserialize_color")]
pub row_alternate_bg: Color,
}
impl Default for Colors {
fn default() -> Self {
Colors {
background: Color::Reset,
foreground: Color::White,
border_focused: Color::White,
border_unfocused: Color::DarkGray,
row_highlight: Color::White,
row_background: Color::Reset,
row_alternate_bg: Color::Reset,
}
}
}
pub fn parse_color(color: &str) -> Color {
match color.to_lowercase().as_str() {
"black" => Color::Black,
"red" => Color::Red,
"green" => Color::Green,
"yellow" => Color::Yellow,
"blue" => Color::Blue,
"magenta" => Color::Magenta,
"cyan" => Color::Cyan,
"white" => Color::White,
"gray" | "grey" => Color::Gray,
"darkgray" | "darkgrey" => Color::DarkGray,
"lightred" => Color::LightRed,
"lightgreen" => Color::LightGreen,
"lightyellow" => Color::LightYellow,
"lightblue" => Color::LightBlue,
"lightmagenta" => Color::LightMagenta,
"lightcyan" => Color::LightCyan,
"reset" => Color::Reset,
hex if hex.starts_with('#') && hex.len() == 7 => {
let r = u8::from_str_radix(&hex[1..3], 16).unwrap_or_else(|_| {
tracing::warn!("Invalid hex color: {}", color);
255
});
let g = u8::from_str_radix(&hex[3..5], 16).unwrap_or_else(|_| {
tracing::warn!("Invalid hex color: {}", color);
255
});
let b = u8::from_str_radix(&hex[5..7], 16).unwrap_or_else(|_| {
tracing::warn!("Invalid hex color: {}", color);
255
});
Color::Rgb(r, g, b)
}
_ => {
tracing::warn!("Unknown color '{}', using White as fallback", color);
Color::White
}
}
}
fn deserialize_color<'de, D>(deserializer: D) -> Result<Color, D::Error>
where
D: Deserializer<'de>,
{
let color_str: String = match Deserialize::deserialize(deserializer) {
Ok(s) => s,
Err(e) => {
tracing::warn!("Failed to deserialize color field: {}", e);
return Ok(Color::White);
}
};
Ok(parse_color(&color_str))
}
#[derive(Debug, Deserialize)]
pub struct Config {
#[serde(default)]
@@ -65,4 +173,6 @@ pub struct Config {
pub paths: Paths,
#[serde(default)]
pub defaults: Defaults,
#[serde(default)]
pub colors: Colors,
}