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