From d423f75989de95782e0b53b0a7246a9b44e523d7 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Mon, 9 May 2022 22:23:59 -0400 Subject: [PATCH] Add xargs and stdin --- src/errors.rs | 6 ++++++ src/kak.rs | 2 +- src/main.rs | 3 +++ src/sort.rs | 3 +-- src/xargs.rs | 26 ++++++++++++++++---------- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 2b29653..bc76a80 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -18,6 +18,12 @@ impl From for KakMessage { } } +impl From<&str> for KakMessage { + fn from(err: &str) -> Self { + Self(err.to_string(), None) + } +} + impl From for KakMessage { fn from(err: shellwords::MismatchedQuotes) -> Self { Self("Corrupt kak response".to_string(), Some(err.to_string())) diff --git a/src/kak.rs b/src/kak.rs index 3448257..cabfc40 100644 --- a/src/kak.rs +++ b/src/kak.rs @@ -186,7 +186,7 @@ pub fn get_selections_with_desc() -> Result, KakMessage> /// # Errors /// /// Will return `Err` if command fifo could not be opened, read from, or written to -pub fn set_selections<'a, I, S: 'a>(selections: I) -> Result<(), KakMessage> +pub fn set_selections<'a, I, S: 'a + ?Sized>(selections: I) -> Result<(), KakMessage> where I: IntoIterator, S: AsRef + fmt::Display, diff --git a/src/main.rs b/src/main.rs index d33919f..baea35b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ mod kak; mod math_eval; mod shuf; mod sort; +mod stdin; mod trim; mod uniq; mod xargs; @@ -43,6 +44,7 @@ enum Commands { MathEval(math_eval::Options), Trim(trim::Options), Xargs(xargs::Options), + Stdin(stdin::Options), } fn main() { @@ -79,6 +81,7 @@ fn run() -> Result { Commands::MathEval(o) => math_eval::math_eval(o), Commands::Trim(o) => trim::trim(o), Commands::Xargs(o) => xargs::xargs(o), + Commands::Stdin(o) => stdin::stdin(o), } } diff --git a/src/sort.rs b/src/sort.rs index d63d563..257ef01 100644 --- a/src/sort.rs +++ b/src/sort.rs @@ -18,6 +18,7 @@ pub struct Options { reverse: bool, #[clap(short, long)] ignore_case: bool, + // TODO: Sort by character ([xba] => [abx]) } fn invert_bool(s: &str) -> Result { @@ -117,8 +118,6 @@ fn to_sortable_selection<'a, 'b>( } pub fn sort(options: &Options) -> Result { - eprintln!("Got sort options: {:?}", options); - // subselections is Some if the user requests it in subselections_register // It will "exec z" to restore the selections before setting selections // If subselections is None, "exec z" is not called diff --git a/src/xargs.rs b/src/xargs.rs index f31813b..bb08f68 100644 --- a/src/xargs.rs +++ b/src/xargs.rs @@ -8,10 +8,9 @@ pub struct Options { args: Vec, } pub fn xargs(options: &Options) -> Result { - // let mut selections = get_selections()?; - let mut child = Command::new("xargs") .arg("-0") + .arg("--") .args(&options.args) .stdin(Stdio::piped()) .stdout(Stdio::piped()) @@ -21,20 +20,27 @@ pub fn xargs(options: &Options) -> Result { let mut stdin = child.stdin.take().expect("Failed to open stdin"); let handle = std::thread::spawn(move || -> Result<(), KakMessage> { for s in get_selections_with_desc()? { + eprintln!("Got selection {}", s.content); write!(stdin, "{}\0", s.content)?; - // stdin - // .write_all(&.as_bytes()) - // .expect("Failed to write to stdin"); - // stdin.write_all(&[b'\0']).expect("Failed to write to stdin"); } Ok(()) }); - set_selections(BufReader::new(child.stdout.take().expect("Failed to get stdout")).split(b'\0')); + eprintln!("About t oreadvv"); - // stdout. + set_selections( + BufReader::new(child.stdout.take().ok_or("Failed to get stdout")?) + .split(b'\0') + .map(|s| Ok(String::from_utf8_lossy(&s?).into_owned())) + .collect::, KakMessage>>()? + .iter(), + )?; - // set_selections(selections.iter())?; + // Wait for the background process to exit + // TODO: Do not use a string + handle + .join() + .map_err(|_e| "Could not join background process")??; - Ok(KakMessage(format!("Shuf selections",), None)) + Ok("xargs selections".into()) }