Skip to content

Commit 04cbd5d

Browse files
committed
Add duration format to battery block
1 parent 0d867e9 commit 04cbd5d

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

src/blocks/battery.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
//! -------------|-------------------------------------------------------------------------|-------------------|-----
2929
//! `icon` | Icon based on battery's state | Icon | -
3030
//! `percentage` | Battery level, in percent | Number | Percents
31-
//! `time` | Time remaining until (dis)charge is complete. Presented only if battery's status is (dis)charging. | String | -
31+
//! `time` | Time remaining until (dis)charge is complete. Presented only if battery's status is (dis)charging. | Duration or String *DEPRECATED* | -
3232
//! `power` | Power consumption by the battery or from the power supply when charging | String or Float | Watts
3333
//!
34+
//! formatting `time` with the `str` formatter has been deprecated in favor of the `dur` formatter.
35+
//!
3436
//! # Examples
3537
//!
3638
//! Basic usage:
@@ -44,7 +46,7 @@
4446
//! ```toml
4547
//! [[block]]
4648
//! block = "battery"
47-
//! format = " $percentage {$time |}"
49+
//! format = " $percentage {$time.dur(hms:true, min_unit:m) |}"
4850
//! device = "DisplayDevice"
4951
//! driver = "upower"
5052
//! ```
@@ -164,11 +166,14 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
164166
info.time_remaining.map(|t| {
165167
values.insert(
166168
"time".into(),
167-
Value::text(format!(
168-
"{}:{:02}",
169-
(t / 3600.) as i32,
170-
(t % 3600. / 60.) as i32
171-
)),
169+
Value::duration_with_str(
170+
Duration::from_secs(t as u64),
171+
Some(format!(
172+
"{}:{:02}",
173+
(t / 3600.) as i32,
174+
(t % 3600. / 60.) as i32
175+
)),
176+
),
172177
)
173178
});
174179

src/formatting/formatter/duration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl DurationFormatter {
196196
impl Formatter for DurationFormatter {
197197
fn format(&self, val: &Value, _config: &SharedConfig) -> Result<String, FormatError> {
198198
match val {
199-
Value::Duration(duration) => {
199+
Value::Duration(duration, _) => {
200200
let mut v = self.get_time_parts(duration.as_millis());
201201

202202
if self.round_up {
@@ -277,7 +277,7 @@ mod tests {
277277
.expect("unit must be one of \"y\", \"w\", \"d\", \"h\", \"m\", \"s\", or \"ms\"")]
278278
as u64);
279279
)*
280-
Value::Duration(std::time::Duration::from_millis(ms))
280+
Value::Duration(std::time::Duration::from_millis(ms), None)
281281
}};
282282
}
283283

src/formatting/formatter/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl StrFormatter {
8383
impl Formatter for StrFormatter {
8484
fn format(&self, val: &Value, config: &SharedConfig) -> Result<String, FormatError> {
8585
match val {
86-
Value::Text(text) => {
86+
Value::Text(text) | Value::Duration(_, Some(text)) => {
8787
let text: Vec<&str> = text.graphemes(true).collect();
8888
let width = text.len();
8989
Ok(match (self.rot_interval_ms, self.init_time) {

src/formatting/value.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum ValueInner {
1919
Icon(Cow<'static, str>, Option<f64>),
2020
Number { val: f64, unit: Unit },
2121
Datetime(DateTime<Utc>, Option<Tz>),
22-
Duration(Duration),
22+
Duration(Duration, Option<String>),
2323
Flag,
2424
}
2525

@@ -71,7 +71,11 @@ impl Value {
7171
}
7272

7373
pub fn duration(duration: Duration) -> Self {
74-
Self::new(ValueInner::Duration(duration))
74+
Self::new(ValueInner::Duration(duration, None))
75+
}
76+
77+
pub fn duration_with_str(duration: Duration, duration_string: Option<String>) -> Self {
78+
Self::new(ValueInner::Duration(duration, duration_string))
7579
}
7680

7781
pub fn icon<S>(name: S) -> Self

0 commit comments

Comments
 (0)