diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2023-04-19 00:46:57 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-19 00:46:57 -0400 |
commit | 1c95411494130931b937a68fb39e8ebdd81ea8b5 (patch) | |
tree | b35f6c933dc0fa6191f1c251a7e62e0b9f5432e8 /src | |
parent | 3e1aa9c75ae76222ad92fa485be6640436dc52bc (diff) |
feature: add current battery charging state, update field names (#1106)
Diffstat (limited to 'src')
-rw-r--r-- | src/app/data_harvester/batteries/battery.rs | 4 | ||||
-rw-r--r-- | src/canvas/widgets/battery_display.rs | 64 | ||||
-rw-r--r-- | src/data_conversion.rs | 16 |
3 files changed, 63 insertions, 21 deletions
diff --git a/src/app/data_harvester/batteries/battery.rs b/src/app/data_harvester/batteries/battery.rs index 0b3da61e..ec95ada2 100644 --- a/src/app/data_harvester/batteries/battery.rs +++ b/src/app/data_harvester/batteries/battery.rs @@ -11,7 +11,7 @@ use starship_battery::{ units::{power::watt, ratio::percent, time::second}, - Battery, Manager, + Battery, Manager, State, }; #[derive(Debug, Clone)] @@ -21,6 +21,7 @@ pub struct BatteryHarvest { pub secs_until_empty: Option<i64>, pub power_consumption_rate_watts: f64, pub health_percent: f64, + pub state: State, } pub fn refresh_batteries(manager: &Manager, batteries: &mut [Battery]) -> Vec<BatteryHarvest> { @@ -40,6 +41,7 @@ pub fn refresh_batteries(manager: &Manager, batteries: &mut [Battery]) -> Vec<Ba charge_percent: f64::from(battery.state_of_charge().get::<percent>()), power_consumption_rate_watts: f64::from(battery.energy_rate().get::<watt>()), health_percent: f64::from(battery.state_of_health().get::<percent>()), + state: battery.state(), }) } else { None diff --git a/src/canvas/widgets/battery_display.rs b/src/canvas/widgets/battery_display.rs index 4ba4fb6e..59171016 100644 --- a/src/canvas/widgets/battery_display.rs +++ b/src/canvas/widgets/battery_display.rs @@ -122,24 +122,42 @@ impl Painter { fn long_time(secs: i64) -> String { let time = time::Duration::seconds(secs); - let num_minutes = time.whole_minutes() - time.whole_hours() * 60; + let num_hours = time.whole_hours(); + let num_minutes = time.whole_minutes() - num_hours * 60; let num_seconds = time.whole_seconds() - time.whole_minutes() * 60; - format!( - "{} hour{}, {} minute{}, {} second{}", - time.whole_hours(), - if time.whole_hours() == 1 { "" } else { "s" }, - num_minutes, - if num_minutes == 1 { "" } else { "s" }, - num_seconds, - if num_seconds == 1 { "" } else { "s" }, - ) + + if num_hours > 0 { + format!( + "{} hour{}, {} minute{}, {} second{}", + num_hours, + if num_hours == 1 { "" } else { "s" }, + num_minutes, + if num_minutes == 1 { "" } else { "s" }, + num_seconds, + if num_seconds == 1 { "" } else { "s" }, + ) + } else { + format!( + "{} minute{}, {} second{}", + num_minutes, + if num_minutes == 1 { "" } else { "s" }, + num_seconds, + if num_seconds == 1 { "" } else { "s" }, + ) + } } fn short_time(secs: i64) -> String { let time = time::Duration::seconds(secs); - let num_minutes = time.whole_minutes() - time.whole_hours() * 60; + let num_hours = time.whole_hours(); + let num_minutes = time.whole_minutes() - num_hours * 60; let num_seconds = time.whole_seconds() - time.whole_minutes() * 60; - format!("{}h {}m {}s", time.whole_hours(), num_minutes, num_seconds,) + + if num_hours > 0 { + format!("{}h {}m {}s", time.whole_hours(), num_minutes, num_seconds,) + } else { + format!("{}m {}s", num_minutes, num_seconds,) + } } let mut battery_rows = Vec::with_capacity(4); @@ -154,17 +172,22 @@ impl Painter { }), ])); battery_rows.push( - Row::new(vec!["Consumption", &battery_details.watt_consumption]) + Row::new(vec!["Rate", &battery_details.watt_consumption]) .style(self.colours.text_style), ); - let s: String; // Keep string in scope. + battery_rows.push( + Row::new(vec!["State", &battery_details.state]).style(self.colours.text_style), + ); + + let mut s: String; // Keep string in scope. { let style = self.colours.text_style; match &battery_details.battery_duration { BatteryDuration::ToEmpty(secs) => { - if half_width > 25 { - s = long_time(*secs); + s = long_time(*secs); + + if half_width as usize > s.len() { battery_rows.push(Row::new(vec!["Time to empty", &s]).style(style)); } else { s = short_time(*secs); @@ -172,15 +195,18 @@ impl Painter { } } BatteryDuration::ToFull(secs) => { - if half_width > 25 { - s = long_time(*secs); + s = long_time(*secs); + + if half_width as usize > s.len() { battery_rows.push(Row::new(vec!["Time to full", &s]).style(style)); } else { s = short_time(*secs); battery_rows.push(Row::new(vec!["To full", &s]).style(style)); } } - BatteryDuration::Unknown => {} + BatteryDuration::Empty + | BatteryDuration::Full + | BatteryDuration::Unknown => {} } } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 03044c9c..19fd3809 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -18,6 +18,8 @@ use crate::widgets::{DiskWidgetData, TempWidgetData}; pub enum BatteryDuration { ToEmpty(i64), ToFull(i64), + Empty, + Full, #[default] Unknown, } @@ -29,6 +31,7 @@ pub struct ConvertedBatteryData { pub watt_consumption: String, pub battery_duration: BatteryDuration, pub health: String, + pub state: String, } #[derive(Default, Debug)] @@ -527,9 +530,20 @@ pub fn convert_battery_harvest(current_data: &DataCollection) -> Vec<ConvertedBa } else if let Some(secs) = battery_harvest.secs_until_full { BatteryDuration::ToFull(secs) } else { - BatteryDuration::Unknown + match battery_harvest.state { + starship_battery::State::Empty => BatteryDuration::Empty, + starship_battery::State::Full => BatteryDuration::Full, + _ => BatteryDuration::Unknown, + } }, health: format!("{:.2}%", battery_harvest.health_percent), + state: { + let mut s = battery_harvest.state.to_string(); + if !s.is_empty() { + s[0..1].make_ascii_uppercase(); + } + s + }, }) .collect() } |