From 2cd29789c3503579da42c6d7289d295b2f6971a4 Mon Sep 17 00:00:00 2001 From: blob42 Date: Thu, 12 Oct 2023 20:42:18 +0200 Subject: [PATCH] prototype spawn yarg command with piped stdin --- src/main.rs | 25 ++++++++++++++++++++----- src/parse.rs | 6 +++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 22f1510..33d1eb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); } diff --git a/src/parse.rs b/src/parse.rs index 92c072e..679a5f8 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -48,6 +48,10 @@ impl<'a> InputText<'a> { pub fn is_empty(self) -> bool { self.raw.is_empty() } + + pub fn split(self) -> Result { + 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 { } } - eprintln!("{:?}", columns); + // eprintln!("{:?}", columns); Ok(Columns(columns)) }