From ce5c445fd431f108e73bbe62b0ee73b8418d85c1 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 6 Aug 2023 17:22:53 +0200 Subject: Make the -e flag work as intended --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/main.rs | 26 ++++++++++++++++++-------- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed60ec2..5fc2540 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + [[package]] name = "errno" version = "0.3.1" @@ -59,6 +65,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "lexopt" version = "0.1.0" @@ -82,6 +97,7 @@ name = "mmv" version = "0.1.0" dependencies = [ "cerm", + "itertools", "lexopt", "tempfile", ] diff --git a/Cargo.toml b/Cargo.toml index fac1bb2..262e69a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,6 @@ authors = [ [dependencies] cerm = "1.0.0" +itertools = "0.11.0" lexopt = "0.1.0" tempfile = "3.7.0" diff --git a/src/main.rs b/src/main.rs index bc00355..23a0bf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,14 @@ use std::{ ffi::OsString, fs, hash::{Hash, Hasher}, - io::{self, BufRead, BufReader, BufWriter, Write}, + io::{self, BufRead, BufReader, BufWriter, Read, Write}, iter, path::{Component, Path, PathBuf}, process::{self, Command, Stdio}, }; +use itertools::Itertools; + use { cerm::{err, warn}, tempfile::tempdir, @@ -48,14 +50,22 @@ fn work() -> Result<(), io::Error> { let (cmd, args) = rest.split_first().unwrap_or_else(|| usage(None)); // Collect sources from standard input - let srcs: Vec<_> = io::stdin() - .lines() - .map(|x| match x { - Err(e) => { err!("{e}"); }, - Ok(l) if l.is_empty() => usage(None), - Ok(l) => l, + let srcs = io::stdin() + .bytes() + .map(|x| { + x.unwrap_or_else(|e| { + err!("{e}"); + }) }) - .collect(); + .group_by(|b| *b == (b'\0' + b'\n' * !flags.nul as u8)); + let srcs = srcs + .into_iter() + .filter(|(x, _)| !x) + .map(|(_, x)| String::from_utf8(x.collect_vec())) + .collect::, _>>() + .unwrap_or_else(|e| { + err!("{e}"); + }); // Spawn the child process let mut child = Command::new(cmd) -- cgit v1.2.3