From 01c57528683a4bc3c910190f6ffdcd67210997da Mon Sep 17 00:00:00 2001 From: emilis Date: Fri, 1 Mar 2024 22:26:21 +0000 Subject: [PATCH] more logging, pango fonts to settings --- src/config.rs | 73 +++++++++++++++++++++++++------------------------ src/hlwm/mod.rs | 7 +++++ src/logerr.rs | 21 ++++++++++++++ src/main.rs | 3 +- 4 files changed, 68 insertions(+), 36 deletions(-) diff --git a/src/config.rs b/src/config.rs index 6d45e98..68ff7e7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -163,13 +163,15 @@ impl<'de> Deserialize<'de> for SetAttribute { impl Config { const FONT: &'static str = "theme.my_font"; const FONT_BOLD: &'static str = "theme.my_font_bold"; + const FONT_PANGO: &'static str = "theme.my_font_pango"; + const FONT_PANGO_BOLD: &'static str = "theme.my_font_pango_bold"; const SERVICES: &'static str = "settings.my_services"; const MOD_KEY: &'static str = "settings.my_mod_key"; pub fn from_file(path: &Path) -> Result { info!( "reading config from: {}", - path.to_str().unwrap_or("") + path.to_str().unwrap_or_log("") ); Self::deserialize(&fs::read_to_string(path)?) } @@ -177,7 +179,7 @@ impl Config { pub fn write_to_file(&self, path: &Path) -> Result<(), ConfigError> { info!( "saving config to: {}", - path.to_str().unwrap_or("") + path.to_str().unwrap_or_log("") ); Ok(fs::write(path, self.serialize()?)?) } @@ -194,7 +196,7 @@ impl Config { Ok(format!( "{home}/.config/herbstluftwm/hlctl.toml", home = env::var("XDG_CONFIG_HOME") - .unwrap_or(env::var("HOME").map_err(|_| ConfigError::HomeNotSet)?) + .unwrap_or_log(env::var("HOME").map_err(|_| ConfigError::HomeNotSet)?) )) } @@ -362,31 +364,29 @@ impl Config { ] }) .flatten() - .chain([ - HlwmCommand::And { - separator: Separator::Comma, - commands: vec![ - HlwmCommand::Substitute { - identifier: "CURRENT_INDEX".into(), - attribute_path: "tags.focus.index".into(), - command: Box::new(HlwmCommand::Compare { - attribute: "tags.by-name.default.index".into(), - operator: Operator::Equal, - value: "CURRENT_INDEX".into(), - }), - }, - HlwmCommand::UseIndex { - index: Index::Relative(1), - skip_visible: false, - }, - ], - } - .to_try(), - HlwmCommand::MergeTag { - tag: "default".to_string(), - target: Some(hlwm::Tag::Name(self.tags.first().unwrap().to_string())), - }, - ]) + .chain([HlwmCommand::And { + separator: Separator::Comma, + commands: vec![ + HlwmCommand::Substitute { + identifier: "CURRENT_INDEX".into(), + attribute_path: "tags.focus.index".into(), + command: Box::new(HlwmCommand::Compare { + attribute: "tags.by-name.default.index".into(), + operator: Operator::Equal, + value: "CURRENT_INDEX".into(), + }), + }, + HlwmCommand::UseIndex { + index: Index::Relative(1), + skip_visible: false, + }, + HlwmCommand::MergeTag { + tag: "default".to_string(), + target: Some(hlwm::Tag::Name(self.tags.first().unwrap().to_string())), + }, + ], + } + .to_try()]) .collect() } @@ -409,15 +409,18 @@ impl Config { let client = Client::new(); let default = Config::default(); Config { - font: setting( - Self::FONT, - |f| -> Result<_, Infallible> { Ok(f.to_string()) }, - default.font, - ), + font: client + .get_str_attr(Self::FONT.to_string()) + .unwrap_or_log(default.font), font_bold: client - .get_attr(ThemeAttr::TitleFont(String::new()).attr_path()) - .map(|a| a.to_string()) + .get_str_attr(Self::FONT_BOLD.to_string()) .unwrap_or_log(default.font_bold), + font_pango: client + .get_str_attr(Self::FONT_PANGO.to_string()) + .unwrap_or_log(default.font_pango), + font_pango_bold: client + .get_str_attr(Self::FONT_PANGO_BOLD.to_string()) + .unwrap_or_log(default.font_pango_bold), mod_key: setting(Self::MOD_KEY, Key::from_str, default.mod_key), services: setting( Self::SERVICES, diff --git a/src/hlwm/mod.rs b/src/hlwm/mod.rs index 70f42a8..d784a50 100644 --- a/src/hlwm/mod.rs +++ b/src/hlwm/mod.rs @@ -83,6 +83,13 @@ impl Client { Ok(()) } + pub fn get_str_attr(&self, attr: String) -> Result { + self.query(HlwmCommand::GetAttr(attr))? + .first() + .cloned() + .ok_or(CommandError::Empty) + } + pub fn get_attr(&self, attr: String) -> Result { let attr_type = self .query(HlwmCommand::AttrType(attr.clone()))? diff --git a/src/logerr.rs b/src/logerr.rs index 2d785b1..19d848b 100644 --- a/src/logerr.rs +++ b/src/logerr.rs @@ -29,3 +29,24 @@ where } } } + +impl UnwrapLog for Option +where + T: Debug, +{ + type Target = T; + + fn unwrap_or_log(self, default: Self::Target) -> Self::Target { + match self { + Some(v) => v, + None => { + error!( + "[{}] unwrap_or_log was None", + std::any::type_name::() + ); + debug!("^ defaulting to {default:#?}"); + default + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 83f9053..ebb0ce8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf}; use clap::{Parser, Subcommand}; use config::Config; use log::{error, info}; +use logerr::UnwrapLog; pub mod cmd; mod config; @@ -53,7 +54,7 @@ fn main() { } fn load_or_default_config() -> Config { - Config::from_file(&Path::new(&Config::default_path().unwrap())).unwrap_or(Config::default()) + Config::from_file(&Path::new(&Config::default_path().unwrap())).unwrap_or_log(Config::default()) } fn x_set_root(path: PathBuf) {