From 0093a45be9f225b969bf7771d8e64b543662f64c Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Wed, 4 Jan 2023 01:27:15 -0500 Subject: [PATCH] bug: reduce battery text cutoff in small windows (#952) Fixes an issue where small windows would cause the battery to-X time to be cut off. --- CHANGELOG.md | 2 + src/canvas/widgets/battery_display.rs | 70 ++++++++++++++++++++++----- src/data_conversion.rs | 51 ++++++++----------- 3 files changed, 79 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7858102..c7e1ba9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Bug Fixes +- [#952](https://github.com/ClementTsang/bottom/pull/952): Partially fix battery text getting cut off in small windows. + ## Changes ## Features diff --git a/src/canvas/widgets/battery_display.rs b/src/canvas/widgets/battery_display.rs index 384d8cba..d69fe7bf 100644 --- a/src/canvas/widgets/battery_display.rs +++ b/src/canvas/widgets/battery_display.rs @@ -11,6 +11,7 @@ use crate::{ app::App, canvas::{drawing_utils::calculate_basic_use_bars, Painter}, constants::*, + data_conversion::BatteryDuration, }; impl Painter { @@ -108,8 +109,8 @@ impl Painter { .get(battery_widget_state.currently_selected_battery_index) { // Assuming a 50/50 split in width - let bar_length = - usize::from((draw_loc.width.saturating_sub(2) / 2).saturating_sub(8)); + let half_width = draw_loc.width.saturating_sub(2) / 2; + let bar_length = usize::from(half_width.saturating_sub(8)); let charge_percentage = battery_details.charge_percentage; let num_bars = calculate_basic_use_bars(charge_percentage, bar_length); let bars = format!( @@ -119,6 +120,60 @@ impl Painter { charge_percentage, ); + fn long_time(secs: i64) -> String { + let time = time::Duration::seconds(secs); + let num_minutes = time.whole_minutes() - time.whole_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" }, + ) + } + + fn short_time(secs: i64) -> String { + let time = time::Duration::seconds(secs); + let num_minutes = time.whole_minutes() - time.whole_hours() * 60; + let num_seconds = time.whole_seconds() - time.whole_minutes() * 60; + format!("{}h {}m {}s", time.whole_hours(), num_minutes, num_seconds,) + } + + let s: String; // Brought out due to lifetimes. + let time_text = { + let style = self.colours.text_style; + match &battery_details.battery_duration { + BatteryDuration::ToEmpty(secs) => { + if half_width > 25 { + s = long_time(*secs); + Row::new(vec!["Time to empty", &s]).style(style) + } else { + s = short_time(*secs); + Row::new(vec!["To empty", &s]).style(style) + } + } + BatteryDuration::ToFull(secs) => { + if half_width > 25 { + s = long_time(*secs); + Row::new(vec!["Time to full", &s]).style(style) + } else { + s = short_time(*secs); + Row::new(vec!["To full", &s]).style(style) + } + } + BatteryDuration::Unknown => { + if half_width > 15 { + Row::new(vec!["Time to full/empty", "N/A"]).style(style) + } else { + Row::new(vec!["To empty/full", "N/A"]).style(style) + } + } + } + }; + let battery_rows = vec![ Row::new(vec![ Cell::from("Charge %").style(self.colours.text_style), @@ -132,16 +187,7 @@ impl Painter { ]), Row::new(vec!["Consumption", &battery_details.watt_consumption]) .style(self.colours.text_style), - if let Some(duration_until_full) = &battery_details.duration_until_full { - Row::new(vec!["Time to full", duration_until_full]) - .style(self.colours.text_style) - } else if let Some(duration_until_empty) = &battery_details.duration_until_empty - { - Row::new(vec!["Time to empty", duration_until_empty]) - .style(self.colours.text_style) - } else { - Row::new(vec!["Time to full/empty", "N/A"]).style(self.colours.text_style) - }, + time_text, Row::new(vec!["Health %", &battery_details.health]) .style(self.colours.text_style), ]; diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 257157ee..98f450d9 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -13,13 +13,25 @@ use crate::units::data_units::DataUnit; use crate::utils::gen_util::*; use crate::widgets::{DiskWidgetData, TempWidgetData}; +#[derive(Debug)] +pub enum BatteryDuration { + ToEmpty(i64), + ToFull(i64), + Unknown, +} + +impl Default for BatteryDuration { + fn default() -> Self { + BatteryDuration::Unknown + } +} + #[derive(Default, Debug)] pub struct ConvertedBatteryData { pub battery_name: String, pub charge_percentage: f64, pub watt_consumption: String, - pub duration_until_full: Option, - pub duration_until_empty: Option, + pub battery_duration: BatteryDuration, pub health: String, } @@ -527,37 +539,12 @@ pub fn convert_battery_harvest(current_data: &DataCollection) -> Vec