diff options
author | Canop <cano.petrole@gmail.com> | 2018-11-26 17:49:28 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2018-11-26 17:49:28 +0100 |
commit | 10870c93ec391410dd3c55d03a63993b00223282 (patch) | |
tree | 820d4aa6a5e1c6b8d07335cb38f48c08a242e04c /src/external.rs | |
parent | c2e5f189eea3ea0442aaf0a63215b68fc649b8b0 (diff) |
clean way to start programs on user commands
Diffstat (limited to 'src/external.rs')
-rw-r--r-- | src/external.rs | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/src/external.rs b/src/external.rs index 76ba479..0a17bd9 100644 --- a/src/external.rs +++ b/src/external.rs @@ -1,23 +1,47 @@ +use std::io; use std::process::Command; use std::path::{PathBuf}; +use std::thread; +use std::time; -pub fn execute(exec: &str) { - let mut tokens = exec.split_whitespace(); - match tokens.next() { - Some(exe) => { - Command::new(exe).args(tokens).spawn().expect("failed to start external exectutable"); - }, - None => { - // FIXME panic? - }, - } +// description of a possible launch of an external program +// (might be more complex, and a sequence of things to try, in the future) +#[derive(Debug)] +pub struct Launchable { + exe: String, + args: Vec<String>, } -pub fn open_file(path: &PathBuf) { - Command::new("xdg-open") - .arg(String::from(path.to_string_lossy())) - .spawn() - .expect("xdg-open failed to start"); +impl Launchable { + pub fn opener(path: &PathBuf) -> io::Result<Launchable> { + Launchable::from(&format!("xdg-open {}", &path.to_string_lossy())) + } + pub fn from(launch_string: &str) -> io::Result<Launchable> { + let mut tokens = launch_string + .split_whitespace() + .map(|t| t.to_string()); + match tokens.next() { + Some(exe) => { + Ok(Launchable{ + exe: exe, + args: tokens.collect() + }) + }, + None => { + Err(io::Error::new(io::ErrorKind::Other, "Invalid launch string")) // can this really happen? + }, + } + } + // execute the external program + // WARNING: this may kill the current program. Caller must + // ensure everything's clean before + pub fn execute(&self) -> io::Result<()> { + Command::new(&self.exe) + .args(self.args.iter()) + .spawn()? + .wait()?; + Ok(()) + } } |