From 3032cc4bec71fa27ca251b9a1e30d4aa41ef5c3c Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Wed, 3 Aug 2022 18:30:37 +1000 Subject: [PATCH] Add flag to hide fractional seconds on timer --- src/app.rs | 19 ++++++++++++++++--- src/app/modes.rs | 17 +++++++++++++++-- src/app/modes/stopwatch.rs | 4 ++-- src/app/modes/timer.rs | 8 +++++--- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/app.rs b/src/app.rs index 859e06b..c94ad23 100644 --- a/src/app.rs +++ b/src/app.rs @@ -8,6 +8,7 @@ use tui::style::Style; use tui::Frame; use self::modes::Clock; +use self::modes::DurationFormat; use self::modes::Stopwatch; use self::modes::Timer; @@ -28,6 +29,10 @@ pub(crate) enum Mode { Timer { #[clap(short, long, value_parser = parse_duration, default_value = "5m")] duration: Duration, + + /// Hide milliseconds + #[clap(long = "no-millis", takes_value = false)] + no_millis: bool, }, /// The stopwatch mode displays the elapsed time since it was started. Stopwatch, @@ -38,7 +43,7 @@ pub(crate) enum Mode { pub(crate) struct App { #[clap(subcommand)] pub mode: Option, - /// Forground color of the clock, possible values are: + /// Foreground color of the clock, possible values are: /// a) Any one of: Black, Red, Green, Yellow, Blue, Magenta, Cyan, Gray, DarkGray, LightRed, LightGreen, LightYellow, LightBlue, LightMagenta, LightCyan, White. /// b) Hexadecimal color code: #RRGGBB. #[clap(short, long, value_parser = parse_color, default_value = "green")] @@ -71,8 +76,16 @@ impl App { show_millis: millis.to_owned(), }); } - Mode::Timer { duration } => { - self.timer = Some(Timer::new(duration.to_owned(), self.size, style)); + Mode::Timer { + duration, + no_millis, + } => { + let format = if *no_millis { + DurationFormat::HourMinSec + } else { + DurationFormat::HourMinSecDeci + }; + self.timer = Some(Timer::new(duration.to_owned(), self.size, style, format)); } Mode::Stopwatch => { self.stopwatch = Some(Stopwatch::new(self.size, style)); diff --git a/src/app/modes.rs b/src/app/modes.rs index 3efb308..843ad9f 100644 --- a/src/app/modes.rs +++ b/src/app/modes.rs @@ -17,7 +17,15 @@ use tui::{ widgets::{Paragraph, Widget}, }; -fn format_duration(duration: Duration) -> String { +#[derive(Copy, Clone)] +pub(crate) enum DurationFormat { + /// Hours, minutes, seconds, deciseconds + HourMinSecDeci, + /// Hours, minutes, seconds + HourMinSec, +} + +fn format_duration(duration: Duration, format: DurationFormat) -> String { let millis = duration.num_milliseconds(); let seconds = millis / 1000; let minutes = seconds / 60; @@ -31,7 +39,12 @@ fn format_duration(duration: Duration) -> String { result.push_str(&format!("{}:", hours % 24)); } result.push_str(&format!("{}:", minutes % 60)); - result.push_str(&format!("{:02}.{}", seconds % 60, (millis % 1000) / 100)); + match format { + DurationFormat::HourMinSecDeci => { + result.push_str(&format!("{:02}.{}", seconds % 60, (millis % 1000) / 100)) + } + DurationFormat::HourMinSec => result.push_str(&format!("{:02}", seconds % 60)), + } result } diff --git a/src/app/modes/stopwatch.rs b/src/app/modes/stopwatch.rs index c5c2bbf..0e38988 100644 --- a/src/app/modes/stopwatch.rs +++ b/src/app/modes/stopwatch.rs @@ -2,7 +2,7 @@ use chrono::{DateTime, Duration, Local}; use clock_tui::bricks_text::BricksText; use tui::{buffer::Buffer, layout::Rect, style::Style, widgets::Widget}; -use super::{format_duration, render_centered}; +use super::{format_duration, render_centered, DurationFormat}; pub struct Stopwatch { pub size: u16, @@ -51,7 +51,7 @@ impl Stopwatch { impl Widget for &Stopwatch { fn render(self, area: Rect, buf: &mut Buffer) { - let time_str = format_duration(self.total_time()); + let time_str = format_duration(self.total_time(), DurationFormat::HourMinSecDeci); let text = BricksText::new(time_str.as_str(), self.size, self.size, self.style); let footer = if self.is_paused() { Some("PAUSED (press to resume)".to_string()) diff --git a/src/app/modes/timer.rs b/src/app/modes/timer.rs index c2a41ad..ee0000b 100644 --- a/src/app/modes/timer.rs +++ b/src/app/modes/timer.rs @@ -2,21 +2,23 @@ use chrono::{DateTime, Duration, Local}; use clock_tui::bricks_text::BricksText; use tui::{buffer::Buffer, layout::Rect, style::Style, widgets::Widget}; -use super::{format_duration, render_centered}; +use super::{format_duration, render_centered, DurationFormat}; pub struct Timer { pub size: u16, pub style: Style, + format: DurationFormat, duration: Duration, ended_at: Option>, } impl Timer { - pub(crate) fn new(duration: Duration, size: u16, style: Style) -> Self { + pub(crate) fn new(duration: Duration, size: u16, style: Style, format: DurationFormat) -> Self { Self { duration, size, style, + format, ended_at: Some(Local::now() + duration), } } @@ -58,7 +60,7 @@ impl Timer { impl Widget for &Timer { fn render(self, area: Rect, buf: &mut Buffer) { - let time_str = format_duration(self.remaining_time()); + let time_str = format_duration(self.remaining_time(), self.format); let text = BricksText::new(time_str.as_str(), self.size, self.size, self.style); let footer = if self.is_paused() { Some("PAUSED (press to resume)".to_string())