Store alarm_exec as Option<T>.

This commit is contained in:
shy 2021-04-05 13:54:02 +02:00
parent c04bde3ba4
commit bde5bc9307
2 changed files with 19 additions and 16 deletions

View file

@ -262,19 +262,20 @@ pub fn alarm_exec(config: &Config, elapsed: u32) {
time = format!("{:02}:{:02}:{:02}", elapsed /3600, (elapsed / 60) % 60, elapsed % 60); time = format!("{:02}:{:02}:{:02}", elapsed /3600, (elapsed / 60) % 60, elapsed % 60);
} }
// Replace every occurrence of "%s". if let Some(exec) = &config.alarm_exec {
for s in config.alarm_exec.iter() { // Replace every occurrence of "%s".
args.push(s.replace("%s", &time)); for s in exec {
} args.push(s.replace("%s", &time));
}
if Command::new( if Command::new(&exec[0])
&config.alarm_exec[0]) .args(&args[1..])
.args(&args[1..]) .stdout(Stdio::null())
.stdout(Stdio::null()) .stdin(Stdio::null())
.stdin(Stdio::null()) .spawn().is_err() {
.spawn().is_err() {
eprintln!("Error: Could not execute command"); eprintln!("Error: Could not execute command");
}
} }
} }

View file

@ -32,14 +32,14 @@ const MENUBAR_SHORT: &str =
pub struct Config { pub struct Config {
plain: bool, plain: bool,
alarm_exec: Vec<String>, alarm_exec: Option<Vec<String>>,
} }
fn main() { fn main() {
let mut config = Config { let mut config = Config {
plain: false, plain: false,
alarm_exec: Vec::new(), alarm_exec: None,
}; };
parse_args(&mut config); parse_args(&mut config);
@ -179,7 +179,8 @@ fn main() {
write!(stdout, "{}", 0x07 as char).unwrap(); write!(stdout, "{}", 0x07 as char).unwrap();
layout.force_redraw = true; 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); alarm_exec(&config, clock.elapsed);
} }
} }
@ -254,10 +255,11 @@ fn parse_args(config: &mut Config) {
// Find position of this flag. // Find position of this flag.
let i = env::args().position(|s| { s == "-e" || s == "--exec" }).unwrap(); let i = env::args().position(|s| { s == "-e" || s == "--exec" }).unwrap();
// Copy everything thereafter. // Copy everything thereafter.
config.alarm_exec = env::args().skip(i + 1).collect(); let exec: Vec<String> = env::args().skip(i + 1).collect();
if config.alarm_exec.len() == 0 { if exec.len() == 0 {
usage(); usage();
} else { } else {
config.alarm_exec = Some(exec);
// Ignore everything after this flag. // Ignore everything after this flag.
break; break;
} }