generated from PlexSheep/rs-base
fix: sync timebar with clock
This commit is contained in:
parent
468c48d327
commit
df3850c1ca
73
src/clock.rs
73
src/clock.rs
|
@ -14,6 +14,7 @@ use ratatui::style::{Style, Stylize};
|
||||||
use ratatui::widgets::{Block, LineGauge, Padding, Paragraph};
|
use ratatui::widgets::{Block, LineGauge, Padding, Paragraph};
|
||||||
use ratatui::Terminal;
|
use ratatui::Terminal;
|
||||||
use std::io::Stdout;
|
use std::io::Stdout;
|
||||||
|
use std::thread::{sleep, sleep_ms};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
@ -80,15 +81,24 @@ impl UiData {
|
||||||
self.fdate[self.data_idx] = fdate;
|
self.fdate[self.data_idx] = fdate;
|
||||||
self.ftime[self.data_idx] = ftime;
|
self.ftime[self.data_idx] = ftime;
|
||||||
self.timebar_ratio[self.data_idx] = timebar_ratio;
|
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?
|
/// did the data change with the last update?
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn changed(&self) -> bool {
|
pub fn changed(&self) -> bool {
|
||||||
!(self.fdate[0] == self.fdate[1]
|
let r =
|
||||||
&& self.ftime[0] == self.fdate[1]
|
self.fdate[0] != self.fdate[1] || self.ftime[0] != self.ftime[1]
|
||||||
&& self.timebar_ratio[0] == self.timebar_ratio[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]
|
#[must_use]
|
||||||
|
@ -255,7 +265,14 @@ impl Clock {
|
||||||
terminal: &mut Terminal<CrosstermBackend<Stdout>>,
|
terminal: &mut Terminal<CrosstermBackend<Stdout>>,
|
||||||
data: &UiData,
|
data: &UiData,
|
||||||
) -> anyhow::Result<()> {
|
) -> 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| {
|
terminal.draw(|frame| {
|
||||||
|
debug!("rendering the ui");
|
||||||
let root = frame.size();
|
let root = frame.size();
|
||||||
let space = Block::bordered()
|
let space = Block::bordered()
|
||||||
.padding(Padding::new(
|
.padding(Padding::new(
|
||||||
|
@ -269,25 +286,14 @@ impl Clock {
|
||||||
.title_alignment(Alignment::Center)
|
.title_alignment(Alignment::Center)
|
||||||
.title_style(Style::new().bold());
|
.title_style(Style::new().bold());
|
||||||
let a = space.inner(root);
|
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);
|
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() {
|
let timebarw: Option<LineGauge> = if self.timebar_len().is_some() {
|
||||||
|
debug!("time bar ration: {:?}", data.timebar_ratio());
|
||||||
let tmp = LineGauge::default()
|
let tmp = LineGauge::default()
|
||||||
.filled_style(Style::default().blue())
|
.filled_style(Style::default().blue())
|
||||||
.unfilled_style(Style::default())
|
.unfilled_style(Style::default())
|
||||||
|
@ -298,22 +304,27 @@ impl Clock {
|
||||||
0,
|
0,
|
||||||
)))
|
)))
|
||||||
.ratio(data.timebar_ratio().unwrap());
|
.ratio(data.timebar_ratio().unwrap());
|
||||||
debug!("time bar ration: {}", self.timebar_ratio().unwrap());
|
|
||||||
Some(tmp)
|
Some(tmp)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
debug!("rendering the configured widgets");
|
|
||||||
#[cfg(debug_assertions)]
|
// render the small date
|
||||||
let prerender = Instant::now();
|
let datew = Paragraph::new(data.fdate())
|
||||||
frame.render_widget(clockw, parts[0]);
|
.blue()
|
||||||
trace!("{:?} after render clockw", prerender.elapsed());
|
.alignment(Alignment::Left)
|
||||||
|
.block(Block::new().padding(Padding::new(
|
||||||
|
parts[1].left(),
|
||||||
|
parts[1].right() / 3,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
)));
|
||||||
frame.render_widget(&timebarw, parts[2]);
|
frame.render_widget(&timebarw, parts[2]);
|
||||||
trace!("{:?} after render timebarw", prerender.elapsed());
|
|
||||||
frame.render_widget(datew, parts[1]);
|
frame.render_widget(datew, parts[1]);
|
||||||
trace!("{:?} to render all widgets", prerender.elapsed());
|
// render the clock
|
||||||
debug!("done rendering");
|
frame.render_widget(clockw, parts[0]);
|
||||||
})?;
|
})?;
|
||||||
|
debug!("done rendering the ui");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn partition(r: Rect) -> Vec<Rect> {
|
fn partition(r: Rect) -> Vec<Rect> {
|
||||||
|
@ -329,7 +340,3 @@ impl Clock {
|
||||||
vec![part[0], subparts[0], subparts[1]]
|
vec![part[0], subparts[0], subparts[1]]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sleep(ms: u64) {
|
|
||||||
std::thread::sleep(std::time::Duration::from_millis(ms));
|
|
||||||
}
|
|
||||||
|
|
Reference in New Issue