diff --git a/src/clock.rs b/src/clock.rs index 3cc2051..4bfef96 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -1,5 +1,6 @@ +use std::fmt; use std::marker::PhantomData; - +use std::time::Duration; use strum::Display; #[derive(Debug, Copy, Clone, Display, PartialEq, Eq)] @@ -33,14 +34,30 @@ impl Clock { self.current_value = self.initial_value; } - pub fn format(&mut self) -> String { - let ms = self.current_value; + fn duration(&self) -> Duration { + Duration::from_millis(self.current_value) + } - let minutes = (ms % 3600000) / 60000; - let seconds = (ms % 60000) / 1000; - let tenths = (ms % 1000) / 100; + fn minutes(&self) -> u64 { + self.duration().as_secs() / 60 + } + fn seconds(&self) -> u64 { + self.duration().as_secs() % 60 + } + fn tenths(&self) -> u32 { + self.duration().subsec_millis() / 100 + } +} - format!("{:02}:{:02}.{}", minutes, seconds, tenths) +impl fmt::Display for Clock { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{:02}:{:02}.{}", + self.minutes(), + self.seconds(), + self.tenths() + ) } } diff --git a/src/widgets/countdown.rs b/src/widgets/countdown.rs index 4459b11..611acdf 100644 --- a/src/widgets/countdown.rs +++ b/src/widgets/countdown.rs @@ -45,7 +45,7 @@ impl StatefulWidget for &CountdownWidget { type State = Countdown; fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { let h = Paragraph::new(state.headline.clone()).centered(); - let c = Paragraph::new(state.clock.format()).centered(); + let c = Paragraph::new(format!("{}", state.clock)).centered(); let [v1, v2] = Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area); h.render(v1, buf); diff --git a/src/widgets/timer.rs b/src/widgets/timer.rs index 114270d..d3f7624 100644 --- a/src/widgets/timer.rs +++ b/src/widgets/timer.rs @@ -45,7 +45,7 @@ impl StatefulWidget for TimerWidget { type State = Timer; fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { let h = Paragraph::new(state.headline.clone()).centered(); - let c = Paragraph::new(state.clock.format()).centered(); + let c = Paragraph::new(format!("{}", state.clock)).centered(); let [v1, v2] = Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area); h.render(v1, buf);