From 5e80619761079d939fd543177748e7e919fd1a8e Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Fri, 8 Apr 2022 22:34:03 +0200 Subject: [PATCH] swayrbar config --- swayr/src/config.rs | 2 +- swayrbar/src/bar.rs | 39 +++++++++++++++++++--------------- swayrbar/src/config.rs | 23 ++++++++++++++++---- swayrbar/src/module/battery.rs | 4 ++-- swayrbar/src/module/date.rs | 8 ++----- swayrbar/src/module/sysinfo.rs | 6 ++++-- swayrbar/src/module/window.rs | 10 +++++++-- 7 files changed, 58 insertions(+), 34 deletions(-) diff --git a/swayr/src/config.rs b/swayr/src/config.rs index 88b49a3..c095c8c 100644 --- a/swayr/src/config.rs +++ b/swayr/src/config.rs @@ -305,7 +305,7 @@ pub fn load_config() -> Config { } #[test] -fn test_load_config() { +fn test_load_swayr_config() { let cfg = cfg::load_config::("swayr"); println!("{:?}", cfg); } diff --git a/swayrbar/src/bar.rs b/swayrbar/src/bar.rs index 493e2b6..1d4da55 100644 --- a/swayrbar/src/bar.rs +++ b/swayrbar/src/bar.rs @@ -29,25 +29,30 @@ pub fn start() { env_logger::Builder::from_env(Env::default().default_filter_or("warn")) .init(); - let config = config::Config::default(); - - let mods: Arc>> = Arc::new(vec![ - module::window::BarModuleWindow::create( - module::window::BarModuleWindow::default_config("0".to_owned()), - ), - module::sysinfo::BarModuleSysInfo::create( - module::sysinfo::BarModuleSysInfo::default_config("0".to_owned()), - ), - module::battery::BarModuleBattery::create( - module::battery::BarModuleBattery::default_config("0".to_owned()), - ), - module::date::BarModuleDate::create( - module::date::BarModuleDate::default_config("0".to_owned()), - ), - ]); + let config = config::load_config(); + let refresh_interval = config.refresh_interval; + let mods: Arc>> = Arc::new(create_modules(config)); let mods_for_input = mods.clone(); thread::spawn(move || handle_input(mods_for_input)); - generate_status(&mods, config.refresh_interval); + generate_status(&mods, refresh_interval); +} + +fn create_modules(config: config::Config) -> Vec> { + let mut mods = vec![]; + for mc in config.modules { + let m = match mc.name.as_str() { + "window" => module::window::BarModuleWindow::create(mc), + "sysinfo" => module::sysinfo::BarModuleSysInfo::create(mc), + "battery" => module::battery::BarModuleBattery::create(mc), + "date" => module::date::BarModuleDate::create(mc), + unknown => { + log::warn!("Unknown module name '{}'. Ignoring...", unknown); + continue; + } + }; + mods.push(m); + } + mods } pub fn handle_input(mods: Arc>>) { diff --git a/swayrbar/src/config.rs b/swayrbar/src/config.rs index 21ef762..cab23fc 100644 --- a/swayrbar/src/config.rs +++ b/swayrbar/src/config.rs @@ -16,6 +16,7 @@ //! TOML configuration for swayrbar. use crate::module::BarModuleFn; +use crate::shared::cfg; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -25,8 +26,7 @@ pub struct Config { pub refresh_interval: u64, /// The list of modules to display in the given order, each one specified /// as `"/"`. - pub modules: Vec, - pub module_configs: Vec, + pub modules: Vec, } #[derive(Debug, Serialize, Deserialize)] @@ -42,8 +42,13 @@ impl Default for Config { fn default() -> Self { Config { refresh_interval: 1000, - modules: vec!["date/0".to_owned()], - module_configs: vec![ + modules: vec![ + crate::module::window::BarModuleWindow::default_config( + "0".to_owned(), + ), + crate::module::sysinfo::BarModuleSysInfo::default_config( + "0".to_owned(), + ), crate::module::date::BarModuleDate::default_config( "0".to_owned(), ), @@ -51,3 +56,13 @@ impl Default for Config { } } } + +pub fn load_config() -> Config { + cfg::load_config::("swayrbar") +} + +#[test] +fn test_load_swayrbar_config() { + let cfg = cfg::load_config::("swayrbar"); + println!("{:?}", cfg); +} diff --git a/swayrbar/src/module/battery.rs b/swayrbar/src/module/battery.rs index 782238d..8f22879 100644 --- a/swayrbar/src/module/battery.rs +++ b/swayrbar/src/module/battery.rs @@ -99,7 +99,7 @@ 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: true, + html_escape: false, on_click: HashMap::new() } } @@ -114,7 +114,7 @@ impl BarModuleFn for BarModuleBattery { name: Some(NAME.to_owned()), instance: Some(self.config.instance.clone()), full_text: text, - align: Some(s::Align::Right), + align: Some(s::Align::Left), markup: Some(s::Markup::Pango), short_text: None, color: None, diff --git a/swayrbar/src/module/date.rs b/swayrbar/src/module/date.rs index ccd7f17..3409d99 100644 --- a/swayrbar/src/module/date.rs +++ b/swayrbar/src/module/date.rs @@ -38,11 +38,7 @@ impl BarModuleFn for BarModuleDate { instance, format: "⏰ %F %X".to_owned(), html_escape: false, - // TODO: Only for testing. - on_click: HashMap::from([( - "Left".to_owned(), - vec!["foot".to_owned(), "htop".to_owned()], - )]), + on_click: HashMap::new(), } } @@ -56,7 +52,7 @@ impl BarModuleFn for BarModuleDate { name: Some(NAME.to_owned()), instance: Some(self.config.instance.clone()), full_text: text, - align: Some(s::Align::Right), + align: Some(s::Align::Left), markup: Some(s::Markup::Pango), short_text: None, color: None, diff --git a/swayrbar/src/module/sysinfo.rs b/swayrbar/src/module/sysinfo.rs index d1e9104..721788e 100644 --- a/swayrbar/src/module/sysinfo.rs +++ b/swayrbar/src/module/sysinfo.rs @@ -99,8 +99,10 @@ impl BarModuleFn for BarModuleSysInfo { name: "sysinfo".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: true, - on_click: HashMap::new(), + html_escape: false, + on_click: HashMap::from([ + ("Left".to_owned(), + vec!["foot".to_owned(), "htop".to_owned()])]), } } diff --git a/swayrbar/src/module/window.rs b/swayrbar/src/module/window.rs index 6a1640e..f8efa0f 100644 --- a/swayrbar/src/module/window.rs +++ b/swayrbar/src/module/window.rs @@ -40,8 +40,14 @@ impl BarModuleFn for BarModuleWindow { name: NAME.to_owned(), instance, format: "🪟 {title} — {app_name}".to_owned(), - html_escape: true, - on_click: HashMap::new(), + html_escape: false, + on_click: HashMap::from([( + "Left".to_owned(), + vec![ + "swayr".to_owned(), + "switch-to-urgent-or-lru-window".to_owned(), + ], + )]), } }