#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "macos")))]
use std::process as path_std_process;
use std::process::Command;
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
use crate::pty_stdio::PtyStdio;
pub(crate) struct ShellProcess {
pub(crate) child: std::process::Child,
pub(crate) stdout: Option<ShellStdout>,
pub(crate) stderr: Option<ShellStderr>,
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
pub(crate) output_users: Option<[std::fs::File; 2]>,
}
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
pub(crate) type ShellStdout = std::fs::File;
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
pub(crate) type ShellStderr = std::fs::File;
#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "macos")))]
pub(crate) type ShellStdout = std::process::ChildStdout;
#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "macos")))]
pub(crate) type ShellStderr = std::process::ChildStderr;
impl ShellProcess {
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
pub(crate) fn spawn(command: &mut Command) -> std::io::Result<Self> {
let ptys = PtyStdio::attach(command)?;
let child = command.spawn()?;
Ok(Self {
child,
stdout: Some(ptys.stdout_controller),
stderr: Some(ptys.stderr_controller),
output_users: Some(ptys.output_users),
})
}
#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "macos")))]
pub(crate) fn spawn(command: &mut Command) -> std::io::Result<Self> {
command
.stdin(path_std_process::Stdio::null())
.stdout(path_std_process::Stdio::piped())
.stderr(path_std_process::Stdio::piped());
let mut child = command.spawn()?;
let stdout = child.stdout.take();
let stderr = child.stderr.take();
Ok(Self {
child,
stdout,
stderr,
})
}
}