diff --git a/src/main.rs b/src/main.rs index 411b0f5..2563007 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,12 +20,11 @@ mod pad; mod set; mod shuf; mod sort; -mod stdin; mod trim; mod uniq; mod utils; +mod xargs; mod xlookup; -// mod xargs; use clap::{Parser, Subcommand}; use kakplugin::{display_message, get_var, KakError}; use std::env; @@ -62,10 +61,8 @@ enum Commands { Trim(trim::Options), #[clap(about = "Perform set operations on selections")] Set(set::Options), - // #[clap(about = "")] - // Xargs(xargs::Options), - #[clap(about = "Pass each selection null terminated to a command")] - Stdin(stdin::Options), + #[clap(about = "Pass each selection null terminated to a command", visible_aliases = &["stdin"])] + Xargs(xargs::Options), #[clap(about = "Make boxes out of selections", visible_aliases = &["square"])] Box_(box_::Options), #[clap(about = "Map selections based on a register", visible_aliases = &["vlookup"])] @@ -119,8 +116,7 @@ fn run() -> Result { Commands::Pad(o) => pad::pad(o), Commands::Trim(o) => trim::trim(o), Commands::Set(o) => set::set(o), - // Commands::Xargs(o) => xargs::xargs(o), - Commands::Stdin(o) => stdin::stdin(o), + Commands::Xargs(o) => xargs::xargs(o), Commands::Box_(o) => box_::box_(o), Commands::Xlookup(o) => xlookup::xlookup(o), Commands::Incr(o) => incr::incr(o, true), diff --git a/src/stdin.rs b/src/stdin.rs deleted file mode 100644 index 0248a6e..0000000 --- a/src/stdin.rs +++ /dev/null @@ -1,50 +0,0 @@ -use kakplugin::{get_selections_with_desc, set_selections_failable, KakError}; -use std::{ - borrow::Cow, - io::{BufRead, BufReader, Write}, - process::{Command, Stdio}, -}; -#[derive(clap::StructOpt, Debug)] -pub struct Options { - #[clap()] - command: String, - #[clap(allow_hyphen_values = true)] - args: Vec, -} -pub fn stdin(options: &Options) -> Result { - let mut child = Command::new(&options.command) - .args(&options.args) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .expect("Failed to spawn child process"); - - let mut child_stdin = child.stdin.take().expect("Failed to open stdin"); - let handle = std::thread::spawn(move || -> Result<(), KakError> { - for s in get_selections_with_desc(None)? { - write!(child_stdin, "{}\0", s.content)?; - } - Ok(()) - }); - - let set_selections_result = set_selections_failable( - BufReader::new(child.stdout.take().expect("Failed to get stdout")) - .split(b'\0') - // TODO: Support non-utf8? - .map(|s| -> Result<_, KakError> { Ok(String::from_utf8(s?)?) }), - ); - - // Wait for the background process to exit - // Return its error (if there is one) first - handle - .join() - .map_err(|_e| KakError::Custom("Could not join background process".to_string()))??; - - // Now print any errors - let num_set = set_selections_result?; - - Ok(format!( - "Set {} selections from {}", - num_set, options.command - )) -} diff --git a/src/xargs.rs b/src/xargs.rs index fb3be44..c2af9a1 100644 --- a/src/xargs.rs +++ b/src/xargs.rs @@ -1,48 +1,50 @@ -use kakplugin::{get_selections_with_desc, set_selections, KakError}; +use kakplugin::{get_selections_with_desc, set_selections_failable, KakError}; use std::{ + borrow::Cow, io::{BufRead, BufReader, Write}, process::{Command, Stdio}, }; #[derive(clap::StructOpt, Debug)] pub struct Options { + #[clap()] + command: String, + #[clap(allow_hyphen_values = true)] args: Vec, } pub fn xargs(options: &Options) -> Result { - let mut child = Command::new("xargs") - .arg("-0") - .arg("--") + let mut child = Command::new(&options.command) .args(&options.args) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() .expect("Failed to spawn child process"); - let mut stdin = child.stdin.take().expect("Failed to open stdin"); + let mut child_stdin = child.stdin.take().expect("Failed to open stdin"); let handle = std::thread::spawn(move || -> Result<(), KakError> { - for s in get_selections_with_desc()? { - write!(stdin, "{}\0", s.content)?; + for s in get_selections_with_desc(None)? { + write!(child_stdin, "{}\0", s.content)?; } Ok(()) }); - set_selections( - BufReader::new( - child - .stdout - .take() - .ok_or(KakError::Custom("Failed to get stdout".to_string()))?, - ) - .split(b'\0') - .map(|s| Ok(String::from_utf8_lossy(&s?).into_owned())) - .collect::, KakError>>()? - .iter(), - )?; + let set_selections_result = set_selections_failable( + BufReader::new(child.stdout.take().expect("Failed to get stdout")) + .split(b'\0') + // TODO: Support non-utf8? + .map(|s| -> Result<_, KakError> { Ok(String::from_utf8(s?)?) }), + ); // Wait for the background process to exit - // TODO: Do not use a string + // Return its error (if there is one) first handle .join() .map_err(|_e| KakError::Custom("Could not join background process".to_string()))??; - Ok("xargs selections".into()) + // Now print any errors + let num_set = set_selections_result?; + + Ok(format!( + "Set {} selections from {}", + num_set, options.command + )) }