Add xargs and stdin

This commit is contained in:
Austen Adler 2022-05-09 22:23:59 -04:00
parent 8f3e1f5dc1
commit d423f75989
5 changed files with 27 additions and 13 deletions

View File

@ -18,6 +18,12 @@ impl From<String> for KakMessage {
}
}
impl From<&str> for KakMessage {
fn from(err: &str) -> Self {
Self(err.to_string(), None)
}
}
impl From<shellwords::MismatchedQuotes> for KakMessage {
fn from(err: shellwords::MismatchedQuotes) -> Self {
Self("Corrupt kak response".to_string(), Some(err.to_string()))

View File

@ -186,7 +186,7 @@ pub fn get_selections_with_desc() -> Result<Vec<SelectionWithDesc>, 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<Item = &'a S>,
S: AsRef<str> + fmt::Display,

View File

@ -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<KakMessage, KakMessage> {
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),
}
}

View File

@ -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<bool, &'static str> {
@ -117,8 +118,6 @@ fn to_sortable_selection<'a, 'b>(
}
pub fn sort(options: &Options) -> Result<KakMessage, KakMessage> {
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

View File

@ -8,10 +8,9 @@ pub struct Options {
args: Vec<String>,
}
pub fn xargs(options: &Options) -> Result<KakMessage, KakMessage> {
// 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<KakMessage, KakMessage> {
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::<Result<Vec<_>, 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())
}