Use refresh_interval from (default) Config; battery module improvements

main
Tassilo Horn 3 years ago
parent 588b7153fb
commit 345c91a559
  1. 8
      src/bar.rs
  2. 7
      src/bar/config.rs
  3. 30
      src/bar/module/battery.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<Box<dyn BarModuleFn>> = 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<dyn BarModuleFn>]) {
pub fn generate_status(mods: &[Box<dyn BarModuleFn>], 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<dyn BarModuleFn>]) {
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));
}
}

@ -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 `"<module_type>/<instance>"`.
pub modules: Vec<String>,
pub module_configs: Vec<ModuleConfig>,
}
@ -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![],
}

@ -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::<f32>()
/ 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::<HashSet<String>>();
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),

Loading…
Cancel
Save