mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-03 11:54:36 +02:00
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.
This commit is contained in:
parent
21f1503277
commit
0093a45be9
@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## Bug Fixes
|
## Bug Fixes
|
||||||
|
|
||||||
|
- [#952](https://github.com/ClementTsang/bottom/pull/952): Partially fix battery text getting cut off in small windows.
|
||||||
|
|
||||||
## Changes
|
## Changes
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
@ -11,6 +11,7 @@ use crate::{
|
|||||||
app::App,
|
app::App,
|
||||||
canvas::{drawing_utils::calculate_basic_use_bars, Painter},
|
canvas::{drawing_utils::calculate_basic_use_bars, Painter},
|
||||||
constants::*,
|
constants::*,
|
||||||
|
data_conversion::BatteryDuration,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Painter {
|
impl Painter {
|
||||||
@ -108,8 +109,8 @@ impl Painter {
|
|||||||
.get(battery_widget_state.currently_selected_battery_index)
|
.get(battery_widget_state.currently_selected_battery_index)
|
||||||
{
|
{
|
||||||
// Assuming a 50/50 split in width
|
// Assuming a 50/50 split in width
|
||||||
let bar_length =
|
let half_width = draw_loc.width.saturating_sub(2) / 2;
|
||||||
usize::from((draw_loc.width.saturating_sub(2) / 2).saturating_sub(8));
|
let bar_length = usize::from(half_width.saturating_sub(8));
|
||||||
let charge_percentage = battery_details.charge_percentage;
|
let charge_percentage = battery_details.charge_percentage;
|
||||||
let num_bars = calculate_basic_use_bars(charge_percentage, bar_length);
|
let num_bars = calculate_basic_use_bars(charge_percentage, bar_length);
|
||||||
let bars = format!(
|
let bars = format!(
|
||||||
@ -119,6 +120,60 @@ impl Painter {
|
|||||||
charge_percentage,
|
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![
|
let battery_rows = vec![
|
||||||
Row::new(vec![
|
Row::new(vec![
|
||||||
Cell::from("Charge %").style(self.colours.text_style),
|
Cell::from("Charge %").style(self.colours.text_style),
|
||||||
@ -132,16 +187,7 @@ impl Painter {
|
|||||||
]),
|
]),
|
||||||
Row::new(vec!["Consumption", &battery_details.watt_consumption])
|
Row::new(vec!["Consumption", &battery_details.watt_consumption])
|
||||||
.style(self.colours.text_style),
|
.style(self.colours.text_style),
|
||||||
if let Some(duration_until_full) = &battery_details.duration_until_full {
|
time_text,
|
||||||
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)
|
|
||||||
},
|
|
||||||
Row::new(vec!["Health %", &battery_details.health])
|
Row::new(vec!["Health %", &battery_details.health])
|
||||||
.style(self.colours.text_style),
|
.style(self.colours.text_style),
|
||||||
];
|
];
|
||||||
|
@ -13,13 +13,25 @@ use crate::units::data_units::DataUnit;
|
|||||||
use crate::utils::gen_util::*;
|
use crate::utils::gen_util::*;
|
||||||
use crate::widgets::{DiskWidgetData, TempWidgetData};
|
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)]
|
#[derive(Default, Debug)]
|
||||||
pub struct ConvertedBatteryData {
|
pub struct ConvertedBatteryData {
|
||||||
pub battery_name: String,
|
pub battery_name: String,
|
||||||
pub charge_percentage: f64,
|
pub charge_percentage: f64,
|
||||||
pub watt_consumption: String,
|
pub watt_consumption: String,
|
||||||
pub duration_until_full: Option<String>,
|
pub battery_duration: BatteryDuration,
|
||||||
pub duration_until_empty: Option<String>,
|
|
||||||
pub health: String,
|
pub health: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -527,37 +539,12 @@ pub fn convert_battery_harvest(current_data: &DataCollection) -> Vec<ConvertedBa
|
|||||||
battery_name: format!("Battery {}", itx),
|
battery_name: format!("Battery {}", itx),
|
||||||
charge_percentage: battery_harvest.charge_percent,
|
charge_percentage: battery_harvest.charge_percent,
|
||||||
watt_consumption: format!("{:.2}W", battery_harvest.power_consumption_rate_watts),
|
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 {
|
battery_duration: if let Some(secs) = battery_harvest.secs_until_empty {
|
||||||
let time = time::Duration::seconds(secs_till_empty);
|
BatteryDuration::ToEmpty(secs)
|
||||||
let num_minutes = time.whole_minutes() - time.whole_hours() * 60;
|
} else if let Some(secs) = battery_harvest.secs_until_full {
|
||||||
let num_seconds = time.whole_seconds() - time.whole_minutes() * 60;
|
BatteryDuration::ToFull(secs)
|
||||||
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 {
|
} else {
|
||||||
None
|
BatteryDuration::Unknown
|
||||||
},
|
|
||||||
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" },
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
},
|
||||||
health: format!("{:.2}%", battery_harvest.health_percent),
|
health: format!("{:.2}%", battery_harvest.health_percent),
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user