support non_date and millis options for Clock

This commit is contained in:
race604 2022-07-20 11:57:09 +08:00
parent b8d6e42c4f
commit 7ce5a117f2
3 changed files with 38 additions and 17 deletions

View file

@ -16,7 +16,14 @@ pub(crate) mod modes;
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub(crate) enum Mode { pub(crate) enum Mode {
/// The clock mode displays the current time, the default 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. /// The timer mode displays the remaining time until the timer is finished.
Timer { Timer {
#[clap(short, long, value_parser = parse_duration, default_value = "5m")] #[clap(short, long, value_parser = parse_duration, default_value = "5m")]
@ -47,13 +54,17 @@ pub(crate) struct App {
impl App { impl App {
pub fn init_app(&mut self) { pub fn init_app(&mut self) {
let style = Style::default().fg(self.color); 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 { match mode {
Mode::Clock => { Mode::Clock { no_date, millis } => {
self.clock = Some(Clock { self.clock = Some(Clock {
size: self.size, size: self.size,
style, style,
long: false, show_date: !no_date.to_owned(),
show_millis: millis.to_owned(),
}); });
} }
Mode::Timer { duration } => { Mode::Timer { duration } => {

View file

@ -12,13 +12,14 @@ use tui::{
pub(crate) struct Clock { pub(crate) struct Clock {
pub size: u16, pub size: u16,
pub style: Style, pub style: Style,
pub long: bool, pub show_date: bool,
pub show_millis: bool,
} }
impl Widget for &Clock { impl Widget for &Clock {
fn render(self, area: Rect, buf: &mut tui::buffer::Buffer) { fn render(self, area: Rect, buf: &mut tui::buffer::Buffer) {
let now = Local::now(); 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(); let mut str = now.format("%H:%M:%S%.3f").to_string();
str.truncate(str.len() - 2); str.truncate(str.len() - 2);
str str
@ -35,6 +36,8 @@ impl Widget for &Clock {
height: min(text_size.0, area.height), height: min(text_size.0, area.height),
}; };
text.render(text_area, buf); text.render(text_area, buf);
if self.show_date {
let text = now.format("%Y-%m-%d %Z").to_string(); let text = now.format("%Y-%m-%d %Z").to_string();
let text_len = text.as_str().len() as u16; let text_len = text.as_str().len() as u16;
let paragrahp = Paragraph::new(Span::from(text)).style(Style::default()); let paragrahp = Paragraph::new(Span::from(text)).style(Style::default());
@ -48,3 +51,4 @@ impl Widget for &Clock {
paragrahp.render(para_area, buf); paragrahp.render(para_area, buf);
} }
} }
}

View file

@ -4,6 +4,7 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use app::Mode;
use clap::Parser; use clap::Parser;
use crossterm::{ use crossterm::{
event::{self, Event, KeyCode}, event::{self, Event, KeyCode},
@ -59,7 +60,12 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut terminal = Terminal::new(backend)?; let mut terminal = Terminal::new(backend)?;
// create app and run it // 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); let res = run_app(&mut terminal, &mut app, tick_rate);
// restore terminal // restore terminal