Rename stdin to xargs

This commit is contained in:
Austen Adler 2022-10-02 12:48:04 -04:00
parent 801c484d76
commit 3ac74d08af
3 changed files with 27 additions and 79 deletions

View File

@ -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<String, KakError> {
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),

View File

@ -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<String>,
}
pub fn stdin(options: &Options) -> Result<String, KakError> {
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
))
}

View File

@ -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<String>,
}
pub fn xargs(options: &Options) -> Result<String, KakError> {
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::<Result<Vec<_>, 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
))
}