From afc5acd3acb11336fef5d2894661b0659f78c13d Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 10 Jul 2024 11:56:27 +0200 Subject: [PATCH] refactor: move some things around and make them more clear --- src/clock.rs | 41 +++++++++++++++++++++++------------------ src/main.rs | 15 +++++++++++++++ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index 72881c8..580ed6a 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -83,10 +83,9 @@ impl Clock { fn timebar_ratio(&self) -> Option { let len = self.timebar_len()?; - let since = (Local::now() + let since = Local::now() .signed_duration_since(self.last_reset.unwrap()) - .num_seconds() - + 1) as f64; + .num_seconds() as f64; Some((since / len.as_secs() as f64).min(1.0).max(0.0)) } @@ -94,30 +93,29 @@ impl Clock { if let Some(len) = self.timebar_len() { trace!("Local Time: {}", Local::now()); // BUG: these resets trigger multiple times + let since_last_reset = Local::now().signed_duration_since(self.last_reset.unwrap()); match len { TimeBarLength::Custom(_) => { - if Local::now() - .signed_duration_since(self.last_reset.unwrap()) - .num_seconds() - >= len.as_secs() + if since_last_reset.num_seconds() >= 1 + && since_last_reset.num_seconds() >= len.as_secs() { self.last_reset = Some(Local::now()); } } TimeBarLength::Minute => { - if Local::now().second() == 0 { + if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 { self.last_reset = Some(Local::now()); debug!("reset the time of the time bar (minute)"); } } TimeBarLength::Hour => { - if Local::now().minute() == 0 { + if since_last_reset.num_minutes() >= 1 && Local::now().minute() == 0 { self.last_reset = Some(Local::now()); debug!("reset the time of the time bar (hour)"); } } TimeBarLength::Day => { - if Local::now().hour() == 0 { + if since_last_reset.num_hours() >= 1 && Local::now().hour() == 0 { self.last_reset = Some(Local::now()); debug!("reset the time of the time bar (day)"); } @@ -181,7 +179,8 @@ impl Clock { .collect(); let fdate: String = splits[0].clone(); let ftime: String = splits[1].clone(); - self.ui(terminal, ftime, fdate)?; + let timebar_ratio = self.timebar_ratio(); + self.ui(terminal, ftime, fdate, timebar_ratio)?; let timeout = tick_rate.saturating_sub(last_tick.elapsed()); if poll(timeout)? { if let Event::Key(key) = event::read()? { @@ -208,6 +207,7 @@ impl Clock { terminal: &mut Terminal>, ftime: String, fdate: String, + timebar_ratio: Option, ) -> anyhow::Result<()> { terminal.draw(|frame| { let root = frame.size(); @@ -241,10 +241,8 @@ impl Clock { ))); frame.render_widget(space, root); - frame.render_widget(clockw, parts[0]); - frame.render_widget(datew, parts[1]); - if self.timebar_len().is_some() { - let timebarw = LineGauge::default() + let timebarw: Option = if self.timebar_len().is_some() { + let tmp = LineGauge::default() .filled_style(Style::default().blue()) .unfilled_style(Style::default()) .block(Block::new().padding(Padding::new( @@ -253,10 +251,17 @@ impl Clock { 0, 0, ))) - .ratio(self.timebar_ratio().unwrap()); + .ratio(timebar_ratio.unwrap()); debug!("time bar ration: {}", self.timebar_ratio().unwrap()); - frame.render_widget(timebarw, parts[2]); - } + Some(tmp) + } else { + None + }; + debug!("rendering the configured widgets"); + frame.render_widget(clockw, parts[0]); + frame.render_widget(&timebarw, parts[2]); + frame.render_widget(datew, parts[1]); + debug!("done rendering"); })?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 557f8c3..80091a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,21 @@ use self::clock::Clock; mod clock; fn main() -> anyhow::Result<()> { + // setup the cli + let clock = Clock::parse(); + if clock.verbose.level() >= Level::DEBUG { + let _logger = Logger::builder() + .log_to_file(true) + .log_dir("/tmp/crock/".into()) + .set_level(clock.verbose.level()) + .display_time(true) + .build()?; + } else { + // no logger + } + debug!("set up logger"); + + debug!("taking over terminal"); // setup terminal enable_raw_mode()?; let mut stdout = io::stdout();