Merge branch 'devel'
Rust CI / cargo CI (push) Has been cancelled Details
Release-plz / Release-plz release (push) Has been cancelled Details
Release-plz / Release-plz PR (push) Has been cancelled Details

This commit is contained in:
Christoph J. Scherr 2024-10-16 20:40:38 +02:00
commit cf4e8b6011
4 changed files with 34 additions and 19 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "crock"
version = "0.3.0"
version = "0.3.1-alpha.0"
edition = "2021"
publish = true
authors = ["Christoph J. Scherr <software@cscherr.de>"]

View File

@ -70,8 +70,6 @@ pub struct Clock {
pub(crate) last_reset: Option<DateTime<Local>>,
#[clap(skip)]
pub(crate) did_notify: bool,
#[clap(skip)]
pub(crate) started_at: DateTime<Local>,
}
impl Clock {
@ -119,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));
@ -127,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)");
}
}
@ -208,7 +222,7 @@ impl Clock {
) -> anyhow::Result<()> {
let tick_rate = std::time::Duration::from_millis(100);
let mut last_tick = Instant::now();
let mut uidata: Data = Data::new(self.timebar_len().unwrap());
let mut uidata: Data = Data::new(self.timebar_len());
self.setup()?;
loop {
let raw_time = chrono::Local::now().round_subsecs(0);
@ -408,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)]
@ -18,21 +19,21 @@ pub struct Data {
ftime: [String; 2],
timebar_ratio: [Option<f64>; 2],
timebar_type: TimeBarLength,
timebar_type: Option<TimeBarLength>,
started_at: DateTime<Local>,
idx: usize,
}
impl Data {
pub fn new(timebar_type: TimeBarLength) -> Self {
pub fn new(timebar_type: Option<TimeBarLength>) -> Self {
let mut this = Self {
now: [Default::default(); 2],
now: [DateTime::default(); 2],
fdate: [String::new(), String::new()],
ftime: [String::new(), String::new()],
timebar_ratio: [Default::default(); 2],
timebar_ratio: [Option::default(); 2],
started_at: Local::now(),
idx: Default::default(),
idx: usize::default(),
timebar_type,
};
@ -90,7 +91,7 @@ impl Data {
#[inline]
#[allow(clippy::missing_const_for_fn)] // no it's not const
pub fn timebar_ratio(&self) -> Option<f64> {
if self.timebar_type == TimeBarLength::Timer {
if self.timebar_type.is_some() && self.timebar_type.unwrap() == TimeBarLength::Timer {
return Some(0.0);
}
self.timebar_ratio[self.idx]
@ -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)
),
};

View File

@ -90,7 +90,7 @@ fn mock_tests() {
info!("0s=0.0");
}
{
let mut data = Data::new(clock::timebar::TimeBarLength::Day);
let mut data = Data::new(None);
let now = Local::now();
data.update(now, "date".to_owned(), "time".to_owned(), Some(0.1));
assert_eq!(data.timebar_ratio(), Some(0.1));