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 { impl Config {
const FONT: &'static str = "theme.my_font"; const FONT: &'static str = "theme.my_font";
const FONT_BOLD: &'static str = "theme.my_font_bold"; 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 SERVICES: &'static str = "settings.my_services";
const MOD_KEY: &'static str = "settings.my_mod_key"; const MOD_KEY: &'static str = "settings.my_mod_key";
pub fn from_file(path: &Path) -> Result<Self, ConfigError> { pub fn from_file(path: &Path) -> Result<Self, ConfigError> {
info!( info!(
"reading config from: {}", "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)?) Self::deserialize(&fs::read_to_string(path)?)
} }
@ -177,7 +179,7 @@ impl Config {
pub fn write_to_file(&self, path: &Path) -> Result<(), ConfigError> { pub fn write_to_file(&self, path: &Path) -> Result<(), ConfigError> {
info!( info!(
"saving config to: {}", "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()?)?) Ok(fs::write(path, self.serialize()?)?)
} }
@ -194,7 +196,7 @@ impl Config {
Ok(format!( Ok(format!(
"{home}/.config/herbstluftwm/hlctl.toml", "{home}/.config/herbstluftwm/hlctl.toml",
home = env::var("XDG_CONFIG_HOME") 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() .flatten()
.chain([ .chain([HlwmCommand::And {
HlwmCommand::And { separator: Separator::Comma,
separator: Separator::Comma, commands: vec![
commands: vec![ HlwmCommand::Substitute {
HlwmCommand::Substitute { identifier: "CURRENT_INDEX".into(),
identifier: "CURRENT_INDEX".into(), attribute_path: "tags.focus.index".into(),
attribute_path: "tags.focus.index".into(), command: Box::new(HlwmCommand::Compare {
command: Box::new(HlwmCommand::Compare { attribute: "tags.by-name.default.index".into(),
attribute: "tags.by-name.default.index".into(), operator: Operator::Equal,
operator: Operator::Equal, value: "CURRENT_INDEX".into(),
value: "CURRENT_INDEX".into(), }),
}), },
}, HlwmCommand::UseIndex {
HlwmCommand::UseIndex { index: Index::Relative(1),
index: Index::Relative(1), skip_visible: false,
skip_visible: false, },
}, HlwmCommand::MergeTag {
], tag: "default".to_string(),
} target: Some(hlwm::Tag::Name(self.tags.first().unwrap().to_string())),
.to_try(), },
HlwmCommand::MergeTag { ],
tag: "default".to_string(), }
target: Some(hlwm::Tag::Name(self.tags.first().unwrap().to_string())), .to_try()])
},
])
.collect() .collect()
} }
@ -409,15 +409,18 @@ impl Config {
let client = Client::new(); let client = Client::new();
let default = Config::default(); let default = Config::default();
Config { Config {
font: setting( font: client
Self::FONT, .get_str_attr(Self::FONT.to_string())
|f| -> Result<_, Infallible> { Ok(f.to_string()) }, .unwrap_or_log(default.font),
default.font,
),
font_bold: client font_bold: client
.get_attr(ThemeAttr::TitleFont(String::new()).attr_path()) .get_str_attr(Self::FONT_BOLD.to_string())
.map(|a| a.to_string())
.unwrap_or_log(default.font_bold), .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), mod_key: setting(Self::MOD_KEY, Key::from_str, default.mod_key),
services: setting( services: setting(
Self::SERVICES, Self::SERVICES,

View File

@ -83,6 +83,13 @@ impl Client {
Ok(()) 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> { pub fn get_attr(&self, attr: String) -> Result<Attribute, CommandError> {
let attr_type = self let attr_type = self
.query(HlwmCommand::AttrType(attr.clone()))? .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 clap::{Parser, Subcommand};
use config::Config; use config::Config;
use log::{error, info}; use log::{error, info};
use logerr::UnwrapLog;
pub mod cmd; pub mod cmd;
mod config; mod config;
@ -53,7 +54,7 @@ fn main() {
} }
fn load_or_default_config() -> Config { 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) { fn x_set_root(path: PathBuf) {