From c8a74d47efb50caf6ad39e16b45b897dc5841eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Sat, 5 Feb 2022 17:04:27 +0100 Subject: [PATCH] show charging icon --- src/config/battery.rs | 2 ++ src/utils/battery.rs | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) 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) }