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

View file

@ -32,14 +32,14 @@ const MENUBAR_SHORT: &str =
pub struct Config {
plain: bool,
alarm_exec: Vec<String>,
alarm_exec: Option<Vec<String>>,
}
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<String> = env::args().skip(i + 1).collect();
if exec.len() == 0 {
usage();
} else {
config.alarm_exec = Some(exec);
// Ignore everything after this flag.
break;
}