prototype spawn yarg command with piped stdin

main
blob42 8 months ago
parent 600f1d5ed3
commit 2cd29789c3

@ -12,7 +12,7 @@ use clap::error::ErrorKind;
use yargs::{DEFAULT_SEP_PATTERN, stdin}; use yargs::{DEFAULT_SEP_PATTERN, stdin};
use yargs::parse::InputText; use yargs::parse::InputText;
use anyhow::Result; use anyhow::Result;
use std::io::{BufRead, Read, BufReader, stdin}; use std::io::{BufRead, Read, BufReader, stdin, Write};
use std::process::{self, Command, Stdio}; use std::process::{self, Command, Stdio};
@ -102,20 +102,35 @@ fn main() -> Result<()> {
eprintln!("======"); eprintln!("======");
} }
let columns = input_text.split()?;
// TODO: RESULT // TODO: RESULT
if cli.yargs.is_empty() { if cli.yargs.is_empty() {
print!("{}", raw_input); print!("{}", raw_input);
} else { } else {
// Handle yargs // Handle yargs
// Exec each yarg as a process for each column // For each columns of text, execute the arg command on
// yarg #1 for column 1, yarg #2 -> col 2 ... // the columns lines (xargs)
// we know we have at least one elm // we know we have at least one elm
let yarg = cli.yargs.first().unwrap(); let yarg = cli.yargs.first().unwrap();
let yarg_cmd = Command::new(yarg) // execute the child process (yarg) using the
.stdout(Stdio::piped()) // 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() .spawn()
.expect(&format!("Failed to exec {yarg}")); .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 { pub fn is_empty(self) -> bool {
self.raw.is_empty() 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 /// 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)) Ok(Columns(columns))
} }

Loading…
Cancel
Save