live-cli/src/command.rs

69 lines
1.8 KiB
Rust
Raw Normal View History

2022-12-15 23:19:17 -05:00
use std::{
io::Write,
process::{Command, Stdio},
};
use tui::widgets::Paragraph;
use crate::App;
pub struct CommandResult<'a> {
pub status_success: bool,
pub stdout: Paragraph<'a>,
pub stderr: Paragraph<'a>,
}
impl Default for CommandResult<'_> {
fn default() -> Self {
Self {
status_success: true,
stdout: Paragraph::new(""),
stderr: Paragraph::new(""),
}
}
}
pub fn run(app: &mut App) {
2022-12-16 20:41:16 -05:00
let args = match shellwords::split(&app.cmdline) {
Ok(a) => a,
Err(e) => {
app.command_result = CommandResult {
status_success: false,
stdout: Paragraph::new(""),
stderr: Paragraph::new(format!("Argument error: {e:?}")),
};
return;
}
};
let mut child = Command::new(&app.hidden_command)
.args(&app.hidden_options)
// .arg(&app.cmdline)
.args(args)
2022-12-15 23:19:17 -05:00
.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");
let stderr_string = ansi4tui::bytes_to_text(output.stderr);
let stdout_string = ansi4tui::bytes_to_text(output.stdout);
app.command_result = CommandResult {
status_success: output.status.success(),
stdout: Paragraph::new(stdout_string),
stderr: Paragraph::new(stderr_string),
}
}