56 lines
1.4 KiB
Rust
56 lines
1.4 KiB
Rust
|
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) {
|
||
|
let mut child = Command::new("jq")
|
||
|
.arg("-C")
|
||
|
.arg(&app.cmdline)
|
||
|
.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),
|
||
|
}
|
||
|
}
|