fix(timebar): shortened format and refactored
cargo devel CI / cargo CI (push) Successful in 1m43s Details

This commit is contained in:
Christoph J. Scherr 2024-09-03 20:38:36 +02:00
parent 89a636589d
commit 04c570243c
2 changed files with 25 additions and 8 deletions

View File

@ -117,7 +117,8 @@ impl Clock {
// the count up should not reset. If the time is over, just keep it at 100%
}
TimeBarLength::Custom(_) => {
if since_last_reset.num_seconds() >= 1
// BUG: this is not consistent, sometimes leads to wrong seconds
if since_last_reset.num_milliseconds() >= 100
&& since_last_reset.num_seconds() >= len.as_secs()
{
self.last_reset = Some(Local::now().round_subsecs(0));
@ -125,19 +126,34 @@ impl Clock {
}
TimeBarLength::Minute => {
if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 {
self.last_reset = Some(Local::now().round_subsecs(0));
self.last_reset = Some(
Local::now()
.round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
debug!("reset the time of the time bar (minute)");
}
}
TimeBarLength::Hour => {
if since_last_reset.num_minutes() >= 1 && Local::now().minute() == 0 {
self.last_reset = Some(Local::now().round_subsecs(0));
self.last_reset = Some(
Local::now()
.round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
debug!("reset the time of the time bar (hour)");
}
}
TimeBarLength::Day => {
if since_last_reset.num_hours() >= 1 && Local::now().hour() == 0 {
self.last_reset = Some(Local::now().round_subsecs(0));
self.last_reset = Some(
Local::now()
.round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
debug!("reset the time of the time bar (day)");
}
}
@ -406,7 +422,7 @@ impl Clock {
.split(r);
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
let hlen_date: u16 = (f32::from(part[1].width) * 0.35) as u16;
let hlen_date: u16 = (f32::from(part[1].width) * 0.32) as u16;
let subparts = Layout::default()
.direction(Direction::Horizontal)
.constraints([

View File

@ -9,6 +9,7 @@ use crate::clock::timebar::TimeBarLength;
use super::Clock;
pub const TIME_FORMAT: &str = "%H:%M:%S";
pub const TIME_FORMAT_SHORT: &str = "%H:%M";
// TODO: make this a ringbuffer with a custom struct inside?
#[derive(Debug, Clone, PartialEq)]
@ -193,17 +194,17 @@ pub fn timebarw_label<'a>(
// example with `-o` #17
.checked_add_signed(len.into())
.expect("could not calculate when the countdown finishes")
.format(TIME_FORMAT);
.format(TIME_FORMAT_SHORT);
let text: String = match clock.timebar_len().unwrap() {
TimeBarLength::Timer => format!("{} + {time_now}", data.started_at.format(TIME_FORMAT)),
TimeBarLength::Countup(_) | TimeBarLength::Custom(_) => format!(
"{time_now} / {len} | {} -> {until}",
last_reset.format(TIME_FORMAT)
last_reset.format(TIME_FORMAT_SHORT)
),
_ => format!(
"{time_now} / {len} | {} -> {until}",
last_reset.with_second(0).unwrap().format(TIME_FORMAT)
last_reset.with_second(0).unwrap().format(TIME_FORMAT_SHORT)
),
};