show charging icon

This commit is contained in:
Özgür Kesim 2022-02-05 17:04:27 +01:00
parent 0de57f2e49
commit c8a74d47ef
2 changed files with 14 additions and 3 deletions

View File

@ -4,6 +4,7 @@ use std::default::Default;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Battery { pub struct Battery {
pub icon: String, pub icon: String,
pub charging_icon: String,
pub enabled: bool, pub enabled: bool,
pub delay: f64, pub delay: f64,
} }
@ -11,6 +12,7 @@ pub struct Battery {
impl Default for Battery { impl Default for Battery {
fn default() -> Self { fn default() -> Self {
Battery { Battery {
charging_icon: String::from(""),
icon: String::from(""), icon: String::from(""),
enabled: false, enabled: false,
delay: 120.0, delay: 120.0,

View File

@ -1,6 +1,7 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData; use crate::types::ThreadsData;
use battery::Manager; use battery::Manager;
use battery::State;
// TODO: better error handeling // TODO: better error handeling
@ -18,15 +19,23 @@ pub async fn get_battery() -> ThreadsData {
return ThreadsData::Battery(String::from("Cannot Get Battery!")); return ThreadsData::Battery(String::from("Cannot Get Battery!"));
}; };
let percentage = if let Some(battery) = batteries.next() { let battery = batteries.next();
f32::from(battery.unwrap().state_of_charge()) * 100.0 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 { } else {
return ThreadsData::Battery(String::from("Cannot Read Battery!")); return ThreadsData::Battery(String::from("Cannot Read Battery!"));
}; };
let result = format!( let result = format!(
" {} {:.0}% {}", " {} {:.0}% {}",
CONFIG.battery.icon, percentage, CONFIG.seperator icon, percentage, CONFIG.seperator
); );
ThreadsData::Battery(result) ThreadsData::Battery(result)
} }