From bde5bc9307dd555ac7c4b7f4926166ab1e7f28b2 Mon Sep 17 00:00:00 2001 From: shy Date: Mon, 5 Apr 2021 13:54:02 +0200 Subject: [PATCH] Store alarm_exec as Option. --- src/alarm.rs | 23 ++++++++++++----------- src/main.rs | 12 +++++++----- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/alarm.rs b/src/alarm.rs index 073d25d..a64b20b 100644 --- a/src/alarm.rs +++ b/src/alarm.rs @@ -262,19 +262,20 @@ pub fn alarm_exec(config: &Config, elapsed: u32) { time = format!("{:02}:{:02}:{:02}", elapsed /3600, (elapsed / 60) % 60, elapsed % 60); } - // Replace every occurrence of "%s". - for s in config.alarm_exec.iter() { - args.push(s.replace("%s", &time)); - } + if let Some(exec) = &config.alarm_exec { + // Replace every occurrence of "%s". + for s in exec { + args.push(s.replace("%s", &time)); + } - if Command::new( - &config.alarm_exec[0]) - .args(&args[1..]) - .stdout(Stdio::null()) - .stdin(Stdio::null()) - .spawn().is_err() { + if Command::new(&exec[0]) + .args(&args[1..]) + .stdout(Stdio::null()) + .stdin(Stdio::null()) + .spawn().is_err() { - eprintln!("Error: Could not execute command"); + eprintln!("Error: Could not execute command"); + } } } diff --git a/src/main.rs b/src/main.rs index 76643d6..dbdc974 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,14 +32,14 @@ const MENUBAR_SHORT: &str = pub struct Config { plain: bool, - alarm_exec: Vec, + alarm_exec: Option>, } fn main() { let mut config = Config { plain: false, - alarm_exec: Vec::new(), + alarm_exec: None, }; parse_args(&mut config); @@ -179,7 +179,8 @@ fn main() { write!(stdout, "{}", 0x07 as char).unwrap(); layout.force_redraw = true; - if config.alarm_exec.len() > 0 { + // Run command if configured. + if config.alarm_exec.is_some() { alarm_exec(&config, clock.elapsed); } } @@ -254,10 +255,11 @@ fn parse_args(config: &mut Config) { // Find position of this flag. let i = env::args().position(|s| { s == "-e" || s == "--exec" }).unwrap(); // Copy everything thereafter. - config.alarm_exec = env::args().skip(i + 1).collect(); - if config.alarm_exec.len() == 0 { + let exec: Vec = env::args().skip(i + 1).collect(); + if exec.len() == 0 { usage(); } else { + config.alarm_exec = Some(exec); // Ignore everything after this flag. break; }