Improve battery module

main
Tassilo Horn 3 years ago
parent 96201e5fba
commit 3a3f16554c
  1. 23
      swayrbar/src/module/battery.rs
  2. 6
      swayrbar/src/module/window.rs

@ -50,14 +50,13 @@ fn get_refreshed_batteries(
Ok(bats) Ok(bats)
} }
fn set_state(state: &Mutex<State>) { fn refresh_state(state: &mut State) {
// FIXME: Creating the Manager on every refresh is bad but internally // FIXME: Creating the Manager on every refresh is bad but internally
// it uses an Rc so if I keep it as a field of BarModuleBattery, that // it uses an Rc so if I keep it as a field of BarModuleBattery, that
// cannot be Sync. // cannot be Sync.
let manager = battery::Manager::new().unwrap(); let manager = battery::Manager::new().unwrap();
match get_refreshed_batteries(&manager) { match get_refreshed_batteries(&manager) {
Ok(bats) => { Ok(bats) => {
let mut state = state.lock().expect("Could not lock state.");
state.state_of_charge = state.state_of_charge =
bats.iter().map(|b| b.state_of_charge().value).sum::<f32>() bats.iter().map(|b| b.state_of_charge().value).sum::<f32>()
/ bats.len() as f32 / bats.len() as f32
@ -95,8 +94,7 @@ fn set_state(state: &Mutex<State>) {
} }
} }
fn get_text(fmt: &str, html_escape: bool, state: &Mutex<State>) -> String { fn get_text(fmt: &str, html_escape: bool, state: &State) -> String {
let state = state.lock().expect("Could not lock state.");
format_placeholders!(fmt, html_escape, { format_placeholders!(fmt, html_escape, {
"state_of_charge" => state.state_of_charge, "state_of_charge" => state.state_of_charge,
"state_of_health" => state.state_of_health, "state_of_health" => state.state_of_health,
@ -131,12 +129,10 @@ impl BarModuleFn for BarModuleBattery {
} }
fn build(&self) -> s::Block { fn build(&self) -> s::Block {
set_state(&self.state); let mut state = self.state.lock().expect("Could not lock state.");
let text = get_text( refresh_state(&mut state);
&self.config.format, let text =
self.config.is_html_escape(), get_text(&self.config.format, self.config.is_html_escape(), &state);
&self.state,
);
s::Block { s::Block {
name: Some(NAME.to_owned()), name: Some(NAME.to_owned()),
instance: Some(self.config.instance.clone()), instance: Some(self.config.instance.clone()),
@ -159,10 +155,7 @@ impl BarModuleFn for BarModuleBattery {
} }
fn subst_args<'a>(&'a self, cmd: &'a [String]) -> Option<Vec<String>> { fn subst_args<'a>(&'a self, cmd: &'a [String]) -> Option<Vec<String>> {
Some( let state = self.state.lock().expect("Could not lock state.");
cmd.iter() Some(cmd.iter().map(|arg| get_text(arg, false, &state)).collect())
.map(|arg| get_text(arg, false, &self.state))
.collect(),
)
} }
} }

@ -38,7 +38,7 @@ pub struct BarModuleWindow {
state: Mutex<State>, state: Mutex<State>,
} }
fn subst_placeholders(s: &str, state: &State, html_escape: bool) -> String { fn subst_placeholders(s: &str, html_escape: bool, state: &State) -> String {
format_placeholders!(s, html_escape, { format_placeholders!(s, html_escape, {
"title" | "name" => state.name.clone(), "title" | "name" => state.name.clone(),
"app_name" => state.app_name.clone(), "app_name" => state.app_name.clone(),
@ -98,8 +98,8 @@ impl BarModuleFn for BarModuleWindow {
state.pid = win.pid.unwrap_or(-1); state.pid = win.pid.unwrap_or(-1);
subst_placeholders( subst_placeholders(
&self.config.format, &self.config.format,
&*state,
self.config.is_html_escape(), self.config.is_html_escape(),
&*state,
) )
} }
None => String::new(), None => String::new(),
@ -142,7 +142,7 @@ impl BarModuleFn for BarModuleWindow {
let state = self.state.lock().expect("Could not lock state."); let state = self.state.lock().expect("Could not lock state.");
let cmd = cmd let cmd = cmd
.iter() .iter()
.map(|arg| subst_placeholders(arg, &*state, false)) .map(|arg| subst_placeholders(arg, false, &*state))
.collect(); .collect();
Some(cmd) Some(cmd)
} }

Loading…
Cancel
Save