From d6102dffa2cdcabd74d03c25332a54dc1a02c958 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 17 Jul 2024 16:30:39 +0200 Subject: [PATCH] refactor: move clock parts to submodules --- src/clock.rs | 81 +++----------------------------------------- src/clock/timebar.rs | 27 +++++++++++++++ src/clock/uidata.rs | 51 ++++++++++++++++++++++++++++ src/main.rs | 2 +- 4 files changed, 83 insertions(+), 78 deletions(-) create mode 100644 src/clock/timebar.rs create mode 100644 src/clock/uidata.rs diff --git a/src/clock.rs b/src/clock.rs index 97b948c..5d90bd0 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -16,33 +16,10 @@ use ratatui::Terminal; use std::io::{Cursor, Stdout, Write}; use std::time::{Duration, Instant}; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum TimeBarLength { - Minute, - Hour, - Custom(i128), - /// implementing a bar that would grow smaller would be weird, so it's a count up instead of - /// a countdown - Countup(i128), - Day, -} - -impl TimeBarLength { - pub(crate) const fn as_secs(self) -> i128 { - match self { - Self::Minute => 60, - Self::Day => 24 * 60 * 60, - Self::Hour => 60 * 60, - Self::Custom(secs) | Self::Countup(secs) => secs, - } - } -} - -impl Default for TimeBarLength { - fn default() -> Self { - Self::Minute - } -} +pub mod timebar; +pub mod uidata; +use timebar::TimeBarLength; +use uidata::UiData; /// Make your terminal into a big clock #[derive(Parser, Debug, Clone)] @@ -89,56 +66,6 @@ pub struct Clock { pub(crate) did_notify: bool, } -#[derive(Debug, Clone, PartialEq, Default)] -pub struct UiData { - fdate: [String; 2], - ftime: [String; 2], - timebar_ratio: [Option; 2], - - data_idx: usize, -} - -impl UiData { - pub fn update(&mut self, fdate: String, ftime: String, timebar_ratio: Option) { - self.data_idx ^= 1; - self.fdate[self.data_idx] = fdate; - self.ftime[self.data_idx] = ftime; - self.timebar_ratio[self.data_idx] = timebar_ratio; - #[cfg(debug_assertions)] - if self.changed() { - trace!("update with change: {:#?}", self); - } - } - - /// did the data change with the last update? - #[must_use] - #[inline] - pub fn changed(&self) -> bool { - // the timebar ratio is discarded, so that we only render the ui when the time - // (second) changes - self.fdate[0] != self.fdate[1] || self.ftime[0] != self.ftime[1] - } - - #[must_use] - #[inline] - pub fn fdate(&self) -> &str { - &self.fdate[self.data_idx] - } - - #[must_use] - #[inline] - pub fn ftime(&self) -> &str { - &self.ftime[self.data_idx] - } - - #[must_use] - #[inline] - #[allow(clippy::missing_const_for_fn)] // no it's not const - pub fn timebar_ratio(&self) -> Option { - self.timebar_ratio[self.data_idx] - } -} - impl Clock { #[must_use] #[allow(clippy::missing_const_for_fn)] diff --git a/src/clock/timebar.rs b/src/clock/timebar.rs new file mode 100644 index 0000000..5886308 --- /dev/null +++ b/src/clock/timebar.rs @@ -0,0 +1,27 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TimeBarLength { + Minute, + Hour, + Custom(i128), + /// implementing a bar that would grow smaller would be weird, so it's a count up instead of + /// a countdown + Countup(i128), + Day, +} + +impl TimeBarLength { + pub(crate) const fn as_secs(self) -> i128 { + match self { + Self::Minute => 60, + Self::Day => 24 * 60 * 60, + Self::Hour => 60 * 60, + Self::Custom(secs) | Self::Countup(secs) => secs, + } + } +} + +impl Default for TimeBarLength { + fn default() -> Self { + Self::Minute + } +} diff --git a/src/clock/uidata.rs b/src/clock/uidata.rs new file mode 100644 index 0000000..c801c6e --- /dev/null +++ b/src/clock/uidata.rs @@ -0,0 +1,51 @@ +use libpt::log::trace; + +#[derive(Debug, Clone, PartialEq, Default)] +pub struct UiData { + fdate: [String; 2], + ftime: [String; 2], + timebar_ratio: [Option; 2], + + data_idx: usize, +} + +impl UiData { + pub fn update(&mut self, fdate: String, ftime: String, timebar_ratio: Option) { + self.data_idx ^= 1; + self.fdate[self.data_idx] = fdate; + self.ftime[self.data_idx] = ftime; + self.timebar_ratio[self.data_idx] = timebar_ratio; + #[cfg(debug_assertions)] + if self.changed() { + trace!("update with change: {:#?}", self); + } + } + + /// did the data change with the last update? + #[must_use] + #[inline] + pub fn changed(&self) -> bool { + // the timebar ratio is discarded, so that we only render the ui when the time + // (second) changes + self.fdate[0] != self.fdate[1] || self.ftime[0] != self.ftime[1] + } + + #[must_use] + #[inline] + pub fn fdate(&self) -> &str { + &self.fdate[self.data_idx] + } + + #[must_use] + #[inline] + pub fn ftime(&self) -> &str { + &self.ftime[self.data_idx] + } + + #[must_use] + #[inline] + #[allow(clippy::missing_const_for_fn)] // no it's not const + pub fn timebar_ratio(&self) -> Option { + self.timebar_ratio[self.data_idx] + } +} diff --git a/src/main.rs b/src/main.rs index 065a46b..c933f04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,7 @@ fn mock_tests() { use chrono::{Local, Timelike}; use libpt::log::info; - use self::clock::UiData; + use crate::clock::uidata::UiData; info!("doing the mock tests"); { let mut c = Clock::parse_from(["some exec", "-mvvv"]);