fix: sync timebar with clock

This commit is contained in:
Christoph J. Scherr 2024-07-10 14:57:59 +02:00
parent 107092b558
commit 2be3470587
1 changed files with 40 additions and 33 deletions

View File

@ -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<CrosstermBackend<Stdout>>,
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<LineGauge> = 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<Rect> {
@ -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));
}