From 7ce5a117f235c12fba50a5bb13e907246acbc8bc Mon Sep 17 00:00:00 2001 From: race604 Date: Wed, 20 Jul 2022 11:57:09 +0800 Subject: [PATCH] support non_date and millis options for Clock --- src/app.rs | 19 +++++++++++++++---- src/app/modes/clock.rs | 28 ++++++++++++++++------------ src/main.rs | 8 +++++++- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index 909f439..cb9bbcd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -16,7 +16,14 @@ pub(crate) mod modes; #[derive(Debug, Subcommand)] pub(crate) enum Mode { /// The clock mode displays the current time, the default mode. - Clock, + Clock { + /// Do not show date + #[clap(short = 'D', long, takes_value = false)] + no_date: bool, + /// Show milliseconds + #[clap(short, long, takes_value = false)] + millis: bool, + }, /// The timer mode displays the remaining time until the timer is finished. Timer { #[clap(short, long, value_parser = parse_duration, default_value = "5m")] @@ -47,13 +54,17 @@ pub(crate) struct App { impl App { pub fn init_app(&mut self) { let style = Style::default().fg(self.color); - let mode = self.mode.as_ref().unwrap_or(&Mode::Clock); + let mode = self.mode.as_ref().unwrap_or(&Mode::Clock { + no_date: false, + millis: false, + }); match mode { - Mode::Clock => { + Mode::Clock { no_date, millis } => { self.clock = Some(Clock { size: self.size, style, - long: false, + show_date: !no_date.to_owned(), + show_millis: millis.to_owned(), }); } Mode::Timer { duration } => { diff --git a/src/app/modes/clock.rs b/src/app/modes/clock.rs index 299c0e0..fe4463c 100644 --- a/src/app/modes/clock.rs +++ b/src/app/modes/clock.rs @@ -12,13 +12,14 @@ use tui::{ pub(crate) struct Clock { pub size: u16, pub style: Style, - pub long: bool, + pub show_date: bool, + pub show_millis: bool, } impl Widget for &Clock { fn render(self, area: Rect, buf: &mut tui::buffer::Buffer) { let now = Local::now(); - let time_str = if self.long { + let time_str = if self.show_millis { let mut str = now.format("%H:%M:%S%.3f").to_string(); str.truncate(str.len() - 2); str @@ -35,16 +36,19 @@ impl Widget for &Clock { height: min(text_size.0, area.height), }; text.render(text_area, buf); - let text = now.format("%Y-%m-%d %Z").to_string(); - let text_len = text.as_str().len() as u16; - let paragrahp = Paragraph::new(Span::from(text)).style(Style::default()); - let para_area = Rect { - x: area.x + (area.width.saturating_sub(text_len)) / 2, - y: text_area.y.saturating_sub(2), - width: min(text_len, area.width), - height: min(1, area.height), - }; - paragrahp.render(para_area, buf); + if self.show_date { + let text = now.format("%Y-%m-%d %Z").to_string(); + let text_len = text.as_str().len() as u16; + let paragrahp = Paragraph::new(Span::from(text)).style(Style::default()); + + let para_area = Rect { + x: area.x + (area.width.saturating_sub(text_len)) / 2, + y: text_area.y.saturating_sub(2), + width: min(text_len, area.width), + height: min(1, area.height), + }; + paragrahp.render(para_area, buf); + } } } diff --git a/src/main.rs b/src/main.rs index cd3bea8..f7704a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use std::{ time::{Duration, Instant}, }; +use app::Mode; use clap::Parser; use crossterm::{ event::{self, Event, KeyCode}, @@ -59,7 +60,12 @@ fn main() -> Result<(), Box> { let mut terminal = Terminal::new(backend)?; // create app and run it - let tick_rate = Duration::from_millis(100); + let low_rate = if let Some(Mode::Clock { millis, .. }) = app.mode { + !millis + } else { + false + }; + let tick_rate = Duration::from_millis(if low_rate { 20 } else { 200 }); let res = run_app(&mut terminal, &mut app, tick_rate); // restore terminal