feat: improve config system

This commit is contained in:
72-S committed 2025-03-08 22:33:21 +01:00
1 parent 3bc5432a20
commit f571aa3183
9 files changed
+117 -61

No files matched your search

Generated
+1
View File
@@ -1095,6 +1095,7 @@ dependencies = [
"fmt",
"image",
"lazy_static",
"once_cell",
"ratatui",
"ratatui-image",
"serde",
+1
View File
@@ -19,4 +19,5 @@ chrono = "0.4.39"
ratatui-image = { version = "5.0.0", features = ["crossterm", "serde"]}
image = "0.25.5"
fmt = "0.1.0"
once_cell = "1.20.3"
+8 -9
View File
@@ -1,12 +1,11 @@
[variables]
[general]
debug = false
[paths]
log_dir = "./logs"
save_dir = "./saves"
[colors]
background = "#000000" # Default background color (black)
foreground = "#FFFFFF" # Default foreground color (white)
highlight = "#FF5733" # Default highlight color (orange)
focused = "#3498DB" # Default focused color (blue)
background = "#1e1e1e" # Dark gray background
foreground = "white" # White text
border_focused = "white" # White for focused widget borders
border_unfocused = "darkgray" # Dark gray for unfocused widget borders
row_highlight = "yellow" # Yellow highlight for selected row
row_alternate_bg = "#282828" # Slightly lighter gray for alternate rows
scrollbar = "white" # White scrollbar
+9 -3
View File
@@ -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")
});
-27
View File
@@ -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,
}
+90
View File
@@ -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))
}
+1 -1
View File
@@ -53,4 +53,4 @@ lazy_static! {
pub fn get_logger() -> std::sync::MutexGuard<'static, Logger> {
LOGGER.lock().unwrap()
}
}
+4 -18
View File
@@ -1,27 +1,13 @@
use std::path::PathBuf;
use crate::config::{ensure_config_exists, load_config};
use config::SETTINGS;
use logger::Logger;
mod cli;
pub mod config;
pub mod logger;
pub mod macros;
pub mod tui;
pub mod config;
fn main() {
let default = "assets/default.toml";
let path: PathBuf = ensure_config_exists(default);
match load_config(&path) {
Ok(config) => {
Logger::init(config.general.debug);
cli::init()
}
Err(e) => {
error!("Failed to load config file: {}", e);
}
}
Logger::init(SETTINGS.general.debug);
cli::init()
}
+3 -3
View File
@@ -11,7 +11,7 @@ use ratatui::{
};
use ratatui_image::StatefulImage;
use crate::tui::layout::FocusedArea;
use crate::{config::SETTINGS, tui::layout::FocusedArea};
use super::{styled_title, WidgetKey};
@@ -113,9 +113,9 @@ impl WidgetKey for State {
pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut State) {
let color = if focused == FocusedArea::Instances {
Color::White
SETTINGS.colors.border_focused
} else {
Color::DarkGray
SETTINGS.colors.border_unfocused
};
let block = Block::default()