use std::{
fmt::Write,
io::IsTerminal,
process::Command,
};
use crate::pls_command::PlsCommand;
use eyre::Result;
use minus::{page_all, Pager};
#[cfg(target_os = "windows")]
pub fn run_command(command: &str, _su: bool, _pager: &Option<String>, pls_command: &PlsCommand) -> Result<i32> {
let mut cmd = Command::new("cmd");
cmd.args(["/C", command]);
let status = if pls_command.support_pager() && std::io::stdin().is_terminal() {
let output = cmd.output()?;
let output_str = String::from_utf8(output.stdout)?;
let mut pager = Pager::new();
pager.set_prompt(command)?;
pager.write_str(&output_str)?;
page_all(pager)?;
output.status
} else {
cmd.status()?
};
Ok(status.code().unwrap_or_default())
}
#[cfg(not(target_os = "windows"))]
pub fn run_command(command: &str, su: bool, pager: &Option<String>, pls_command: &PlsCommand) -> Result<i32> {
let mut cmd = if su {
let mut cmd = Command::new("sudo");
cmd.args(command.split(" "));
cmd
} else {
let mut cmd = Command::new("sh");
cmd.args(["-c", &command]);
cmd
};
let status = if pls_command.support_pager() && pager.is_none() && std::io::stdin().is_terminal() {
let output = cmd.output()?;
let output_str = String::from_utf8(output.stdout)?;
let mut pager = Pager::new();
pager.set_prompt(command)?;
pager.write_str(&output_str)?;
page_all(pager)?;
output.status
} else {
cmd.status()?
};
Ok(status.code().unwrap_or_default())
}