From 345c91a5595ca8f16c3db0e0f349e37939216372 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Mon, 4 Apr 2022 16:07:39 +0200 Subject: [PATCH] Use refresh_interval from (default) Config; battery module improvements --- src/bar.rs | 8 +++++--- src/bar/config.rs | 7 +++++-- src/bar/module/battery.rs | 30 ++++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/bar.rs b/src/bar.rs index 7fb0ac2..d6ace69 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -27,6 +27,8 @@ pub fn start() { env_logger::Builder::from_env(Env::default().default_filter_or("warn")) .init(); + let config = config::Config::default(); + thread::spawn(handle_input); let mods: Vec> = vec![ module::window::BarModuleWindow::create( @@ -42,14 +44,14 @@ pub fn start() { module::date::BarModuleDate::default_config("0".to_owned()), ), ]; - generate_status(&mods); + generate_status(&mods, config.refresh_interval); } pub fn handle_input() { // TODO: Read stdin and react to click events. } -pub fn generate_status(mods: &[Box]) { +pub fn generate_status(mods: &[Box], refresh_interval: u64) { println!("{{\"version\": 1}}"); // status_command should output an infinite array meaning we emit an // opening [ and never the closing bracket. @@ -63,6 +65,6 @@ pub fn generate_status(mods: &[Box]) { let json = serde_json::to_string_pretty(&blocks) .unwrap_or_else(|_| "".to_string()); println!("{},", json); - thread::sleep(std::time::Duration::from_secs(1)); + thread::sleep(std::time::Duration::from_millis(refresh_interval)); } } diff --git a/src/bar/config.rs b/src/bar/config.rs index 8549ce1..38ae408 100644 --- a/src/bar/config.rs +++ b/src/bar/config.rs @@ -19,7 +19,10 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub struct Config { - pub refresh_interval: f32, + /// The status is refreshed every `refresh_interval` milliseconds. + 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, } @@ -35,7 +38,7 @@ pub struct ModuleConfig { impl Default for Config { fn default() -> Self { Config { - refresh_interval: 1.0, + refresh_interval: 1000, modules: vec!["date/0".to_owned()], module_configs: vec![], } diff --git a/src/bar/module/battery.rs b/src/bar/module/battery.rs index 65f1eed..450bf8e 100644 --- a/src/bar/module/battery.rs +++ b/src/bar/module/battery.rs @@ -20,6 +20,7 @@ use crate::bar::module::BarModuleFn; use crate::fmt_replace::fmt_replace; use battery as bat; use std::cell::RefCell; +use std::collections::HashSet; use swaybar_types as s; pub struct BarModuleBattery { @@ -49,6 +50,9 @@ fn get_text( ) -> String { match get_refreshed_batteries(manager) { Ok(bats) => { + if bats.is_empty() { + return String::new(); + } fmt_replace!(&cfg.format, cfg.html_escape, { "state_of_charge" => bats.iter() .map(|b| b.state_of_charge().value) @@ -58,10 +62,28 @@ fn get_text( .map(|b| b.state_of_health().value) .sum::() / bats.len() as f32 * 100_f32, - "state" => bats.iter() - .map(|b| format!("{:?}", b.state())) - .next() - .unwrap_or_default(), + "state" => { + let states = bats.iter() + .map(|b| format!("{:?}", b.state())) + .collect::>(); + if states.len() == 1 { + states.iter().next().unwrap().to_owned() + } else { + let mut comma_sep_string = String::from("["); + let mut first = true; + for state in states { + if first { + comma_sep_string = comma_sep_string + &state; + first = false; + } else { + comma_sep_string = comma_sep_string + + ", " + &state; + } + } + comma_sep_string += "]"; + comma_sep_string + } + }, }) } Err(err) => format!("{}", err),