diff --git a/swayr/src/shared/cfg.rs b/swayr/src/shared/cfg.rs index 490f0a8..428c644 100644 --- a/swayr/src/shared/cfg.rs +++ b/swayr/src/shared/cfg.rs @@ -54,7 +54,7 @@ where file.write_all(content.as_str().as_bytes()).unwrap(); } -pub fn load_config<'a, T>(project: &str) -> T +pub fn load_config(project: &str) -> T where T: Serialize + DeserializeOwned + Default, { diff --git a/swayrbar/src/config.rs b/swayrbar/src/config.rs index cab23fc..52a7908 100644 --- a/swayrbar/src/config.rs +++ b/swayrbar/src/config.rs @@ -34,8 +34,14 @@ pub struct ModuleConfig { pub name: String, pub instance: String, pub format: String, - pub html_escape: bool, - pub on_click: HashMap>, + pub html_escape: Option, + pub on_click: Option>>, +} + +impl ModuleConfig { + pub fn is_html_escape(&self) -> bool { + self.html_escape.unwrap_or(false) + } } impl Default for Config { @@ -49,6 +55,9 @@ impl Default for Config { crate::module::sysinfo::BarModuleSysInfo::default_config( "0".to_owned(), ), + crate::module::battery::BarModuleBattery::default_config( + "0".to_owned(), + ), crate::module::date::BarModuleDate::default_config( "0".to_owned(), ), diff --git a/swayrbar/src/module.rs b/swayrbar/src/module.rs index 55e384b..0324ea4 100644 --- a/swayrbar/src/module.rs +++ b/swayrbar/src/module.rs @@ -38,7 +38,7 @@ pub trait BarModuleFn: Sync + Send { ) -> Option<&HashMap>> { let cfg = self.get_config(); if name == cfg.name && instance == cfg.instance { - Some(&cfg.on_click) + cfg.on_click.as_ref() } else { None } diff --git a/swayrbar/src/module/battery.rs b/swayrbar/src/module/battery.rs index 8f22879..8e46c55 100644 --- a/swayrbar/src/module/battery.rs +++ b/swayrbar/src/module/battery.rs @@ -19,7 +19,7 @@ use crate::config; use crate::module::BarModuleFn; use crate::shared::fmt::format_placeholders; use battery as bat; -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use swaybar_types as s; const NAME: &str = "battery"; @@ -52,7 +52,7 @@ fn get_text(cfg: &config::ModuleConfig) -> String { if bats.is_empty() { return String::new(); } - format_placeholders!(&cfg.format, cfg.html_escape, { + format_placeholders!(&cfg.format, cfg.is_html_escape(), { "state_of_charge" => bats.iter() .map(|b| b.state_of_charge().value) .sum::() @@ -99,8 +99,8 @@ impl BarModuleFn for BarModuleBattery { name: NAME.to_owned(), instance, format: "🔋 Bat: {state_of_charge:{:5.1}}%, {state}, Health: {state_of_health:{:5.1}}%".to_owned(), - html_escape: false, - on_click: HashMap::new() + html_escape: Some(false), + on_click: None, } } diff --git a/swayrbar/src/module/date.rs b/swayrbar/src/module/date.rs index 3409d99..7d799f8 100644 --- a/swayrbar/src/module/date.rs +++ b/swayrbar/src/module/date.rs @@ -15,8 +15,6 @@ //! The date `swayrbar` module. -use std::collections::HashMap; - use crate::module::config; use crate::module::BarModuleFn; use swaybar_types as s; @@ -34,11 +32,11 @@ impl BarModuleFn for BarModuleDate { fn default_config(instance: String) -> config::ModuleConfig { config::ModuleConfig { - name: "date".to_owned(), + name: NAME.to_owned(), instance, format: "⏰ %F %X".to_owned(), - html_escape: false, - on_click: HashMap::new(), + html_escape: Some(false), + on_click: None, } } diff --git a/swayrbar/src/module/sysinfo.rs b/swayrbar/src/module/sysinfo.rs index 721788e..43d8b6b 100644 --- a/swayrbar/src/module/sysinfo.rs +++ b/swayrbar/src/module/sysinfo.rs @@ -96,13 +96,13 @@ impl BarModuleFn for BarModuleSysInfo { fn default_config(instance: String) -> config::ModuleConfig { config::ModuleConfig { - name: "sysinfo".to_owned(), + name: NAME.to_owned(), instance, format: "💻 CPU: {cpu_usage:{:4.1}}% Mem: {mem_usage:{:4.1}}% Load: {load_avg_1:{:4.2}} / {load_avg_5:{:4.2}} / {load_avg_15:{:4.2}}".to_owned(), - html_escape: false, - on_click: HashMap::from([ - ("Left".to_owned(), - vec!["foot".to_owned(), "htop".to_owned()])]), + html_escape: Some(false), + on_click: Some(HashMap::from([ + ("Left".to_owned(), + vec!["foot".to_owned(), "htop".to_owned()])])), } } @@ -117,7 +117,8 @@ impl BarModuleFn for BarModuleSysInfo { instance: Some(self.config.instance.clone()), full_text: { let mut sys = self.system.lock().unwrap(); - format_placeholders!(&self.config.format, self.config.html_escape, { + format_placeholders!(&self.config.format, + self.config.is_html_escape(), { "cpu_usage" => get_cpu_usage(&mut sys, &updater), "mem_usage" => get_memory_usage(&mut sys, &updater), "load_avg_1" => get_load_average(&mut sys, diff --git a/swayrbar/src/module/window.rs b/swayrbar/src/module/window.rs index f8efa0f..e2696a7 100644 --- a/swayrbar/src/module/window.rs +++ b/swayrbar/src/module/window.rs @@ -40,14 +40,14 @@ impl BarModuleFn for BarModuleWindow { name: NAME.to_owned(), instance, format: "🪟 {title} — {app_name}".to_owned(), - html_escape: false, - on_click: HashMap::from([( + html_escape: Some(false), + on_click: Some(HashMap::from([( "Left".to_owned(), vec![ "swayr".to_owned(), "switch-to-urgent-or-lru-window".to_owned(), ], - )]), + )])), } } @@ -62,7 +62,8 @@ impl BarModuleFn for BarModuleWindow { .find(|n| n.focused && n.get_type() == ipc::Type::Window); let text = match focused_win { Some(win) => { - format_placeholders!(&self.config.format, self.config.html_escape, { + format_placeholders!(&self.config.format, + self.config.is_html_escape(), { "title" | "name" => win.get_name(), "app_name" => win.get_app_name(), })