add mode show and fix some things #20

Merged
cscherrNT merged 21 commits from devel into master 2024-07-26 10:43:20 +02:00
4 changed files with 83 additions and 78 deletions
Showing only changes of commit d6102dffa2 - Show all commits

View File

@ -16,33 +16,10 @@ use ratatui::Terminal;
use std::io::{Cursor, Stdout, Write}; use std::io::{Cursor, Stdout, Write};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub mod timebar;
pub enum TimeBarLength { pub mod uidata;
Minute, use timebar::TimeBarLength;
Hour, use uidata::UiData;
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
}
}
/// Make your terminal into a big clock /// Make your terminal into a big clock
#[derive(Parser, Debug, Clone)] #[derive(Parser, Debug, Clone)]
@ -89,56 +66,6 @@ pub struct Clock {
pub(crate) did_notify: bool, 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 { impl Clock {
#[must_use] #[must_use]
#[allow(clippy::missing_const_for_fn)] #[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 chrono::{Local, Timelike};
use libpt::log::info; use libpt::log::info;
use self::clock::UiData; use crate::clock::uidata::UiData;
info!("doing the mock tests"); info!("doing the mock tests");
{ {
let mut c = Clock::parse_from(["some exec", "-mvvv"]); let mut c = Clock::parse_from(["some exec", "-mvvv"]);