2020-01-26 22:37:18 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig, Shell};
|
2019-09-30 12:10:35 +00:00
|
|
|
use crate::configs::battery::BatteryConfig;
|
2019-05-22 16:29:39 +00:00
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
/// Creates a module for the battery percentage and charging state
|
2019-07-02 20:12:53 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-08-26 01:52:44 +00:00
|
|
|
// TODO: Update when v1.0 printing refactor is implemented to only
|
|
|
|
// print escapes in a prompt context.
|
2020-01-26 22:37:18 +00:00
|
|
|
let percentage_char = match context.shell {
|
|
|
|
Shell::Zsh => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc`
|
2019-08-26 01:52:44 +00:00
|
|
|
_ => "%",
|
|
|
|
};
|
2019-05-22 16:29:39 +00:00
|
|
|
|
|
|
|
let battery_status = get_battery_status()?;
|
|
|
|
let BatteryStatus { state, percentage } = battery_status;
|
|
|
|
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("battery");
|
2019-10-02 05:55:17 +00:00
|
|
|
let battery_config: BatteryConfig = BatteryConfig::try_load(module.config);
|
2019-09-12 18:06:59 +00:00
|
|
|
|
|
|
|
// Parse config under `display`
|
2019-09-30 12:10:35 +00:00
|
|
|
let display_styles = &battery_config.display;
|
|
|
|
let display_style = display_styles
|
|
|
|
.iter()
|
|
|
|
.find(|display_style| percentage <= display_style.threshold as f32);
|
2019-09-12 18:06:59 +00:00
|
|
|
|
|
|
|
if let Some(display_style) = display_style {
|
|
|
|
// Set style based on percentage
|
2019-09-30 12:10:35 +00:00
|
|
|
module.set_style(display_style.style);
|
2019-09-12 18:06:59 +00:00
|
|
|
module.get_prefix().set_value("");
|
|
|
|
|
|
|
|
match state {
|
|
|
|
battery::State::Full => {
|
2019-10-02 05:55:17 +00:00
|
|
|
module.create_segment("full_symbol", &battery_config.full_symbol);
|
2019-09-12 18:06:59 +00:00
|
|
|
}
|
|
|
|
battery::State::Charging => {
|
2019-10-02 05:55:17 +00:00
|
|
|
module.create_segment("charging_symbol", &battery_config.charging_symbol);
|
2019-09-12 18:06:59 +00:00
|
|
|
}
|
|
|
|
battery::State::Discharging => {
|
2019-10-02 05:55:17 +00:00
|
|
|
module.create_segment("discharging_symbol", &battery_config.discharging_symbol);
|
2019-09-12 18:06:59 +00:00
|
|
|
}
|
2019-09-20 16:52:54 +00:00
|
|
|
battery::State::Unknown => {
|
|
|
|
log::debug!("Unknown detected");
|
2019-09-30 12:10:35 +00:00
|
|
|
if let Some(unknown_symbol) = battery_config.unknown_symbol {
|
2019-10-02 05:55:17 +00:00
|
|
|
module.create_segment("unknown_symbol", &unknown_symbol);
|
2019-09-30 12:10:35 +00:00
|
|
|
}
|
2019-09-20 16:52:54 +00:00
|
|
|
}
|
|
|
|
battery::State::Empty => {
|
2019-09-30 12:10:35 +00:00
|
|
|
if let Some(empty_symbol) = battery_config.empty_symbol {
|
2019-10-02 05:55:17 +00:00
|
|
|
module.create_segment("empty_symbol", &empty_symbol);
|
2019-09-30 12:10:35 +00:00
|
|
|
}
|
2019-09-20 16:52:54 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
log::debug!("Unhandled battery state `{}`", state);
|
|
|
|
return None;
|
|
|
|
}
|
2019-05-22 16:29:39 +00:00
|
|
|
}
|
2019-09-12 18:06:59 +00:00
|
|
|
|
|
|
|
let mut percent_string = Vec::<String>::with_capacity(2);
|
|
|
|
// Round the percentage to a whole number
|
|
|
|
percent_string.push(percentage.round().to_string());
|
|
|
|
percent_string.push(percentage_char.to_string());
|
2019-10-02 05:55:17 +00:00
|
|
|
module.create_segment(
|
|
|
|
"percentage",
|
|
|
|
&battery_config
|
|
|
|
.percentage
|
|
|
|
.with_value(percent_string.join("").as_ref()),
|
|
|
|
);
|
2019-09-12 18:06:59 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
} else {
|
|
|
|
None
|
2019-05-22 16:29:39 +00:00
|
|
|
}
|
2019-09-12 18:06:59 +00:00
|
|
|
}
|
2019-05-22 16:29:39 +00:00
|
|
|
|
|
|
|
fn get_battery_status() -> Option<BatteryStatus> {
|
|
|
|
let battery_manager = battery::Manager::new().ok()?;
|
2019-12-03 16:48:50 +00:00
|
|
|
let batteries = battery_manager.batteries().ok()?;
|
|
|
|
let battery_contructor = batteries
|
|
|
|
.filter_map(|battery| match battery {
|
|
|
|
Ok(battery) => {
|
|
|
|
log::debug!("Battery found: {:?}", battery);
|
|
|
|
Some(BatteryInfo {
|
|
|
|
energy: battery.energy().value,
|
|
|
|
energy_full: battery.energy_full().value,
|
|
|
|
state: battery.state(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
log::debug!("Unable to access battery information:\n{}", &e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fold(
|
|
|
|
BatteryInfo {
|
|
|
|
energy: 0.0,
|
|
|
|
energy_full: 0.0,
|
|
|
|
state: battery::State::Unknown,
|
|
|
|
},
|
|
|
|
|mut acc, x| {
|
|
|
|
acc.energy += x.energy;
|
|
|
|
acc.energy_full += x.energy_full;
|
|
|
|
acc.state = merge_battery_states(acc.state, x.state);
|
|
|
|
acc
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if battery_contructor.energy_full != 0.0 {
|
|
|
|
let battery = BatteryStatus {
|
|
|
|
percentage: battery_contructor.energy / battery_contructor.energy_full * 100.0,
|
|
|
|
state: battery_contructor.state,
|
|
|
|
};
|
|
|
|
log::debug!("Battery status: {:?}", battery);
|
|
|
|
Some(battery)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 16:29:39 +00:00
|
|
|
|
2019-12-03 16:48:50 +00:00
|
|
|
/// the merge returns Charging if at least one is charging
|
|
|
|
/// Discharging if at least one is Discharging
|
|
|
|
/// Full if both are Full or one is Full and the other Unknow
|
|
|
|
/// Empty if both are Empty or one is Empty and the other Unknow
|
|
|
|
/// Unknown otherwise
|
|
|
|
fn merge_battery_states(state1: battery::State, state2: battery::State) -> battery::State {
|
|
|
|
use battery::State::{Charging, Discharging, Unknown};
|
|
|
|
if state1 == Charging || state2 == Charging {
|
|
|
|
Charging
|
|
|
|
} else if state1 == Discharging || state2 == Discharging {
|
|
|
|
Discharging
|
|
|
|
} else if state1 == state2 {
|
|
|
|
state1
|
|
|
|
} else if state1 == Unknown {
|
|
|
|
state2
|
|
|
|
} else if state2 == Unknown {
|
|
|
|
state1
|
|
|
|
} else {
|
|
|
|
Unknown
|
2019-05-22 16:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 16:48:50 +00:00
|
|
|
struct BatteryInfo {
|
|
|
|
energy: f32,
|
|
|
|
energy_full: f32,
|
|
|
|
state: battery::State,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2019-05-22 16:29:39 +00:00
|
|
|
struct BatteryStatus {
|
|
|
|
percentage: f32,
|
|
|
|
state: battery::State,
|
|
|
|
}
|