fix: WIP #10
cargo devel CI / cargo CI (push) Successful in 1m33s Details

This commit is contained in:
Christoph J. Scherr 2024-07-11 16:16:25 +02:00
parent cdc912b7bf
commit 8c74f9123a
1 changed files with 43 additions and 15 deletions

View File

@ -1,7 +1,7 @@
#![warn(clippy::pedantic, clippy::style, clippy::nursery)]
#![allow(clippy::question_mark_used)]
use chrono::{DateTime, Local, SubsecRound, Timelike};
use chrono::{DateTime, Local, SubsecRound, TimeZone, Timelike};
use clap::Parser;
use libpt::cli::args::HELP_TEMPLATE;
use libpt::cli::clap::ArgGroup;
@ -82,11 +82,10 @@ impl UiData {
self.fdate[self.data_idx] = fdate;
self.ftime[self.data_idx] = ftime;
self.timebar_ratio[self.data_idx] = timebar_ratio;
trace!(
"data after update: {:#?}\nconsidered changed: {}",
self,
self.changed()
);
#[cfg(debug_assertions)]
if self.changed() {
trace!("update with change: {:#?}", self);
}
}
/// did the data change with the last update?
@ -96,7 +95,6 @@ impl UiData {
// the timebar ratio is discarded, so that we only render the ui when the time
// (second) changes
let r = self.fdate[0] != self.fdate[1] || self.ftime[0] != self.ftime[1];
trace!("changed: {r}");
r
}
@ -136,22 +134,28 @@ impl Clock {
}
}
// 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
#[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
fn timebar_ratio(&self) -> 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 since = (Local::now()
let since = current_time
.signed_duration_since(self.last_reset.unwrap())
.num_seconds()
+ 1) as f64;
.num_seconds() as f64;
#[cfg(debug_assertions)]
if since < 1.0 {
trace!("ratio calculation since is now <1: {:#?}", since);
}
Some((since / len.as_secs() as f64).clamp(0.0, 1.0))
}
fn maybe_reset_since_zero(&mut self) {
if let Some(len) = self.timebar_len() {
trace!("Local Time: {}", Local::now());
let since_last_reset = Local::now().signed_duration_since(self.last_reset.unwrap());
match len {
TimeBarLength::Custom(_) => {
@ -218,6 +222,26 @@ impl Clock {
#[allow(clippy::unnecessary_wraps)] // we have that to be future proof
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();
Ok(())
}
@ -239,7 +263,11 @@ impl Clock {
.map(str::to_string)
.collect();
uidata.update(splits[0].clone(), splits[1].clone(), self.timebar_ratio());
uidata.update(
splits[0].clone(),
splits[1].clone(),
self.timebar_ratio(raw_time),
);
if uidata.changed() {
self.ui(terminal, &uidata)?;
}