| 1 | //! Uses the battery crate. |
| 2 | //! |
| 3 | //! Covers battery usage for: |
| 4 | //! - Linux 2.6.39+ |
| 5 | //! - MacOS 10.10+ |
| 6 | //! - iOS |
| 7 | //! - Windows 7+ |
| 8 | //! - FreeBSD |
| 9 | //! - DragonFlyBSD |
| 10 | //! |
| 11 | //! For more information, refer to the [starship_battery](https://github.com/starship/rust-battery) repo/docs. |
| 12 | |
| 13 | use starship_battery::{ |
| 14 | Battery, Manager, State, |
| 15 | units::{power::watt, ratio::percent, time::second}, |
| 16 | }; |
| 17 | |
| 18 | /// Battery state. |
| 19 | #[derive(Debug, Clone)] |
| 20 | pub enum BatteryState { |
| 21 | Charging { |
| 22 | /// Time to full in seconds. |
| 23 | time_to_full: Option<u32>, |
| 24 | }, |
| 25 | Discharging { |
| 26 | /// Time to empty in seconds. |
| 27 | time_to_empty: Option<u32>, |
| 28 | }, |
| 29 | Empty, |
| 30 | Full, |
| 31 | Unknown, |
| 32 | } |
| 33 | |
| 34 | impl BatteryState { |
| 35 | /// Return the string representation. |
| 36 | pub fn as_str(&self) -> &'static str { |
| 37 | match self { |
| 38 | BatteryState::Charging { .. } => "Charging", |
| 39 | BatteryState::Discharging { .. } => "Discharging", |
| 40 | BatteryState::Empty => "Empty", |
| 41 | BatteryState::Full => "Full", |
| 42 | BatteryState::Unknown => "Unknown", |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Clone)] |
| 48 | pub struct BatteryData { |
| 49 | /// Current charge percent. |
| 50 | pub charge_percent: f64, |
| 51 | /// Power consumption, in watts. |
| 52 | pub power_consumption: f64, |
| 53 | /// Reported battery health. |
| 54 | pub health_percent: f64, |
| 55 | /// The current battery "state" (e.g. is it full, charging, etc.). |
| 56 | pub state: BatteryState, |
| 57 | } |
| 58 | |
| 59 | impl BatteryData { |
| 60 | pub fn watt_consumption(&self) -> String { |
| 61 | format!("{:.2}W", self.power_consumption) |
| 62 | } |
| 63 | |
| 64 | pub fn health(&self) -> String { |
| 65 | format!("{:.2}%", self.health_percent) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn refresh_batteries(manager: &Manager, batteries: &mut [Battery]) -> Vec<BatteryData> { |
| 70 | batteries |
| 71 | .iter_mut() |
| 72 | .filter_map(|battery| { |
| 73 | if manager.refresh(battery).is_ok() { |
| 74 | Some(BatteryData { |
| 75 | charge_percent: f64::from(battery.state_of_charge().get::<percent>()), |
| 76 | power_consumption: f64::from(battery.energy_rate().get::<watt>()), |
| 77 | health_percent: f64::from(battery.state_of_health().get::<percent>()), |
| 78 | state: match battery.state() { |
| 79 | State::Unknown => BatteryState::Unknown, |
| 80 | State::Charging => BatteryState::Charging { |
| 81 | time_to_full: { |
| 82 | let optional_time = battery.time_to_full(); |
| 83 | optional_time.map(|time| f64::from(time.get::<second>()) as u32) |
| 84 | }, |
| 85 | }, |
| 86 | State::Discharging => BatteryState::Discharging { |
| 87 | time_to_empty: { |
| 88 | let optional_time = battery.time_to_empty(); |
| 89 | optional_time.map(|time| f64::from(time.get::<second>()) as u32) |
| 90 | }, |
| 91 | }, |
| 92 | State::Empty => BatteryState::Empty, |
| 93 | State::Full => BatteryState::Full, |
| 94 | }, |
| 95 | }) |
| 96 | } else { |
| 97 | None |
| 98 | } |
| 99 | }) |
| 100 | .collect::<Vec<_>>() |
| 101 | } |