more logging, pango fonts to settings

This commit is contained in:
emilis 2024-03-01 22:26:21 +00:00
parent 3ded017eee
commit 01c5752868
4 changed files with 68 additions and 36 deletions

View File

@ -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<Self, ConfigError> {
info!(
"reading config from: {}",
path.to_str().unwrap_or("<not a valid utf8 string>")
path.to_str().unwrap_or_log("<not a valid utf8 string>")
);
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("<not a valid utf8 string>")
path.to_str().unwrap_or_log("<not a valid utf8 string>")
);
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,

View File

@ -83,6 +83,13 @@ impl Client {
Ok(())
}
pub fn get_str_attr(&self, attr: String) -> Result<String, CommandError> {
self.query(HlwmCommand::GetAttr(attr))?
.first()
.cloned()
.ok_or(CommandError::Empty)
}
pub fn get_attr(&self, attr: String) -> Result<Attribute, CommandError> {
let attr_type = self
.query(HlwmCommand::AttrType(attr.clone()))?

View File

@ -29,3 +29,24 @@ where
}
}
}
impl<T> UnwrapLog for Option<T>
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::<Self::Target>()
);
debug!("^ defaulting to {default:#?}");
default
}
}
}
}

View File

@ -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) {