Skip to content

Commit

Permalink
Add duration format to tea_timer block
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed Mar 31, 2024
1 parent af70fe2 commit 8ccd198
Showing 1 changed file with 49 additions and 33 deletions.
82 changes: 49 additions & 33 deletions src/blocks/tea_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
//!
//! Key | Values | Default
//! ----|--------|--------
//! `format` | A string to customise the output of this block. See below for available placeholders. | <code>\" $icon {$minutes:$seconds \|}\"</code>
//! `format` | A string to customise the output of this block. See below for available placeholders. | <code>\" $icon {$time.duration(hms:true) \|}\"</code>
//! `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
//! ------------|---------------
Expand All @@ -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<i64>,
pub increment: Option<u64>,
pub done_cmd: Option<String>,
}

Expand All @@ -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 {
Expand All @@ -78,32 +85,41 @@ 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 {
widget.state = State::Info;
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}")));
} else {
widget.state = State::Idle;
}

widget.set_values(values);

api.set_widget(widget)?;

select! {
_ = 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,
Expand Down

0 comments on commit 8ccd198

Please sign in to comment.