Improve swayrbar configuration

main
Tassilo Horn 3 years ago
parent 5e80619761
commit 8d37fd95d1
  1. 2
      swayr/src/shared/cfg.rs
  2. 13
      swayrbar/src/config.rs
  3. 2
      swayrbar/src/module.rs
  4. 8
      swayrbar/src/module/battery.rs
  5. 8
      swayrbar/src/module/date.rs
  6. 11
      swayrbar/src/module/sysinfo.rs
  7. 9
      swayrbar/src/module/window.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<T>(project: &str) -> T
where
T: Serialize + DeserializeOwned + Default,
{

@ -34,8 +34,14 @@ pub struct ModuleConfig {
pub name: String,
pub instance: String,
pub format: String,
pub html_escape: bool,
pub on_click: HashMap<String, Vec<String>>,
pub html_escape: Option<bool>,
pub on_click: Option<HashMap<String, Vec<String>>>,
}
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(),
),

@ -38,7 +38,7 @@ pub trait BarModuleFn: Sync + Send {
) -> Option<&HashMap<String, Vec<String>>> {
let cfg = self.get_config();
if name == cfg.name && instance == cfg.instance {
Some(&cfg.on_click)
cfg.on_click.as_ref()
} else {
None
}

@ -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::<f32>()
@ -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,
}
}

@ -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,
}
}

@ -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([
html_escape: Some(false),
on_click: Some(HashMap::from([
("Left".to_owned(),
vec!["foot".to_owned(), "htop".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,

@ -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(),
})

Loading…
Cancel
Save