Compare commits

..

1 commit

Author SHA1 Message Date
c316e71d46 fix: add one second to the live timebar_ratio now time to align percentages with expectations #10
All checks were successful
cargo devel CI / cargo CI (push) Successful in 1m36s
2024-07-11 16:53:59 +02:00
2 changed files with 64 additions and 37 deletions

View file

@ -1,12 +1,12 @@
#![warn(clippy::pedantic, clippy::style, clippy::nursery)] #![warn(clippy::pedantic, clippy::style, clippy::nursery)]
#![allow(clippy::question_mark_used)] #![allow(clippy::question_mark_used)]
use chrono::{DateTime, Local, SubsecRound, Timelike}; use chrono::{DateTime, Local, SubsecRound, TimeZone, Timelike};
use clap::Parser; use clap::Parser;
use libpt::cli::args::HELP_TEMPLATE; use libpt::cli::args::HELP_TEMPLATE;
use libpt::cli::clap::ArgGroup; use libpt::cli::clap::ArgGroup;
use libpt::cli::{args::VerbosityLevel, clap}; use libpt::cli::{args::VerbosityLevel, clap};
use libpt::log::{debug, trace}; use libpt::log::{debug, info, trace};
use ratatui::backend::CrosstermBackend; use ratatui::backend::CrosstermBackend;
use ratatui::crossterm::event::{self, poll, Event, KeyCode, KeyModifiers}; use ratatui::crossterm::event::{self, poll, Event, KeyCode, KeyModifiers};
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect}; use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
@ -64,7 +64,7 @@ pub struct Clock {
#[clap(short, long)] #[clap(short, long)]
pub custom: Option<i64>, pub custom: Option<i64>,
#[clap(skip)] #[clap(skip)]
last_reset: Option<DateTime<Local>>, pub(crate) last_reset: Option<DateTime<Local>>,
} }
#[derive(Debug, Clone, PartialEq, Default)] #[derive(Debug, Clone, PartialEq, Default)]
@ -94,26 +94,25 @@ impl UiData {
pub fn changed(&self) -> bool { pub fn changed(&self) -> bool {
// the timebar ratio is discarded, so that we only render the ui when the time // the timebar ratio is discarded, so that we only render the ui when the time
// (second) changes // (second) changes
self.fdate[0] != self.fdate[1] || self.ftime[0] != self.ftime[1] self.fdate[0] != self.fdate[1] || self.ftime[0] != self.ftime[1]
} }
#[must_use] #[must_use]
#[inline] #[inline]
pub(crate) fn fdate(&self) -> &str { pub fn fdate(&self) -> &str {
&self.fdate[self.data_idx] &self.fdate[self.data_idx]
} }
#[must_use] #[must_use]
#[inline] #[inline]
pub(crate) fn ftime(&self) -> &str { pub fn ftime(&self) -> &str {
&self.ftime[self.data_idx] &self.ftime[self.data_idx]
} }
#[must_use] #[must_use]
#[inline] #[inline]
#[allow(clippy::missing_const_for_fn)] // no it's not const #[allow(clippy::missing_const_for_fn)] // no it's not const
pub(crate) fn timebar_ratio(&self) -> Option<f64> { pub fn timebar_ratio(&self) -> Option<f64> {
self.timebar_ratio[self.data_idx] self.timebar_ratio[self.data_idx]
} }
} }
@ -136,13 +135,7 @@ impl Clock {
#[allow(clippy::cast_precision_loss)] // okay, good to know, but I accept the loss. It #[allow(clippy::cast_precision_loss)] // okay, good to know, but I accept the loss. It
// shouldn't come to more than 2^52 seconds anyway // shouldn't come to more than 2^52 seconds anyway
pub(crate) fn timebar_ratio(&self, current_time: DateTime<Local>) -> Option<f64> {
// FIXME: This generally works, but we want 0% at the start and 100% at the end, which does not
// fully work. We also want 50% at the half etc. #10
//
// The logs show that it's calculated correctly for the given timestamp, so why does the GUI
// show it delayed? It seems to always show the previous version!
fn timebar_ratio(&self, current_time: DateTime<Local>) -> Option<f64> {
let len = self.timebar_len()?; let len = self.timebar_len()?;
let since = current_time let since = current_time
.signed_duration_since(self.last_reset.unwrap()) .signed_duration_since(self.last_reset.unwrap())
@ -154,7 +147,7 @@ impl Clock {
Some((since / len.as_secs() as f64).clamp(0.0, 1.0)) Some((since / len.as_secs() as f64).clamp(0.0, 1.0))
} }
fn maybe_reset_since_zero(&mut self) { pub(crate) fn maybe_reset_since_zero(&mut self) {
if let Some(len) = self.timebar_len() { if let Some(len) = self.timebar_len() {
let since_last_reset = Local::now().signed_duration_since(self.last_reset.unwrap()); let since_last_reset = Local::now().signed_duration_since(self.last_reset.unwrap());
match len { match len {
@ -221,27 +214,7 @@ impl Clock {
} }
#[allow(clippy::unnecessary_wraps)] // we have that to be future proof #[allow(clippy::unnecessary_wraps)] // we have that to be future proof
fn setup(&mut self) -> anyhow::Result<()> { pub(crate) fn setup(&mut self) -> anyhow::Result<()> {
// mock tests
#[cfg(debug_assertions)]
#[allow(clippy::cast_precision_loss)]
{
let len = TimeBarLength::Minute;
let reset_time = Local::now().with_second(0).unwrap();
let pretend_time = reset_time.with_second(30).unwrap();
assert_eq!(
(pretend_time.signed_duration_since(reset_time).num_seconds() as f64)
/ len.as_secs() as f64,
0.5
);
let pretend_time = reset_time.with_second(0).unwrap();
assert_eq!(
(pretend_time.signed_duration_since(reset_time).num_seconds() as f64)
/ len.as_secs() as f64,
0.0
);
}
self.setup_last_reset(); self.setup_last_reset();
Ok(()) Ok(())
} }
@ -263,10 +236,26 @@ impl Clock {
.map(str::to_string) .map(str::to_string)
.collect(); .collect();
// We somehow fill timebar_ratio with a bad value here if we don't add 1 second. It's
// always the value that would be right for now-1s. The start of the minute is
// special, with this strategy it is 100%. #10
//
// If we manually add a second, it works as expected, but it feels weird. We use the
// same time for all of the datapoints here, so it can't be because of time diff in
// calculation. I noticed that we don't start at 0% this way (with len=minute)
// . Normally, chrono does not include 60 seconds, only letting it range between 0 and
// 59. This makes sense but feels weird to the human understanding, of course there are
// seconds in a minute! If we do it this way, we don't quite start at 0%, but 100%,
// which feels correct.
//
// In short: if we add a second here, we get the correct percentages. 01:00 is 100%,
// 01:30 is 50%, 01:59 is 98%, 01:60 does not exist because that's how counting from
// 0 works.
uidata.update( uidata.update(
splits[0].clone(), splits[0].clone(),
splits[1].clone(), splits[1].clone(),
self.timebar_ratio(raw_time), self.timebar_ratio(raw_time + chrono::Duration::seconds(1)),
); );
if uidata.changed() { if uidata.changed() {
self.ui(terminal, &uidata)?; self.ui(terminal, &uidata)?;

View file

@ -29,6 +29,9 @@ fn main() -> anyhow::Result<()> {
} }
debug!("set up logger"); debug!("set up logger");
#[cfg(debug_assertions)]
mock_tests();
debug!("taking over terminal"); debug!("taking over terminal");
// setup terminal // setup terminal
enable_raw_mode()?; enable_raw_mode()?;
@ -53,3 +56,38 @@ fn main() -> anyhow::Result<()> {
debug!("done"); debug!("done");
result result
} }
#[cfg(debug_assertions)]
#[allow(clippy::cast_precision_loss)]
fn mock_tests() {
use chrono::{Local, Timelike};
use libpt::log::info;
use self::clock::UiData;
info!("doing the mock tests");
{
let mut c = Clock::parse_from(["some exec", "-mvvv"]);
let now = Local::now();
c.last_reset = Some(now.with_second(0).unwrap());
assert_eq!(c.timebar_ratio(now.with_second(30).unwrap()), Some(0.5));
info!("30s=0.5");
assert_eq!(
c.timebar_ratio(now.with_second(59).unwrap()),
Some(0.9833333333333333)
);
info!("60s=1.0");
assert_eq!(c.timebar_ratio(now.with_second(0).unwrap()), Some(0.0));
info!("0s=0.0");
}
{
let mut data = UiData::default();
data.update("date".to_owned(), "time".to_owned(), Some(0.1));
assert_eq!(data.timebar_ratio(), Some(0.1));
data.update("date".to_owned(), "time".to_owned(), Some(0.2));
assert_eq!(data.timebar_ratio(), Some(0.2));
data.update("date".to_owned(), "time".to_owned(), Some(0.3));
assert_eq!(data.timebar_ratio(), Some(0.3));
}
info!("finished the mock tests");
}