use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use anyhow::{Context, Result, bail};
use super::TabToOpen;
use crate::shells;
pub fn run(program: &Path, args: &[String]) -> Result<()> {
let status = Command::new(program)
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.with_context(|| format!("can't start {}", program.display()))?;
if !status.success() {
bail!("{} failed: {status}", program.display());
}
Ok(())
}
pub fn start(program: &Path, args: &[String]) -> Result<()> {
let mut command = Command::new(program);
command
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
command.process_group(0);
}
command
.spawn()
.with_context(|| format!("can't start {}", program.display()))?;
Ok(())
}
pub fn beside(exe: Option<&Path>, name: &str) -> PathBuf {
exe.and_then(Path::parent)
.map(|folder| folder.join(name))
.filter(|program| program.is_file())
.unwrap_or_else(|| PathBuf::from(name))
}
pub fn command(tab: &TabToOpen) -> Vec<String> {
shells::shell_with_run(tab).unwrap_or_default()
}