diff options
Diffstat (limited to 'src/config/configuration.rs')
-rw-r--r-- | src/config/configuration.rs | 103 |
1 files changed, 95 insertions, 8 deletions
diff --git a/src/config/configuration.rs b/src/config/configuration.rs index be5da59..93699ff 100644 --- a/src/config/configuration.rs +++ b/src/config/configuration.rs @@ -3,12 +3,13 @@ use std::{fs::File, path}; use anyhow::Result; use clap::Parser; use serde_yaml; -use tuikit::attr::Color; +use tuikit::attr::{Attr, Color}; -use crate::common::is_program_in_path; +use crate::common::{is_program_in_path, DEFAULT_TERMINAL_FLAG}; use crate::common::{CONFIG_PATH, DEFAULT_TERMINAL_APPLICATION}; use crate::config::Bindings; use crate::config::Colorer; +use crate::io::color_to_attr; /// Holds every configurable aspect of the application. /// All attributes are hardcoded then updated from optional values @@ -18,23 +19,29 @@ use crate::config::Colorer; pub struct Config { /// The name of the terminal application. It should be installed properly. pub terminal: String, + /// terminal flag to run a command with the terminal emulator + pub terminal_flag: String, /// Configurable keybindings. pub binds: Bindings, } - -impl Config { +impl Default for Config { /// Returns a default config with hardcoded values. - fn new() -> Result<Self> { - Ok(Self { + fn default() -> Self { + Self { terminal: DEFAULT_TERMINAL_APPLICATION.to_owned(), binds: Bindings::default(), - }) + terminal_flag: DEFAULT_TERMINAL_FLAG.to_owned(), + } } +} + +impl Config { /// Updates the config from a configuration content. fn update_from_config(&mut self, yaml: &serde_yaml::value::Value) -> Result<()> { self.binds.update_normal(&yaml["keys"]); self.binds.update_custom(&yaml["custom"]); self.update_terminal(&yaml["terminal"]); + self.update_terminal_flag(&yaml["terminal_emulator_flags"]); Ok(()) } @@ -50,6 +57,23 @@ impl Config { } } + fn update_terminal_flag(&mut self, terminal_flag: &serde_yaml::value::Value) { + let terminal = self.terminal(); + if let Some(terminal_flag) = read_yaml_value(terminal_flag, terminal) { + self.terminal_flag = terminal_flag.as_str().to_owned(); + crate::log_info!( + "updated terminal_flag for {terminal} using {tf}", + terminal = self.terminal, + tf = self.terminal_flag + ); + } else { + crate::log_info!( + "Couldn't find {terminal} in config file. Using default", + terminal = self.terminal + ) + } + } + /// The terminal name pub fn terminal(&self) -> &str { &self.terminal @@ -63,7 +87,7 @@ impl Config { /// 2. configured values from `~/.config/fm/config_file_name.yaml` if those files exists. /// If the config fle is poorly formated its simply ignored. pub fn load_config(path: &str) -> Result<Config> { - let mut config = Config::new()?; + let mut config = Config::default(); let file = File::open(path::Path::new(&shellexpand::tilde(path).to_string()))?; let Ok(yaml) = serde_yaml::from_reader(file) else { return Ok(config); @@ -230,3 +254,66 @@ lazy_static::lazy_static! { pub static ref START_FOLDER: std::path::PathBuf = std::fs::canonicalize(crate::io::Args::parse().path).unwrap_or_default(); } + +pub struct MenuColors { + pub first: Color, + pub second: Color, + pub selected_border: Color, + pub inert_border: Color, + pub palette_1: Color, + pub palette_2: Color, + pub palette_3: Color, + pub palette_4: Color, +} + +impl Default for MenuColors { + fn default() -> Self { + Self { + first: Color::Rgb(45, 250, 209), + second: Color::Rgb(230, 189, 87), + selected_border: Color::Rgb(45, 250, 209), + inert_border: Color::Rgb(248, 248, 248), + palette_1: Color::Rgb(45, 250, 209), + palette_2: Color::Rgb(230, 189, 87), + palette_3: Color::Rgb(230, 167, 255), + palette_4: Color::Rgb(59, 204, 255), + } + } +} + +impl MenuColors { + pub fn update(mut self) -> Self { + if let Ok(file) = File::open(path::Path::new( + &shellexpand::tilde(CONFIG_PATH).to_string(), + )) { + if let Ok(yaml) = + serde_yaml::from_reader::<std::fs::File, serde_yaml::value::Value>(file) + { + let menu_colors = &yaml["menu_colors"]; + update_attribute!(self.first, menu_colors, "first"); + update_attribute!(self.second, menu_colors, "second"); + update_attribute!(self.selected_border, menu_colors, "selected_border"); + update_attribute!(self.inert_border, menu_colors, "inert_border"); + update_attribute!(self.palette_1, menu_colors, "palette_1"); + update_attribute!(self.palette_2, menu_colors, "palette_2"); + update_attribute!(self.palette_3, menu_colors, "palette_3"); + update_attribute!(self.palette_4, menu_colors, "palette_4"); + } + } + self + } + + #[inline] + pub const fn palette(&self) -> [Attr; 4] { + [ + color_to_attr(self.palette_1), + color_to_attr(self.palette_2), + color_to_attr(self.palette_3), + color_to_attr(self.palette_4), + ] + } +} + +lazy_static::lazy_static! { + pub static ref MENU_COLORS: MenuColors = MenuColors::default().update(); +} |