refactor: move clock parts to submodules
cargo devel CI / cargo CI (push) Successful in 1m51s Details

This commit is contained in:
Christoph J. Scherr 2024-07-17 16:30:39 +02:00
parent 27d135b1c8
commit d6102dffa2
4 changed files with 83 additions and 78 deletions

View File

@ -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<f64>; 2],
data_idx: usize,
}
impl UiData {
pub fn update(&mut self, fdate: String, ftime: String, timebar_ratio: Option<f64>) {
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<f64> {
self.timebar_ratio[self.data_idx]
}
}
impl Clock {
#[must_use]
#[allow(clippy::missing_const_for_fn)]

27
src/clock/timebar.rs Normal file
View File

@ -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
}
}

51
src/clock/uidata.rs Normal file
View File

@ -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<f64>; 2],
data_idx: usize,
}
impl UiData {
pub fn update(&mut self, fdate: String, ftime: String, timebar_ratio: Option<f64>) {
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<f64> {
self.timebar_ratio[self.data_idx]
}
}

View File

@ -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"]);