Skip to content

Commit

Permalink
bug: reduce battery text cutoff in small windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Jan 4, 2023
1 parent 7c53f08 commit fea6876
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 44 deletions.
70 changes: 58 additions & 12 deletions src/canvas/widgets/battery_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
app::App,
canvas::{drawing_utils::calculate_basic_use_bars, Painter},
constants::*,
data_conversion::BatteryDuration,
};

impl Painter {
Expand Down Expand Up @@ -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!(
Expand All @@ -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),
Expand All @@ -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),
];
Expand Down
51 changes: 19 additions & 32 deletions src/data_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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),
})
Expand Down

0 comments on commit fea6876

Please sign in to comment.