Rename stdin to xargs
This commit is contained in:
parent
801c484d76
commit
3ac74d08af
12
src/main.rs
12
src/main.rs
@ -20,12 +20,11 @@ mod pad;
|
|||||||
mod set;
|
mod set;
|
||||||
mod shuf;
|
mod shuf;
|
||||||
mod sort;
|
mod sort;
|
||||||
mod stdin;
|
|
||||||
mod trim;
|
mod trim;
|
||||||
mod uniq;
|
mod uniq;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
mod xargs;
|
||||||
mod xlookup;
|
mod xlookup;
|
||||||
// mod xargs;
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use kakplugin::{display_message, get_var, KakError};
|
use kakplugin::{display_message, get_var, KakError};
|
||||||
use std::env;
|
use std::env;
|
||||||
@ -62,10 +61,8 @@ enum Commands {
|
|||||||
Trim(trim::Options),
|
Trim(trim::Options),
|
||||||
#[clap(about = "Perform set operations on selections")]
|
#[clap(about = "Perform set operations on selections")]
|
||||||
Set(set::Options),
|
Set(set::Options),
|
||||||
// #[clap(about = "")]
|
#[clap(about = "Pass each selection null terminated to a command", visible_aliases = &["stdin"])]
|
||||||
// Xargs(xargs::Options),
|
Xargs(xargs::Options),
|
||||||
#[clap(about = "Pass each selection null terminated to a command")]
|
|
||||||
Stdin(stdin::Options),
|
|
||||||
#[clap(about = "Make boxes out of selections", visible_aliases = &["square"])]
|
#[clap(about = "Make boxes out of selections", visible_aliases = &["square"])]
|
||||||
Box_(box_::Options),
|
Box_(box_::Options),
|
||||||
#[clap(about = "Map selections based on a register", visible_aliases = &["vlookup"])]
|
#[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::Pad(o) => pad::pad(o),
|
||||||
Commands::Trim(o) => trim::trim(o),
|
Commands::Trim(o) => trim::trim(o),
|
||||||
Commands::Set(o) => set::set(o),
|
Commands::Set(o) => set::set(o),
|
||||||
// Commands::Xargs(o) => xargs::xargs(o),
|
Commands::Xargs(o) => xargs::xargs(o),
|
||||||
Commands::Stdin(o) => stdin::stdin(o),
|
|
||||||
Commands::Box_(o) => box_::box_(o),
|
Commands::Box_(o) => box_::box_(o),
|
||||||
Commands::Xlookup(o) => xlookup::xlookup(o),
|
Commands::Xlookup(o) => xlookup::xlookup(o),
|
||||||
Commands::Incr(o) => incr::incr(o, true),
|
Commands::Incr(o) => incr::incr(o, true),
|
||||||
|
50
src/stdin.rs
50
src/stdin.rs
@ -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
|
|
||||||
))
|
|
||||||
}
|
|
44
src/xargs.rs
44
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::{
|
use std::{
|
||||||
|
borrow::Cow,
|
||||||
io::{BufRead, BufReader, Write},
|
io::{BufRead, BufReader, Write},
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
};
|
};
|
||||||
#[derive(clap::StructOpt, Debug)]
|
#[derive(clap::StructOpt, Debug)]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
|
#[clap()]
|
||||||
|
command: String,
|
||||||
|
#[clap(allow_hyphen_values = true)]
|
||||||
args: Vec<String>,
|
args: Vec<String>,
|
||||||
}
|
}
|
||||||
pub fn xargs(options: &Options) -> Result<String, KakError> {
|
pub fn xargs(options: &Options) -> Result<String, KakError> {
|
||||||
let mut child = Command::new("xargs")
|
let mut child = Command::new(&options.command)
|
||||||
.arg("-0")
|
|
||||||
.arg("--")
|
|
||||||
.args(&options.args)
|
.args(&options.args)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("Failed to spawn child process");
|
.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> {
|
let handle = std::thread::spawn(move || -> Result<(), KakError> {
|
||||||
for s in get_selections_with_desc()? {
|
for s in get_selections_with_desc(None)? {
|
||||||
write!(stdin, "{}\0", s.content)?;
|
write!(child_stdin, "{}\0", s.content)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
|
|
||||||
set_selections(
|
let set_selections_result = set_selections_failable(
|
||||||
BufReader::new(
|
BufReader::new(child.stdout.take().expect("Failed to get stdout"))
|
||||||
child
|
.split(b'\0')
|
||||||
.stdout
|
// TODO: Support non-utf8?
|
||||||
.take()
|
.map(|s| -> Result<_, KakError> { Ok(String::from_utf8(s?)?) }),
|
||||||
.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(),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
// Wait for the background process to exit
|
// Wait for the background process to exit
|
||||||
// TODO: Do not use a string
|
// Return its error (if there is one) first
|
||||||
handle
|
handle
|
||||||
.join()
|
.join()
|
||||||
.map_err(|_e| KakError::Custom("Could not join background process".to_string()))??;
|
.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
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user