mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-23 21:55:11 +02:00
feature: add current battery charging state, update field names (#1106)
This commit is contained in:
parent
3e1aa9c75a
commit
1c95411494
@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- [#1022](https://github.com/ClementTsang/bottom/pull/1022): Support three-character hex colour strings for styling.
|
- [#1022](https://github.com/ClementTsang/bottom/pull/1022): Support three-character hex colour strings for styling.
|
||||||
- [#1024](https://github.com/ClementTsang/bottom/pull/1024): Support FreeBSD temperature sensors based on `hw.temperature`.
|
- [#1024](https://github.com/ClementTsang/bottom/pull/1024): Support FreeBSD temperature sensors based on `hw.temperature`.
|
||||||
- [#1063](https://github.com/ClementTsang/bottom/pull/1063): Add buffer and cache memory tracking.
|
- [#1063](https://github.com/ClementTsang/bottom/pull/1063): Add buffer and cache memory tracking.
|
||||||
|
- [#1106](https://github.com/ClementTsang/bottom/pull/1106): Add current battery charging state.
|
||||||
|
|
||||||
## Changes
|
## Changes
|
||||||
|
|
||||||
@ -32,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- [#1064](https://github.com/ClementTsang/bottom/pull/1064): Migrate away from heim for storage information.
|
- [#1064](https://github.com/ClementTsang/bottom/pull/1064): Migrate away from heim for storage information.
|
||||||
- [#812](https://github.com/ClementTsang/bottom/issues/812): Fully remove heim from bottom.
|
- [#812](https://github.com/ClementTsang/bottom/issues/812): Fully remove heim from bottom.
|
||||||
- [#1075](https://github.com/ClementTsang/bottom/issues/1075): Update how drives are named in Windows.
|
- [#1075](https://github.com/ClementTsang/bottom/issues/1075): Update how drives are named in Windows.
|
||||||
|
- [#1106](https://github.com/ClementTsang/bottom/pull/1106): Rename battery consumption field to rate.
|
||||||
|
|
||||||
## Other
|
## Other
|
||||||
|
|
||||||
|
@ -17,7 +17,8 @@ The battery widget can be enabled through either the `--battery` flag, the `batt
|
|||||||
The following data is displayed for batteries:
|
The following data is displayed for batteries:
|
||||||
|
|
||||||
- Charge percent
|
- Charge percent
|
||||||
- Consumption
|
- Consumption rate
|
||||||
|
- Charging state
|
||||||
- Time to empty/charge, based on the current state
|
- Time to empty/charge, based on the current state
|
||||||
- Battery health percent
|
- Battery health percent
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
use starship_battery::{
|
use starship_battery::{
|
||||||
units::{power::watt, ratio::percent, time::second},
|
units::{power::watt, ratio::percent, time::second},
|
||||||
Battery, Manager,
|
Battery, Manager, State,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -21,6 +21,7 @@ pub struct BatteryHarvest {
|
|||||||
pub secs_until_empty: Option<i64>,
|
pub secs_until_empty: Option<i64>,
|
||||||
pub power_consumption_rate_watts: f64,
|
pub power_consumption_rate_watts: f64,
|
||||||
pub health_percent: f64,
|
pub health_percent: f64,
|
||||||
|
pub state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh_batteries(manager: &Manager, batteries: &mut [Battery]) -> Vec<BatteryHarvest> {
|
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>()),
|
charge_percent: f64::from(battery.state_of_charge().get::<percent>()),
|
||||||
power_consumption_rate_watts: f64::from(battery.energy_rate().get::<watt>()),
|
power_consumption_rate_watts: f64::from(battery.energy_rate().get::<watt>()),
|
||||||
health_percent: f64::from(battery.state_of_health().get::<percent>()),
|
health_percent: f64::from(battery.state_of_health().get::<percent>()),
|
||||||
|
state: battery.state(),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -122,24 +122,42 @@ impl Painter {
|
|||||||
|
|
||||||
fn long_time(secs: i64) -> String {
|
fn long_time(secs: i64) -> String {
|
||||||
let time = time::Duration::seconds(secs);
|
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;
|
let num_seconds = time.whole_seconds() - time.whole_minutes() * 60;
|
||||||
|
|
||||||
|
if num_hours > 0 {
|
||||||
format!(
|
format!(
|
||||||
"{} hour{}, {} minute{}, {} second{}",
|
"{} hour{}, {} minute{}, {} second{}",
|
||||||
time.whole_hours(),
|
num_hours,
|
||||||
if time.whole_hours() == 1 { "" } else { "s" },
|
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,
|
num_minutes,
|
||||||
if num_minutes == 1 { "" } else { "s" },
|
if num_minutes == 1 { "" } else { "s" },
|
||||||
num_seconds,
|
num_seconds,
|
||||||
if num_seconds == 1 { "" } else { "s" },
|
if num_seconds == 1 { "" } else { "s" },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn short_time(secs: i64) -> String {
|
fn short_time(secs: i64) -> String {
|
||||||
let time = time::Duration::seconds(secs);
|
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;
|
let num_seconds = time.whole_seconds() - time.whole_minutes() * 60;
|
||||||
|
|
||||||
|
if num_hours > 0 {
|
||||||
format!("{}h {}m {}s", time.whole_hours(), num_minutes, num_seconds,)
|
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);
|
let mut battery_rows = Vec::with_capacity(4);
|
||||||
@ -154,17 +172,22 @@ impl Painter {
|
|||||||
}),
|
}),
|
||||||
]));
|
]));
|
||||||
battery_rows.push(
|
battery_rows.push(
|
||||||
Row::new(vec!["Consumption", &battery_details.watt_consumption])
|
Row::new(vec!["Rate", &battery_details.watt_consumption])
|
||||||
.style(self.colours.text_style),
|
.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;
|
let style = self.colours.text_style;
|
||||||
match &battery_details.battery_duration {
|
match &battery_details.battery_duration {
|
||||||
BatteryDuration::ToEmpty(secs) => {
|
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));
|
battery_rows.push(Row::new(vec!["Time to empty", &s]).style(style));
|
||||||
} else {
|
} else {
|
||||||
s = short_time(*secs);
|
s = short_time(*secs);
|
||||||
@ -172,15 +195,18 @@ impl Painter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
BatteryDuration::ToFull(secs) => {
|
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));
|
battery_rows.push(Row::new(vec!["Time to full", &s]).style(style));
|
||||||
} else {
|
} else {
|
||||||
s = short_time(*secs);
|
s = short_time(*secs);
|
||||||
battery_rows.push(Row::new(vec!["To full", &s]).style(style));
|
battery_rows.push(Row::new(vec!["To full", &s]).style(style));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BatteryDuration::Unknown => {}
|
BatteryDuration::Empty
|
||||||
|
| BatteryDuration::Full
|
||||||
|
| BatteryDuration::Unknown => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@ use crate::widgets::{DiskWidgetData, TempWidgetData};
|
|||||||
pub enum BatteryDuration {
|
pub enum BatteryDuration {
|
||||||
ToEmpty(i64),
|
ToEmpty(i64),
|
||||||
ToFull(i64),
|
ToFull(i64),
|
||||||
|
Empty,
|
||||||
|
Full,
|
||||||
#[default]
|
#[default]
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
@ -29,6 +31,7 @@ pub struct ConvertedBatteryData {
|
|||||||
pub watt_consumption: String,
|
pub watt_consumption: String,
|
||||||
pub battery_duration: BatteryDuration,
|
pub battery_duration: BatteryDuration,
|
||||||
pub health: String,
|
pub health: String,
|
||||||
|
pub state: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[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 {
|
} else if let Some(secs) = battery_harvest.secs_until_full {
|
||||||
BatteryDuration::ToFull(secs)
|
BatteryDuration::ToFull(secs)
|
||||||
} else {
|
} 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),
|
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()
|
.collect()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user