prototype spawn yarg command with piped stdin

main
blob42 7 months ago
parent 600f1d5ed3
commit 2cd29789c3

@ -12,7 +12,7 @@ use clap::error::ErrorKind;
use yargs::{DEFAULT_SEP_PATTERN, stdin};
use yargs::parse::InputText;
use anyhow::Result;
use std::io::{BufRead, Read, BufReader, stdin};
use std::io::{BufRead, Read, BufReader, stdin, Write};
use std::process::{self, Command, Stdio};
@ -102,20 +102,35 @@ fn main() -> Result<()> {
eprintln!("======");
}
let columns = input_text.split()?;
// TODO: RESULT
if cli.yargs.is_empty() {
print!("{}", raw_input);
} else {
// Handle yargs
// Exec each yarg as a process for each column
// yarg #1 for column 1, yarg #2 -> col 2 ...
// For each columns of text, execute the arg command on
// the columns lines (xargs)
// we know we have at least one elm
let yarg = cli.yargs.first().unwrap();
let yarg_cmd = Command::new(yarg)
.stdout(Stdio::piped())
// execute the child process (yarg) using the
// piped input
// EXAMPLE: using thread to spawn the processes
// std::thread::spawn(move || {
// stdin.write_all("Hello, world!".as_bytes()).expect("Failed to write to stdin");
// });
let mut yarg_cmd = Command::new(yarg)
.stdin(Stdio::piped())
.spawn()
.expect(&format!("Failed to exec {yarg}"));
let mut yarg_stdin = yarg_cmd.stdin.take().expect("failed to open stdin");
yarg_stdin.write_all(columns[5].join("\n").as_bytes())?;
// println!("{}", String::from_utf8(output.stdout).unwrap());
}

@ -48,6 +48,10 @@ impl<'a> InputText<'a> {
pub fn is_empty(self) -> bool {
self.raw.is_empty()
}
pub fn split(self) -> Result<Columns> {
split_columns(self.raw, &self.sep)
}
}
/// Return the number of columns given input text and a separator
@ -92,7 +96,7 @@ pub fn split_columns(text: &str, sep: &str) -> Result<Columns> {
}
}
eprintln!("{:?}", columns);
// eprintln!("{:?}", columns);
Ok(Columns(columns))
}

Loading…
Cancel
Save