2019-05-22 16:29:39 +00:00
|
|
|
use ansi_term::Color;
|
|
|
|
|
|
|
|
use super::{Context, Module};
|
|
|
|
|
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-05-22 16:29:39 +00:00
|
|
|
const BATTERY_FULL: &str = "•";
|
|
|
|
const BATTERY_CHARGING: &str = "⇡";
|
|
|
|
const BATTERY_DISCHARGING: &str = "⇣";
|
|
|
|
const BATTERY_THRESHOLD: f32 = 10.0;
|
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.
|
|
|
|
let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default();
|
|
|
|
let percentage_char = match shell.as_str() {
|
|
|
|
"zsh" => "%%", // % is an escape in zsh, see PROMPT in `man zshmisc`
|
|
|
|
_ => "%",
|
|
|
|
};
|
2019-05-22 16:29:39 +00:00
|
|
|
|
|
|
|
let battery_status = get_battery_status()?;
|
|
|
|
let BatteryStatus { state, percentage } = battery_status;
|
|
|
|
|
|
|
|
if percentage > BATTERY_THRESHOLD {
|
|
|
|
log::debug!(
|
|
|
|
"Battery percentage is higher than threshold ({} > {})",
|
|
|
|
percentage,
|
|
|
|
BATTERY_THRESHOLD
|
|
|
|
);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Set style based on percentage when threshold is modifiable
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("battery");
|
2019-09-08 00:33:06 +00:00
|
|
|
let module_style = module
|
|
|
|
.config_value_style("style")
|
|
|
|
.unwrap_or_else(|| Color::Red.bold());
|
|
|
|
module.set_style(module_style);
|
2019-05-22 16:29:39 +00:00
|
|
|
module.get_prefix().set_value("");
|
|
|
|
|
|
|
|
match state {
|
|
|
|
battery::State::Full => {
|
2019-07-19 20:18:52 +00:00
|
|
|
module.new_segment("full_symbol", BATTERY_FULL);
|
2019-05-22 16:29:39 +00:00
|
|
|
}
|
|
|
|
battery::State::Charging => {
|
2019-07-19 20:18:52 +00:00
|
|
|
module.new_segment("charging_symbol", BATTERY_CHARGING);
|
2019-05-22 16:29:39 +00:00
|
|
|
}
|
|
|
|
battery::State::Discharging => {
|
2019-07-19 20:18:52 +00:00
|
|
|
module.new_segment("discharging_symbol", BATTERY_DISCHARGING);
|
2019-05-22 16:29:39 +00:00
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut percent_string = Vec::<String>::with_capacity(2);
|
|
|
|
// Round the percentage to a whole number
|
|
|
|
percent_string.push(percentage.round().to_string());
|
2019-08-26 01:52:44 +00:00
|
|
|
percent_string.push(percentage_char.to_string());
|
2019-07-14 15:15:47 +00:00
|
|
|
module.new_segment("percentage", percent_string.join("").as_ref());
|
2019-05-22 16:29:39 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_battery_status() -> Option<BatteryStatus> {
|
|
|
|
let battery_manager = battery::Manager::new().ok()?;
|
|
|
|
match battery_manager.batteries().ok()?.next() {
|
|
|
|
Some(Ok(battery)) => {
|
|
|
|
log::debug!("Battery found: {:?}", battery);
|
|
|
|
let battery_status = BatteryStatus {
|
|
|
|
percentage: battery.state_of_charge().value * 100.0,
|
|
|
|
state: battery.state(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(battery_status)
|
|
|
|
}
|
|
|
|
Some(Err(e)) => {
|
2019-07-02 20:12:53 +00:00
|
|
|
log::debug!("Unable to access battery information:\n{}", &e);
|
2019-05-22 16:29:39 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
log::debug!("No batteries found");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BatteryStatus {
|
|
|
|
percentage: f32,
|
|
|
|
state: battery::State,
|
|
|
|
}
|