@@ 18,7 18,7 @@ use crate::config::Config;
use log::debug;
use nix::unistd::{getpid, setpgid};
use std::env::VarError;
-use std::io;
+use std::io::{self, ErrorKind};
use std::os::unix::process::CommandExt as _;
use std::process::{Command, Stdio};
use thiserror::Error;
@@ 37,48 37,34 @@ pub enum RunError {
},
#[error("Error getting value of $TERM: {0}")]
TermVar(VarError),
+ #[error("Could not determine what terminal program to use to launch this app, please set `term_cmd` in the config file")]
+ CantFindTerm,
}
-// fn parse_exec(exec: &mut V) -> (String, Vec<&str>) {
-// let mut iter = exec.split(' ');
-// let cmd = iter.next().expect("Empty Exec").to_owned();
-// let args = iter.collect();
-// (cmd, args)
-// }
-
-// fn with_term<'a>(
-// config: &Config,
-// exec: &'a Vec<String>,
-// ) -> Result<(String, &'a Vec<String>), std::env::VarError> {
-// let term = if let Some(term) = &config.term_cmd {
-// term.clone()
-// } else {
-// std::env::var("TERM")?
-// };
-// let mut args: Vec<&str> = exec.split(' ').collect();
-// args.insert(0, "-e");
-// Ok((term, args))
-// }
-
impl App {
/// Run the app.
pub fn run(&self, config: &Config) -> Result<(), RunError> {
debug!("Exec: `{:?}`", self.exec);
let mut command = if self.terminal {
- let term = if let Some(term) = &config.term_cmd {
- term.clone()
+ if let Some(term) = &config.term_cmd {
+ let args: Vec<_> = term.split(' ').collect();
+ let mut cmd = Command::new(args[0]);
+ cmd.args(&args[1..]);
+ cmd.args(&self.exec);
+ cmd
} else {
- std::env::var("TERM").map_err(RunError::TermVar)?
- };
- let mut cmd = Command::new(term);
- cmd.arg("-e");
- cmd.args(&self.exec);
- cmd
+ let term = std::env::var("TERM").map_err(RunError::TermVar)?;
+ let mut cmd = Command::new(term);
+ cmd.arg("-e");
+ cmd.args(&self.exec);
+ cmd
+ }
} else {
let mut cmd = Command::new(&self.exec[0]);
cmd.args(&self.exec[1..]);
cmd
};
+ debug!("Running command: `{:?}`", command);
command
.stdout(Stdio::null())
.stderr(Stdio::null());
@@ 90,10 76,13 @@ impl App {
Ok(())
});
}
- let _child = command.spawn().map_err(|err| RunError::Exec {
- exec: format!("{:?}", &self.exec),
- err,
- })?;
- Ok(())
+ match command.spawn() {
+ Ok(_) => Ok(()),
+ Err(e) if config.term_cmd.is_none() && e.kind() == ErrorKind::NotFound => Err(RunError::CantFindTerm),
+ Err(e) => Err(RunError::Exec {
+ exec: format!("`{:?}`", command),
+ err: e,
+ })
+ }
}
}