diff --git a/Cargo.lock b/Cargo.lock index c613bf2..30e67f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1095,6 +1095,7 @@ dependencies = [ "fmt", "image", "lazy_static", + "once_cell", "ratatui", "ratatui-image", "serde", diff --git a/Cargo.toml b/Cargo.toml index b32abda..3c888a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/assets/default.toml b/assets/default.toml index 8487947..0fcd59e 100644 --- a/assets/default.toml +++ b/assets/default.toml @@ -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 diff --git a/src/config/mod.rs b/src/config/mod.rs index 33a597b..9244a0c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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 { .build()? .try_deserialize() } + +pub static SETTINGS: Lazy = Lazy::new(|| { + let path: PathBuf = ensure_config_exists("assets/default.toml"); + load_config(&path).expect("Failed to load configuration") +}); diff --git a/src/config/obj.rs b/src/config/obj.rs deleted file mode 100644 index 55cfc1f..0000000 --- a/src/config/obj.rs +++ /dev/null @@ -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, -} diff --git a/src/config/types.rs b/src/config/types.rs new file mode 100644 index 0000000..8a61000 --- /dev/null +++ b/src/config/types.rs @@ -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 +where + D: Deserializer<'de>, +{ + let color_str: String = Deserialize::deserialize(deserializer)?; + Ok(parse_color(&color_str)) +} diff --git a/src/logger.rs b/src/logger.rs index 9fd9628..4891bf3 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -53,4 +53,4 @@ lazy_static! { pub fn get_logger() -> std::sync::MutexGuard<'static, Logger> { LOGGER.lock().unwrap() -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 7713adb..e3f6839 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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() } - diff --git a/src/tui/widgets/instances.rs b/src/tui/widgets/instances.rs index c70119f..7f3ab47 100644 --- a/src/tui/widgets/instances.rs +++ b/src/tui/widgets/instances.rs @@ -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()