diff --git a/src/config/battery.rs b/src/config/battery.rs index 2d4599d..7e7aa4f 100644 --- a/src/config/battery.rs +++ b/src/config/battery.rs @@ -4,6 +4,7 @@ use std::default::Default; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Battery { pub icon: String, + pub charging_icon: String, pub enabled: bool, pub delay: f64, } @@ -11,6 +12,7 @@ pub struct Battery { impl Default for Battery { fn default() -> Self { Battery { + charging_icon: String::from(""), icon: String::from(""), enabled: false, delay: 120.0, diff --git a/src/utils/battery.rs b/src/utils/battery.rs index 432e22a..f1361ea 100644 --- a/src/utils/battery.rs +++ b/src/utils/battery.rs @@ -1,6 +1,7 @@ use crate::config::CONFIG; use crate::types::ThreadsData; use battery::Manager; +use battery::State; // TODO: better error handeling @@ -18,15 +19,23 @@ pub async fn get_battery() -> ThreadsData { return ThreadsData::Battery(String::from("Cannot Get Battery!")); }; - let percentage = if let Some(battery) = batteries.next() { - f32::from(battery.unwrap().state_of_charge()) * 100.0 + let battery = batteries.next(); + let (icon, percentage); + + if let Some(bat) = battery { + let b = bat.unwrap(); + percentage = f32::from(b.state_of_charge()) * 100.0; + icon = match b.state() { + State::Charging => &CONFIG.battery.charging_icon, + _ => &CONFIG.battery.icon, + }; } else { return ThreadsData::Battery(String::from("Cannot Read Battery!")); }; let result = format!( " {} {:.0}% {}", - CONFIG.battery.icon, percentage, CONFIG.seperator + icon, percentage, CONFIG.seperator ); ThreadsData::Battery(result) }