Add option to hide clock seconds

This commit is contained in:
Wesley Moore 2022-08-11 11:41:03 +10:00 committed by Jimmy
parent 484440905e
commit 55346b5b79
3 changed files with 11 additions and 3 deletions

View file

@ -21,6 +21,9 @@ pub(crate) enum Mode {
/// Do not show date /// Do not show date
#[clap(short = 'D', long, takes_value = false)] #[clap(short = 'D', long, takes_value = false)]
no_date: bool, no_date: bool,
/// Do not show seconds
#[clap(short = 'S', long, takes_value = false)]
no_seconds: bool,
/// Show milliseconds /// Show milliseconds
#[clap(short, long, takes_value = false)] #[clap(short, long, takes_value = false)]
millis: bool, millis: bool,
@ -71,14 +74,16 @@ impl App {
let mode = self.mode.as_ref().unwrap_or(&Mode::Clock { let mode = self.mode.as_ref().unwrap_or(&Mode::Clock {
no_date: false, no_date: false,
millis: false, millis: false,
no_seconds: false,
}); });
match mode { match mode {
Mode::Clock { no_date, millis } => { Mode::Clock { no_date, no_seconds, millis } => {
self.clock = Some(Clock { self.clock = Some(Clock {
size: self.size, size: self.size,
style, style,
show_date: !no_date.to_owned(), show_date: !no_date.to_owned(),
show_millis: millis.to_owned(), show_millis: millis.to_owned(),
show_secs: !*no_seconds,
}); });
} }
Mode::Timer { Mode::Timer {

View file

@ -9,6 +9,7 @@ pub(crate) struct Clock {
pub style: Style, pub style: Style,
pub show_date: bool, pub show_date: bool,
pub show_millis: bool, pub show_millis: bool,
pub show_secs: bool,
} }
impl Widget for &Clock { impl Widget for &Clock {
@ -18,8 +19,10 @@ impl Widget for &Clock {
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
} else { } else if self.show_secs {
now.format("%H:%M:%S").to_string() now.format("%H:%M:%S").to_string()
} else {
now.format("%H:%M").to_string()
}; };
let time_str = time_str.as_str(); let time_str = time_str.as_str();
let text = BricksText::new(time_str, self.size, self.size, self.style); let text = BricksText::new(time_str, self.size, self.size, self.style);

View file

@ -61,7 +61,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// create app and run it // create app and run it
let low_rate = match app.mode { let low_rate = match app.mode {
Some(Mode::Clock { millis, .. }) => !millis, Some(Mode::Clock { millis, no_seconds, .. }) => !millis || no_seconds,
Some(Mode::Timer { no_millis, .. }) => no_millis, Some(Mode::Timer { no_millis, .. }) => no_millis,
_ => false, _ => false,
}; };