Skip to content

Commit

Permalink
Support multiple --exec instances
Browse files Browse the repository at this point in the history
and `--exec-batch`.

Fixes: sharkdp#406
  • Loading branch information
tmccombs committed Mar 5, 2022
1 parent 0fd7ec5 commit d7a692b
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ once_cell = "1.9.0"

[dependencies.clap]
version = "3.1"
features = ["suggestions", "color", "wrap_help", "cargo"]
features = ["suggestions", "color", "wrap_help", "cargo", "unstable-grouped"]

[target.'cfg(unix)'.dependencies]
users = "0.11.0"
Expand Down
2 changes: 2 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ pub fn build_app() -> Command<'static> {
.long("exec")
.short('x')
.min_values(1)
.multiple_occurrences(true)
.allow_hyphen_values(true)
.value_terminator(";")
.value_name("cmd")
Expand Down Expand Up @@ -417,6 +418,7 @@ pub fn build_app() -> Command<'static> {
.long("exec-batch")
.short('X')
.min_values(1)
.multiple_occurrences(true)
.allow_hyphen_values(true)
.value_terminator(";")
.value_name("cmd")
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{path::PathBuf, sync::Arc, time::Duration};
use lscolors::LsColors;
use regex::bytes::RegexSet;

use crate::exec::CommandTemplate;
use crate::exec::CommandSet;
use crate::filetypes::FileTypes;
#[cfg(unix)]
use crate::filter::OwnerFilter;
Expand Down Expand Up @@ -83,7 +83,7 @@ pub struct Config {
pub extensions: Option<RegexSet>,

/// If a value is supplied, each item found will be used to generate and execute commands.
pub command: Option<Arc<CommandTemplate>>,
pub command: Option<Arc<CommandSet>>,

/// Maximum number of search results to pass to each `command`. If zero, the number is
/// unlimited.
Expand Down
12 changes: 6 additions & 6 deletions src/exec/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::error::print_error;
use crate::exit_codes::{merge_exitcodes, ExitCode};
use crate::walk::WorkerResult;

use super::CommandTemplate;
use super::CommandSet;

/// An event loop that listens for inputs from the `rx` receiver. Each received input will
/// generate a command with the supplied command template. The generated command will then
/// be executed, and this process will continue until the receiver's sender has closed.
pub fn job(
rx: Arc<Mutex<Receiver<WorkerResult>>>,
cmd: Arc<CommandTemplate>,
cmd: Arc<CommandSet>,
out_perm: Arc<Mutex<()>>,
show_filesystem_errors: bool,
buffer_output: bool,
Expand All @@ -39,15 +39,15 @@ pub fn job(
// Drop the lock so that other threads can read from the receiver.
drop(lock);
// Generate a command, execute it and store its exit code.
results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm), buffer_output))
results.push(cmd.execute(&value, Arc::clone(&out_perm), buffer_output))
}
// Returns error in case of any error.
merge_exitcodes(results)
}

pub fn batch(
rx: Receiver<WorkerResult>,
cmd: &CommandTemplate,
cmd: &CommandSet,
show_filesystem_errors: bool,
buffer_output: bool,
limit: usize,
Expand All @@ -63,14 +63,14 @@ pub fn batch(
});
if limit == 0 {
// no limit
return cmd.generate_and_execute_batch(paths, buffer_output);
return cmd.execute_batch(paths, buffer_output);
}

let mut exit_codes = Vec::new();
let mut peekable = paths.peekable();
while peekable.peek().is_some() {
let limited = peekable.by_ref().take(limit);
let exit_code = cmd.generate_and_execute_batch(limited, buffer_output);
let exit_code = cmd.execute_batch(limited, buffer_output);
exit_codes.push(exit_code);
}
merge_exitcodes(exit_codes)
Expand Down
Loading

0 comments on commit d7a692b

Please sign in to comment.