diff --git a/src/blocks/tea_timer.rs b/src/blocks/tea_timer.rs
index 12771bb10c..bf4a187fcf 100644
--- a/src/blocks/tea_timer.rs
+++ b/src/blocks/tea_timer.rs
@@ -4,18 +4,21 @@
//!
//! Key | Values | Default
//! ----|--------|--------
-//! `format` | A string to customise the output of this block. See below for available placeholders. | \" $icon {$minutes:$seconds \|}\"
+//! `format` | A string to customise the output of this block. See below for available placeholders. | \" $icon {$time.duration(hms:true) \|}\"
//! `increment` | The numbers of seconds to add each time the block is clicked. | 30
//! `done_cmd` | A command to run in `sh` when timer finishes. | None
//!
-//! Placeholder | Value | Type | Unit
-//! -----------------|----------------------------------------------------------------|--------|---------------
-//! `icon` | A static icon | Icon | -
-//! `hours` | The hours remaining on the timer | Text | h
-//! `minutes` | The minutes remaining on the timer | Text | mn
-//! `seconds` | The seconds remaining on the timer | Text | s
+//! Placeholder | Value | Type | Unit
+//! -----------------------|----------------------------------------------------------------|----------|---------------
+//! `icon` | A static icon | Icon | -
+//! `time` | The time remaining on the timer | Duration | -
+//! `hours` *DEPRECATED* | The hours remaining on the timer | Text | h
+//! `minutes` *DEPRECATED* | The minutes remaining on the timer | Text | mn
+//! `seconds` *DEPRECATED* | The seconds remaining on the timer | Text | s
//!
-//! `hours`, `minutes`, and `seconds` are unset when the timer is inactive.
+//! `time`, `hours`, `minutes`, and `seconds` are unset when the timer is inactive.
+//!
+//! `hours`, `minutes`, and `seconds` have been deprecated in favor of `time`.
//!
//! Action | Default button
//! ------------|---------------
@@ -37,13 +40,14 @@
use super::prelude::*;
use crate::subprocess::spawn_shell;
-use chrono::{Duration, Utc};
+
+use std::time::{Duration, Instant};
#[derive(Deserialize, Debug, SmartDefault)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
pub format: FormatConfig,
- pub increment: Option,
+ pub increment: Option,
pub done_cmd: Option,
}
@@ -59,17 +63,20 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let interval: Seconds = 1.into();
let mut timer = interval.timer();
- let format = config.format.with_default(" $icon {$minutes:$seconds |}")?;
+ let format = config
+ .format
+ .with_default(" $icon {$time.duration(hms:true) |}")?;
- let increment =
- Duration::try_seconds(config.increment.unwrap_or(30)).error("invalid increment value")?;
- let mut timer_end = Utc::now();
+ let increment = Duration::from_secs(config.increment.unwrap_or(30));
+ let mut timer_end = Instant::now();
let mut timer_was_active = false;
loop {
- let remaining_time = timer_end - Utc::now();
- let is_timer_active = remaining_time > Duration::zero();
+ let mut widget = Widget::new().with_format(format.clone());
+
+ let remaining_time = timer_end - Instant::now();
+ let is_timer_active = !remaining_time.is_zero();
if !is_timer_active && timer_was_active {
if let Some(cmd) = &config.done_cmd {
@@ -78,24 +85,30 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
}
timer_was_active = is_timer_active;
- let (hours, minutes, seconds) = if is_timer_active {
- (
- remaining_time.num_hours(),
- remaining_time.num_minutes() % 60,
- remaining_time.num_seconds() % 60,
- )
- } else {
- (0, 0, 0)
- };
+ let mut values = map!(
+ "icon" => Value::icon("tea"),
+ );
- let mut widget = Widget::new().with_format(format.clone());
+ if is_timer_active {
+ values.insert("time".into(), Value::duration(remaining_time));
+ let mut seconds = remaining_time.as_secs();
- widget.set_values(map!(
- "icon" => Value::icon("tea"),
- [if is_timer_active] "hours" => Value::text(format!("{hours:02}")),
- [if is_timer_active] "minutes" => Value::text(format!("{minutes:02}")),
- [if is_timer_active] "seconds" => Value::text(format!("{seconds:02}")),
- ));
+ if format.contains_key("hours") {
+ let hours = seconds / 3_600;
+ values.insert("hours".into(), Value::text(format!("{hours:02}")));
+ seconds %= 3_600;
+ }
+
+ if format.contains_key("minutes") {
+ let minutes = seconds / 60;
+ values.insert("minutes".into(), Value::text(format!("{minutes:02}")));
+ seconds %= 60;
+ }
+
+ values.insert("seconds".into(), Value::text(format!("{seconds:02}")));
+ }
+
+ widget.set_values(values);
api.set_widget(widget)?;
@@ -103,7 +116,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
_ = timer.tick(), if is_timer_active => (),
_ = api.wait_for_update_request() => (),
Some(action) = actions.recv() => {
- let now = Utc::now();
+ let now = Instant::now();
match action.as_ref() {
"increment" if is_timer_active => timer_end += increment,
"increment" => timer_end = now + increment,