add a time bar #8

Merged
PlexSheep merged 18 commits from feat/timebar into devel 2024-07-11 16:55:49 +02:00
2 changed files with 24 additions and 19 deletions
Showing only changes of commit 4da80e8f0f - Show all commits

View File

@ -83,10 +83,9 @@ impl Clock {
fn timebar_ratio(&self) -> Option<f64> { fn timebar_ratio(&self) -> Option<f64> {
let len = self.timebar_len()?; let len = self.timebar_len()?;
let since = (Local::now() let since = Local::now()
.signed_duration_since(self.last_reset.unwrap()) .signed_duration_since(self.last_reset.unwrap())
.num_seconds() .num_seconds() as f64;
+ 1) as f64;
Some((since / len.as_secs() as f64).min(1.0).max(0.0)) 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() { if let Some(len) = self.timebar_len() {
trace!("Local Time: {}", Local::now()); trace!("Local Time: {}", Local::now());
// BUG: these resets trigger multiple times // BUG: these resets trigger multiple times
let since_last_reset = Local::now().signed_duration_since(self.last_reset.unwrap());
match len { match len {
TimeBarLength::Custom(_) => { TimeBarLength::Custom(_) => {
if Local::now() if since_last_reset.num_seconds() >= 1
.signed_duration_since(self.last_reset.unwrap()) && since_last_reset.num_seconds() >= len.as_secs()
.num_seconds()
>= len.as_secs()
{ {
self.last_reset = Some(Local::now()); self.last_reset = Some(Local::now());
} }
} }
TimeBarLength::Minute => { TimeBarLength::Minute => {
if Local::now().second() == 0 { if since_last_reset.num_seconds() >= 1 && Local::now().second() == 0 {
self.last_reset = Some(Local::now()); self.last_reset = Some(Local::now());
debug!("reset the time of the time bar (minute)"); debug!("reset the time of the time bar (minute)");
} }
} }
TimeBarLength::Hour => { TimeBarLength::Hour => {
if Local::now().minute() == 0 { if since_last_reset.num_minutes() >= 1 && Local::now().minute() == 0 {
self.last_reset = Some(Local::now()); self.last_reset = Some(Local::now());
debug!("reset the time of the time bar (hour)"); debug!("reset the time of the time bar (hour)");
} }
} }
TimeBarLength::Day => { TimeBarLength::Day => {
if Local::now().hour() == 0 { if since_last_reset.num_hours() >= 1 && Local::now().hour() == 0 {
self.last_reset = Some(Local::now()); self.last_reset = Some(Local::now());
debug!("reset the time of the time bar (day)"); debug!("reset the time of the time bar (day)");
} }
@ -181,7 +179,8 @@ impl Clock {
.collect(); .collect();
let fdate: String = splits[0].clone(); let fdate: String = splits[0].clone();
let ftime: String = splits[1].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()); let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if poll(timeout)? { if poll(timeout)? {
if let Event::Key(key) = event::read()? { if let Event::Key(key) = event::read()? {
@ -208,6 +207,7 @@ impl Clock {
terminal: &mut Terminal<CrosstermBackend<Stdout>>, terminal: &mut Terminal<CrosstermBackend<Stdout>>,
ftime: String, ftime: String,
fdate: String, fdate: String,
timebar_ratio: Option<f64>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
terminal.draw(|frame| { terminal.draw(|frame| {
let root = frame.size(); let root = frame.size();
@ -241,10 +241,8 @@ impl Clock {
))); )));
frame.render_widget(space, root); frame.render_widget(space, root);
frame.render_widget(clockw, parts[0]); let timebarw: Option<LineGauge> = if self.timebar_len().is_some() {
frame.render_widget(datew, parts[1]); let tmp = LineGauge::default()
if self.timebar_len().is_some() {
let timebarw = LineGauge::default()
.filled_style(Style::default().blue()) .filled_style(Style::default().blue())
.unfilled_style(Style::default()) .unfilled_style(Style::default())
.block(Block::new().padding(Padding::new( .block(Block::new().padding(Padding::new(
@ -253,10 +251,17 @@ impl Clock {
0, 0,
0, 0,
))) )))
.ratio(self.timebar_ratio().unwrap()); .ratio(timebar_ratio.unwrap());
debug!("time bar ration: {}", self.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(()) Ok(())
} }

View File

@ -22,7 +22,7 @@ fn main() -> anyhow::Result<()> {
.log_to_file(true) .log_to_file(true)
.log_dir("/tmp/crock/".into()) .log_dir("/tmp/crock/".into())
.set_level(clock.verbose.level()) .set_level(clock.verbose.level())
.display_time(false) .display_time(true)
.build()?; .build()?;
} else { } else {
// no logger // no logger