feat: improve config system
This commit is contained in:
1 parent
3bc5432a20
commit
f571aa3183
9 files changed
+117
-61
No files matched your search
+9
-3
@@ -1,19 +1,20 @@
|
||||
use config::{Config as ConfigLoader, ConfigError, File};
|
||||
use dirs_next::config_dir;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use obj::Config;
|
||||
use types::Config;
|
||||
|
||||
use crate::debug;
|
||||
|
||||
pub mod obj;
|
||||
pub mod types;
|
||||
|
||||
fn get_config_path() -> PathBuf {
|
||||
let base_dir = config_dir().unwrap();
|
||||
base_dir.join("mcl/config.toml")
|
||||
}
|
||||
|
||||
pub fn ensure_config_exists(default_path: &str) -> PathBuf {
|
||||
fn ensure_config_exists(default_path: &str) -> PathBuf {
|
||||
let config_path = get_config_path();
|
||||
|
||||
if !config_path.exists() {
|
||||
@@ -34,3 +35,8 @@ pub fn load_config(config_path: &PathBuf) -> Result<Config, ConfigError> {
|
||||
.build()?
|
||||
.try_deserialize()
|
||||
}
|
||||
|
||||
pub static SETTINGS: Lazy<Config> = Lazy::new(|| {
|
||||
let path: PathBuf = ensure_config_exists("assets/default.toml");
|
||||
load_config(&path).expect("Failed to load configuration")
|
||||
});
|
||||
@@ -1,27 +0,0 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct General {
|
||||
pub debug: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Paths {
|
||||
pub log_dir: String,
|
||||
pub save_dir: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Colors {
|
||||
pub background: String,
|
||||
pub foreground: String,
|
||||
pub highlight: String,
|
||||
pub focused: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub general: General,
|
||||
pub paths: Paths,
|
||||
pub colors: Colors,
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use ratatui::style::Color;
|
||||
use crate::error;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct General {
|
||||
pub debug: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Colors {
|
||||
#[serde(deserialize_with = "deserialize_color")]
|
||||
pub background: Color, // General background color
|
||||
|
||||
#[serde(deserialize_with = "deserialize_color")]
|
||||
pub foreground: Color, // General text color
|
||||
|
||||
#[serde(deserialize_with = "deserialize_color")]
|
||||
pub border_focused: Color, // Border color for focused areas
|
||||
|
||||
#[serde(deserialize_with = "deserialize_color")]
|
||||
pub border_unfocused: Color, // Border color for unfocused areas
|
||||
|
||||
#[serde(deserialize_with = "deserialize_color")]
|
||||
pub row_highlight: Color, // Highlighted row color
|
||||
|
||||
#[serde(deserialize_with = "deserialize_color")]
|
||||
pub row_alternate_bg: Color, // Alternate row background color
|
||||
|
||||
#[serde(deserialize_with = "deserialize_color")]
|
||||
pub scrollbar: Color, // Scrollbar color
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub general: General,
|
||||
pub colors: Colors,
|
||||
}
|
||||
|
||||
|
||||
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(|_| {
|
||||
error!("Invalid hex color format: {}", color);
|
||||
255
|
||||
});
|
||||
let g = u8::from_str_radix(&hex[3..5], 16).unwrap_or_else(|_| {
|
||||
error!("Invalid hex color format: {}", color);
|
||||
255
|
||||
});
|
||||
let b = u8::from_str_radix(&hex[5..7], 16).unwrap_or_else(|_| {
|
||||
error!("Invalid hex color format: {}", color);
|
||||
255
|
||||
});
|
||||
Color::Rgb(r, g, b)
|
||||
}
|
||||
_ => {
|
||||
error!("Invalid color name or hex value: {}", color);
|
||||
panic!("Invalid config");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn deserialize_color<'de, D>(deserializer: D) -> Result<Color, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let color_str: String = Deserialize::deserialize(deserializer)?;
|
||||
Ok(parse_color(&color_str))
|
||||
}
|
||||
Reference in new issue
Block a user