| 1 |
use std::process::{Command, Stdio};
|
| 2 |
|
| 3 |
pub fn spawn_detached(cmd: &str) {
|
| 4 |
if let Ok(mut child) = Command::new("sh")
|
| 5 |
.arg("-c")
|
| 6 |
.arg(format!("({}) &", cmd))
|
| 7 |
.stdin(Stdio::null())
|
| 8 |
.stdout(Stdio::null())
|
| 9 |
.stderr(Stdio::null())
|
| 10 |
.spawn()
|
| 11 |
{
|
| 12 |
let _ = child.wait();
|
| 13 |
}
|
| 14 |
}
|
| 15 |
|
| 16 |
pub fn spawn_detached_with_args(program: &str, args: &[&str]) {
|
| 17 |
let escaped_args: Vec<String> = args.iter().map(|a| shell_escape(a)).collect();
|
| 18 |
let full_cmd = if escaped_args.is_empty() {
|
| 19 |
program.to_string()
|
| 20 |
} else {
|
| 21 |
format!("{} {}", program, escaped_args.join(" "))
|
| 22 |
};
|
| 23 |
spawn_detached(&full_cmd)
|
| 24 |
}
|
| 25 |
|
| 26 |
fn shell_escape(s: &str) -> String {
|
| 27 |
if s.contains(|c: char| c.is_whitespace() || c == '\'' || c == '"' || c == '\\') {
|
| 28 |
format!("'{}'", s.replace('\'', "'\\''"))
|
| 29 |
} else {
|
| 30 |
s.to_string()
|
| 31 |
}
|
| 32 |
}
|