From 066b88eeee0d737972e73481b295b754603936e9 Mon Sep 17 00:00:00 2001 From: shy Date: Wed, 7 Apr 2021 10:08:57 +0200 Subject: [PATCH] Implemented "--quit". --- src/alarm.rs | 20 +++++++++++++++----- src/main.rs | 22 ++++++++++++++-------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/alarm.rs b/src/alarm.rs index 21c7d5d..08709df 100644 --- a/src/alarm.rs +++ b/src/alarm.rs @@ -90,14 +90,18 @@ impl AlarmRoster { for sub in buffer.rsplit(':') { if !sub.is_empty() { match sub.parse::() { + // Valid. Ok(d) if d < 60 && index < 3 => time += d * 60u32.pow(index), - Ok(_) => return Err("Could not parse as time."), - Err(_) => return Err("Could not parse number as ."), + // Passes as u32, but does not fit into time range. + Ok(_) => return Err("Could not parse value as time."), + // Could not parse to u32. + Err(_) => return Err("Could not parse value as integer."), } } index += 1; } } else { + // Parse as seconds only. match buffer.parse::() { Ok(d) => time = d, Err(_) => return Err("Could not parse as ."), @@ -115,7 +119,7 @@ impl AlarmRoster { exceeded: false, }; - // Add to list, insert based on alarm time. Filter out double entries. + // Add to list, insert based on alarm time. Disallow double entries. let mut i = self.list.len(); if i == 0 { self.list.push(alarm); @@ -134,8 +138,14 @@ impl AlarmRoster { Ok(()) } - pub fn pop(&mut self) -> Option { - self.list.pop() + // Remove last alarm. + pub fn drop_last(&mut self) -> bool { + self.list.pop().is_some() + } + + // Check for active alarms. + pub fn active(&self) -> bool { + self.list.iter().any(|a| !a.exceeded) } // Check for exceeded alarms. diff --git a/src/main.rs b/src/main.rs index 16e6005..d7fde3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,17 +22,18 @@ use layout::{Layout, Position}; const NAME: &str = env!("CARGO_PKG_NAME"); const VERSION: &str = env!("CARGO_PKG_VERSION"); const USAGE: &str = concat!("USAGE: ", env!("CARGO_PKG_NAME"), -" [-h|-v] [-p] [ALARM TIME(s)] [-e|--exec COMMAND [...]] +" [-h|-v] [-p] [-q] [ALARM TIME(s)] [-e|--exec COMMAND [...]] PARAMETERS: [ALARM TIME] None or multiple alarm times (HH:MM:SS). OPTIONS: -h, --help Display this help. -v, --version Display version information. -p, --plain Use simpler block chars. + -q, --quit Quit program after last alarm. -e, --exec [COMMAND] Execute COMMAND on alarm. Must be the last flag on the command line. Everything after it is passed as - argument to COMMAND. Every \"%s\" will be replaced - with the elapsed time in (HH:)MM:SS format. + argument to COMMAND. Every \"{}\" will be replaced + by the elapsed time in (HH:)MM:SS format. SIGNALS: Reset clock. Pause or un-pause clock."); @@ -51,6 +52,7 @@ const SIGUSR2: usize = signal_hook::consts::SIGUSR2 as usize; pub struct Config { plain: bool, + auto_quit: bool, alarm_exec: Option>, } @@ -58,6 +60,7 @@ pub struct Config { fn main() { let mut config = Config { plain: false, + auto_quit: false, alarm_exec: None, }; let mut alarm_roster = AlarmRoster::new(); @@ -147,7 +150,7 @@ fn main() { }, // Delete last alarm on 'd'. Key::Char('d') => { - if alarm_roster.pop().is_some() { + if alarm_roster.drop_last() { // If we remove the last alarm we have to reset "countdown" // manually. It is safe to do it anyway. countdown.reset(); @@ -188,10 +191,8 @@ fn main() { }, // Backspace. Key::Backspace => { - // Delete last char in buffer. It makes no difference to us - // if this succeeds of fails. - let _ = buffer.pop(); - buffer_updated = true; + // Delete last char in buffer. + if buffer.pop().is_some() { buffer_updated = true }; }, Key::Char(c) => { if c.is_ascii_digit() { @@ -249,6 +250,10 @@ fn main() { if config.alarm_exec.is_some() { alarm_exec(&config, clock.elapsed); } + // Quit if configured. + if config.auto_quit && !alarm_roster.active() { + break; + } } // Clear the window and redraw menu bar, alarm roster and buffer if @@ -317,6 +322,7 @@ fn parse_args(config: &mut Config, alarm_roster: &mut AlarmRoster) { std::process::exit(0); }, "-p" | "--plain" => { config.plain = true; }, + "-q" | "--quit" => { config.auto_quit = true; }, "-e" | "--exec" => { // Find position of this flag. let i = env::args().position(|s| { s == "-e" || s == "--exec" }).unwrap();