use std::{ io::Write, process::{Command, Stdio}, }; use crate::App; pub struct CommandResult { pub status_success: bool, pub stdout: Vec, pub stderr: Vec, } impl Default for CommandResult { fn default() -> Self { Self { status_success: true, stdout: vec![], stderr: vec![], } } } pub fn run(app: &mut App) { let args = match shellwords::split(&app.cmdline) { Ok(a) => a, Err(e) => { app.command_result = CommandResult { status_success: false, stdout: vec![], stderr: format!("Argument error: {e:?}").as_bytes().to_vec(), }; return; } }; let mut child = Command::new(&app.hidden_command) .args(&app.hidden_options) // .arg(&app.cmdline) .args(args) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .expect("Could not spawn child process"); let mut stdin = child.stdin.take().expect("Could not take stdin"); let text_orig_clone = app.text_orig.clone(); std::thread::spawn(move || { stdin .write_all(text_orig_clone.as_bytes()) .expect("Failed to write to stdin"); }); // Collect the output let output = child.wait_with_output().expect("Failed to read stdout"); app.command_result = CommandResult { status_success: output.status.success(), stdout: output.stdout, stderr: output.stderr, } }