Compare commits

..

No commits in common. "cf4e8b6011066f1bcde499b5d2289b79f9e47438" and "b0c605452abbb29b00a9d2410b803c4a5cff848b" have entirely different histories.

4 changed files with 19 additions and 34 deletions

View file

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

View file

@ -70,6 +70,8 @@ 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 {
@ -117,8 +119,7 @@ impl Clock {
// the count up should not reset. If the time is over, just keep it at 100%
}
TimeBarLength::Custom(_) => {
// BUG: this is not consistent, sometimes leads to wrong seconds
if since_last_reset.num_milliseconds() >= 100
if since_last_reset.num_seconds() >= 1
&& since_last_reset.num_seconds() >= len.as_secs()
{
self.last_reset = Some(Local::now().round_subsecs(0));
@ -126,34 +127,19 @@ impl Clock {
}
TimeBarLength::Minute => {
if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 {
self.last_reset = Some(
Local::now()
.round_subsecs(0)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
self.last_reset = Some(Local::now().round_subsecs(0));
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)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
self.last_reset = Some(Local::now().round_subsecs(0));
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)
.with_second(1)
.expect("tried to use a time that does not exist"),
);
self.last_reset = Some(Local::now().round_subsecs(0));
debug!("reset the time of the time bar (day)");
}
}
@ -222,7 +208,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());
let mut uidata: Data = Data::new(self.timebar_len().unwrap());
self.setup()?;
loop {
let raw_time = chrono::Local::now().round_subsecs(0);
@ -422,7 +408,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.32) as u16;
let hlen_date: u16 = (f32::from(part[1].width) * 0.35) as u16;
let subparts = Layout::default()
.direction(Direction::Horizontal)
.constraints([

View file

@ -9,7 +9,6 @@ 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)]
@ -19,21 +18,21 @@ pub struct Data {
ftime: [String; 2],
timebar_ratio: [Option<f64>; 2],
timebar_type: Option<TimeBarLength>,
timebar_type: TimeBarLength,
started_at: DateTime<Local>,
idx: usize,
}
impl Data {
pub fn new(timebar_type: Option<TimeBarLength>) -> Self {
pub fn new(timebar_type: TimeBarLength) -> Self {
let mut this = Self {
now: [DateTime::default(); 2],
now: [Default::default(); 2],
fdate: [String::new(), String::new()],
ftime: [String::new(), String::new()],
timebar_ratio: [Option::default(); 2],
timebar_ratio: [Default::default(); 2],
started_at: Local::now(),
idx: usize::default(),
idx: Default::default(),
timebar_type,
};
@ -91,7 +90,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.is_some() && self.timebar_type.unwrap() == TimeBarLength::Timer {
if self.timebar_type == TimeBarLength::Timer {
return Some(0.0);
}
self.timebar_ratio[self.idx]
@ -194,17 +193,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_SHORT);
.format(TIME_FORMAT);
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_SHORT)
last_reset.format(TIME_FORMAT)
),
_ => format!(
"{time_now} / {len} | {} -> {until}",
last_reset.with_second(0).unwrap().format(TIME_FORMAT_SHORT)
last_reset.with_second(0).unwrap().format(TIME_FORMAT)
),
};

View file

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