summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2023-01-04 01:27:15 -0500
committerGitHub <noreply@github.com>2023-01-04 01:27:15 -0500
commit0093a45be9f225b969bf7771d8e64b543662f64c (patch)
tree7f1d57feca936fea4207cbdfa4ebc96aec1312fb
parent21f1503277d8fe3c2e0700436bea0dcf510bbd7c (diff)
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.
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/canvas/widgets/battery_display.rs70
-rw-r--r--src/data_conversion.rs51
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<String>,
- pub duration_until_empty: Option<String>,
+ pub battery_duration: BatteryDuration,
pub health: String,
}
@@ -527,37 +539,12 @@ pub fn convert_battery_harvest(current_data: &DataCollection) -> Vec<ConvertedBa
battery_name: format!("Battery {}", itx),
charge_percentage: battery_harvest.charge_percent,
watt_consumption: format!("{:.2}W", battery_harvest.power_consumption_rate_watts),
- duration_until_empty: if let Some(secs_till_empty) = battery_harvest.secs_until_empty {
- let time = time::Duration::seconds(secs_till_empty);
- let num_minutes = time.whole_minutes() - time.whole_hours() * 60;
- let num_seconds = time.whole_seconds() - time.whole_minutes() * 60;
- Some(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" },
- ))
- } else {
- None
- },
- duration_until_full: if let Some(secs_till_full) = battery_harvest.secs_until_full {
- let time = time::Duration::seconds(secs_till_full);
- let num_minutes = time.whole_minutes() - time.whole_hours() * 60;
- let num_seconds = time.whole_seconds() - time.whole_minutes() * 60;
- Some(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" },
- ))
+ battery_duration: if let Some(secs) = battery_harvest.secs_until_empty {
+ BatteryDuration::ToEmpty(secs)
+ } else if let Some(secs) = battery_harvest.secs_until_full {
+ BatteryDuration::ToFull(secs)
} else {
- None
+ BatteryDuration::Unknown
},
health: format!("{:.2}%", battery_harvest.health_percent),
})