From edeadb313cfb255d71e580767e0530e7b0dfba19 Mon Sep 17 00:00:00 2001 From: cscherrNT Date: Wed, 10 Jul 2024 10:40:51 +0000 Subject: [PATCH 1/2] automatic cargo CI changes --- src/clock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clock.rs b/src/clock.rs index d634e83..ee51bcb 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -66,7 +66,7 @@ pub struct Clock { } #[derive(Debug, Clone, PartialEq, Default)] -pub(crate) struct UiData { +pub struct UiData { fdate: [String; 2], ftime: [String; 2], timebar_ratio: [Option; 2], From df3850c1ca24d70dec3786f6c84798fabce8eac1 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 10 Jul 2024 14:57:59 +0200 Subject: [PATCH 2/2] fix: sync timebar with clock --- src/clock.rs | 73 ++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index d634e83..786f06d 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -14,6 +14,7 @@ use ratatui::style::{Style, Stylize}; use ratatui::widgets::{Block, LineGauge, Padding, Paragraph}; use ratatui::Terminal; use std::io::Stdout; +use std::thread::{sleep, sleep_ms}; use std::time::{Duration, Instant}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -80,15 +81,24 @@ impl UiData { self.fdate[self.data_idx] = fdate; self.ftime[self.data_idx] = ftime; self.timebar_ratio[self.data_idx] = timebar_ratio; + trace!( + "data after update: {:#?}\nconsidered changed: {}", + self, + self.changed() + ); } /// did the data change with the last update? #[must_use] #[inline] pub fn changed(&self) -> bool { - !(self.fdate[0] == self.fdate[1] - && self.ftime[0] == self.fdate[1] - && self.timebar_ratio[0] == self.timebar_ratio[1]) + let r = + self.fdate[0] != self.fdate[1] || self.ftime[0] != self.ftime[1] + //&& self.timebar_ratio[0] == self.timebar_ratio[1] + // NOTE: the timebar ratio is discarded, so that we only render the ui when the time (second) changes + ; + trace!("changed: {r}"); + r } #[must_use] @@ -255,7 +265,14 @@ impl Clock { terminal: &mut Terminal>, data: &UiData, ) -> anyhow::Result<()> { + let clockw = tui_big_text::BigText::builder() + .style(Style::new().red()) + .lines(vec![data.ftime().into()]) + .alignment(Alignment::Center) + .build() + .expect("could not render time widget"); terminal.draw(|frame| { + debug!("rendering the ui"); let root = frame.size(); let space = Block::bordered() .padding(Padding::new( @@ -269,25 +286,14 @@ impl Clock { .title_alignment(Alignment::Center) .title_style(Style::new().bold()); let a = space.inner(root); - let parts = Self::partition(a); - let clockw = tui_big_text::BigText::builder() - .style(Style::new().red()) - .lines(vec![data.ftime().into()]) - .alignment(Alignment::Center) - .build() - .expect("could not render time widget"); - let datew = Paragraph::new(data.fdate()) - .blue() - .alignment(Alignment::Left) - .block(Block::new().padding(Padding::new( - parts[1].left(), - parts[1].right() / 3, - 0, - 0, - ))); - frame.render_widget(space, root); + let parts = Self::partition(a); + + // render the timebar which counts up to the full minute and so on + // + // Will not be rendered if it is None let timebarw: Option = if self.timebar_len().is_some() { + debug!("time bar ration: {:?}", data.timebar_ratio()); let tmp = LineGauge::default() .filled_style(Style::default().blue()) .unfilled_style(Style::default()) @@ -298,22 +304,27 @@ impl Clock { 0, ))) .ratio(data.timebar_ratio().unwrap()); - debug!("time bar ration: {}", self.timebar_ratio().unwrap()); Some(tmp) } else { None }; - debug!("rendering the configured widgets"); - #[cfg(debug_assertions)] - let prerender = Instant::now(); - frame.render_widget(clockw, parts[0]); - trace!("{:?} after render clockw", prerender.elapsed()); + + // render the small date + let datew = Paragraph::new(data.fdate()) + .blue() + .alignment(Alignment::Left) + .block(Block::new().padding(Padding::new( + parts[1].left(), + parts[1].right() / 3, + 0, + 0, + ))); frame.render_widget(&timebarw, parts[2]); - trace!("{:?} after render timebarw", prerender.elapsed()); frame.render_widget(datew, parts[1]); - trace!("{:?} to render all widgets", prerender.elapsed()); - debug!("done rendering"); + // render the clock + frame.render_widget(clockw, parts[0]); })?; + debug!("done rendering the ui"); Ok(()) } fn partition(r: Rect) -> Vec { @@ -329,7 +340,3 @@ impl Clock { vec![part[0], subparts[0], subparts[1]] } } - -fn sleep(ms: u64) { - std::thread::sleep(std::time::Duration::from_millis(ms)); -}