yazi/core/src/external/shell.rs
2023-08-12 17:48:18 +08:00

41 lines
895 B
Rust

use std::{ffi::OsString, process::Stdio};
use anyhow::Result;
use tokio::process::{Child, Command};
pub struct ShellOpt {
pub cmd: OsString,
pub args: Vec<OsString>,
pub piped: bool,
}
pub fn shell(opt: ShellOpt) -> Result<Child> {
#[cfg(not(target_os = "windows"))]
{
Ok(
Command::new("sh")
.arg("-c")
.arg(opt.cmd)
.arg("") // $0 is the command name
.args(opt.args)
.stdout(if opt.piped { Stdio::piped() } else { Stdio::inherit() })
.stderr(if opt.piped { Stdio::piped() } else { Stdio::inherit() })
.kill_on_drop(true)
.spawn()?,
)
}
#[cfg(target_os = "windows")]
{
Ok(
Command::new("cmd")
.arg("/C")
.arg(opt.cmd)
.args(opt.args)
.stdout(if opt.piped { Stdio::piped() } else { Stdio::inherit() })
.stderr(if opt.piped { Stdio::piped() } else { Stdio::inherit() })
.kill_on_drop(true)
.spawn()?,
)
}
}