From f1c6eedb215b1141b85d4c0e81540ed4a02bdc23 Mon Sep 17 00:00:00 2001 From: shy Date: Fri, 16 Apr 2021 12:46:02 +0200 Subject: [PATCH] Return child in result. --- src/lib.rs | 20 ++++++++++---------- src/main.rs | 25 ++++++++++++++----------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8a61680..205ee35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,8 +29,7 @@ pub use consts::ui::*; pub fn run( config: Config, mut alarm_roster: AlarmRoster, - spawned: &mut Option, -) -> Result<(), std::io::Error> +) -> Result, std::io::Error> { let mut layout = Layout::new(); // Initialise roster_width. @@ -43,6 +42,7 @@ pub fn run( let mut input_keys = async_stdin.keys(); let stdout = std::io::stdout(); let mut stdout = stdout.lock().into_raw_mode()?; + let mut child: Option = None; let mut force_redraw = true; // Register signals. @@ -121,21 +121,21 @@ pub fn run( // Check on last spawned child process prior to processing the // alarm roster and possibly starting a new one. - if let Some(ref mut child) = spawned { - match child.try_wait() { + if let Some(ref mut spawn) = child { + match spawn.try_wait() { // Process exited successfully. - Ok(Some(status)) if status.success() => *spawned = None, + Ok(Some(status)) if status.success() => child = None, // Abnormal exit. Ok(Some(status)) => { eprintln!("Spawned process terminated with non-zero exit status. ({})", status); - *spawned = None; + child = None; }, // Process is still running. Ok(None) => (), // Other error. Err(error) => { eprintln!("Error executing command. ({})", error); - *spawned = None; + child = None; }, } } @@ -156,8 +156,8 @@ pub fn run( match config.command { // Run command if configured and no command is running. - Some(ref command) if spawned.is_none() => { - *spawned = exec_command(command, alarm.time, &alarm.label); + Some(ref command) if child.is_none() => { + child = exec_command(command, alarm.time, &alarm.label); }, // Last command is still running. Some(_) => eprintln!("Not executing command, as its predecessor is still running"), @@ -345,7 +345,7 @@ pub fn run( cursor::Show)?; stdout.flush()?; - Ok(()) + Ok(child) } pub struct Config { diff --git a/src/main.rs b/src/main.rs index f5048a2..05e2b9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ fn main() { process::exit(1); }); - // Read alarm times from stdin if stdin not a tty. + // Read alarm times from stdin if stdin is not a tty. let stdin = std::io::stdin(); if !termion::is_tty(&stdin) { stdin.lock(); @@ -22,20 +22,23 @@ fn main() { } } - // Holds spawned child process if any. - let mut spawned: Option = None; + // Run main loop. Returns spawned child process if any. + let child = match run(config, alarm_roster) { + Ok(child) => child, + Err(error) => { + eprintln!("Main loop exited with error: {}", error); + process::exit(1); + }, + }; - // Run main loop. - if let Err(e) = run(config, alarm_roster, &mut spawned) { - eprintln!("Main loop exited with error: {}", e); - process::exit(1); - } - - // Wait for remaining spawned processes to exit. - if let Some(ref mut child) = spawned { + // Wait for remaining spawned process to exit. + if let Some(mut child) = child { eprint!("Waiting for spawned process (PID {}) to finish ...", child.id()); match child.wait() { + Ok(status) if status.success() => eprintln!(" ok"), + // Unix only. + Ok(status) if status.code().is_none() => eprintln!(" interrupted ({})", status), Ok(status) => eprintln!(" ok ({})", status), Err(error) => eprintln!(" failed ({})", error), }