Skip to content

Commit

Permalink
Add duration format to battery block
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed May 15, 2024
1 parent 7df8a4b commit eef9c43
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/blocks/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
//! -------------|-------------------------------------------------------------------------|-------------------|-----
//! `icon` | Icon based on battery's state | Icon | -
//! `percentage` | Battery level, in percent | Number | Percents
//! `time` | Time remaining until (dis)charge is complete. Presented only if battery's status is (dis)charging. | String | -
//! `time_remaining` | Time remaining until (dis)charge is complete. Presented only if battery's status is (dis)charging. | Duration | -
//! `time` | Time remaining until (dis)charge is complete. Presented only if battery's status is (dis)charging. | String *DEPRECATED* | -
//! `power` | Power consumption by the battery or from the power supply when charging | String or Float | Watts
//!
//! `time` has been deprecated in favor of `time_remaining`.
//!
//! # Examples
//!
//! Basic usage:
Expand All @@ -44,7 +47,7 @@
//! ```toml
//! [[block]]
//! block = "battery"
//! format = " $percentage {$time |}"
//! format = " $percentage {$time_remaining.dur(hms:true, min_unit:m) |}"
//! device = "DisplayDevice"
//! driver = "upower"
//! ```
Expand Down Expand Up @@ -161,15 +164,19 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

info.power
.map(|p| values.insert("power".into(), Value::watts(p)));
info.time_remaining.map(|t| {
values.insert(
"time".into(),
Value::text(format!(
"{}:{:02}",
(t / 3600.) as i32,
(t % 3600. / 60.) as i32
)),
)
info.time_remaining.inspect(|&t| {
map! { @extend values
"time" => Value::text(
format!(
"{}:{:02}",
(t / 3600.) as i32,
(t % 3600. / 60.) as i32
),
),
"time_remaining" => Value::duration(
Duration::from_secs(t as u64),
),
}
});

let (icon_name, icon_value, state) = match (info.status, info.capacity) {
Expand Down

0 comments on commit eef9c43

Please sign in to comment.