Compare commits

...

4 Commits

Author SHA1 Message Date
Christoph J. Scherr 757fe6b7b2 chore: bump to v0.3.1-alpha.0
cargo devel CI / cargo CI (push) Successful in 1m45s Details
2024-09-05 11:38:02 +02:00
Christoph J. Scherr 140123c124 fix: crashed when no timebar was requested because we unwrapped for the creation of Ui Data #26
cargo devel CI / cargo CI (push) Has been cancelled Details
2024-09-05 11:35:51 +02:00
Christoph J. Scherr 04c570243c fix(timebar): shortened format and refactored
cargo devel CI / cargo CI (push) Successful in 1m43s Details
2024-09-03 20:39:08 +02:00
Christoph J. Scherr 89a636589d chore: some more clippy findings
cargo devel CI / cargo CI (push) Successful in 1m47s Details
2024-09-03 19:16:25 +02:00
4 changed files with 34 additions and 19 deletions

View File

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

View File

@ -70,8 +70,6 @@ pub struct Clock {
pub(crate) last_reset: Option<DateTime<Local>>, pub(crate) last_reset: Option<DateTime<Local>>,
#[clap(skip)] #[clap(skip)]
pub(crate) did_notify: bool, pub(crate) did_notify: bool,
#[clap(skip)]
pub(crate) started_at: DateTime<Local>,
} }
impl Clock { impl Clock {
@ -119,7 +117,8 @@ impl Clock {
// the count up should not reset. If the time is over, just keep it at 100% // the count up should not reset. If the time is over, just keep it at 100%
} }
TimeBarLength::Custom(_) => { 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() && since_last_reset.num_seconds() >= len.as_secs()
{ {
self.last_reset = Some(Local::now().round_subsecs(0)); self.last_reset = Some(Local::now().round_subsecs(0));
@ -127,19 +126,34 @@ impl Clock {
} }
TimeBarLength::Minute => { TimeBarLength::Minute => {
if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 { 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)"); debug!("reset the time of the time bar (minute)");
} }
} }
TimeBarLength::Hour => { TimeBarLength::Hour => {
if since_last_reset.num_minutes() >= 1 && Local::now().minute() == 0 { 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)"); debug!("reset the time of the time bar (hour)");
} }
} }
TimeBarLength::Day => { TimeBarLength::Day => {
if since_last_reset.num_hours() >= 1 && Local::now().hour() == 0 { 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)"); debug!("reset the time of the time bar (day)");
} }
} }
@ -208,7 +222,7 @@ impl Clock {
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let tick_rate = std::time::Duration::from_millis(100); let tick_rate = std::time::Duration::from_millis(100);
let mut last_tick = Instant::now(); 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()?; self.setup()?;
loop { loop {
let raw_time = chrono::Local::now().round_subsecs(0); let raw_time = chrono::Local::now().round_subsecs(0);
@ -408,7 +422,7 @@ impl Clock {
.split(r); .split(r);
#[allow(clippy::cast_sign_loss)] #[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)] #[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() let subparts = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([ .constraints([

View File

@ -9,6 +9,7 @@ use crate::clock::timebar::TimeBarLength;
use super::Clock; use super::Clock;
pub const TIME_FORMAT: &str = "%H:%M:%S"; 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? // TODO: make this a ringbuffer with a custom struct inside?
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -18,21 +19,21 @@ pub struct Data {
ftime: [String; 2], ftime: [String; 2],
timebar_ratio: [Option<f64>; 2], timebar_ratio: [Option<f64>; 2],
timebar_type: TimeBarLength, timebar_type: Option<TimeBarLength>,
started_at: DateTime<Local>, started_at: DateTime<Local>,
idx: usize, idx: usize,
} }
impl Data { impl Data {
pub fn new(timebar_type: TimeBarLength) -> Self { pub fn new(timebar_type: Option<TimeBarLength>) -> Self {
let mut this = Self { let mut this = Self {
now: [Default::default(); 2], now: [DateTime::default(); 2],
fdate: [String::new(), String::new()], fdate: [String::new(), String::new()],
ftime: [String::new(), String::new()], ftime: [String::new(), String::new()],
timebar_ratio: [Default::default(); 2], timebar_ratio: [Option::default(); 2],
started_at: Local::now(), started_at: Local::now(),
idx: Default::default(), idx: usize::default(),
timebar_type, timebar_type,
}; };
@ -90,7 +91,7 @@ impl Data {
#[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 fn timebar_ratio(&self) -> Option<f64> { 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); return Some(0.0);
} }
self.timebar_ratio[self.idx] self.timebar_ratio[self.idx]
@ -193,17 +194,17 @@ pub fn timebarw_label<'a>(
// example with `-o` #17 // example with `-o` #17
.checked_add_signed(len.into()) .checked_add_signed(len.into())
.expect("could not calculate when the countdown finishes") .expect("could not calculate when the countdown finishes")
.format(TIME_FORMAT); .format(TIME_FORMAT_SHORT);
let text: String = match clock.timebar_len().unwrap() { let text: String = match clock.timebar_len().unwrap() {
TimeBarLength::Timer => format!("{} + {time_now}", data.started_at.format(TIME_FORMAT)), TimeBarLength::Timer => format!("{} + {time_now}", data.started_at.format(TIME_FORMAT)),
TimeBarLength::Countup(_) | TimeBarLength::Custom(_) => format!( TimeBarLength::Countup(_) | TimeBarLength::Custom(_) => format!(
"{time_now} / {len} | {} -> {until}", "{time_now} / {len} | {} -> {until}",
last_reset.format(TIME_FORMAT) last_reset.format(TIME_FORMAT_SHORT)
), ),
_ => format!( _ => format!(
"{time_now} / {len} | {} -> {until}", "{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"); info!("0s=0.0");
} }
{ {
let mut data = Data::new(clock::timebar::TimeBarLength::Day); let mut data = Data::new(None);
let now = Local::now(); let now = Local::now();
data.update(now, "date".to_owned(), "time".to_owned(), Some(0.1)); data.update(now, "date".to_owned(), "time".to_owned(), Some(0.1));
assert_eq!(data.timebar_ratio(), Some(0.1)); assert_eq!(data.timebar_ratio(), Some(0.1));