Implement `--batch-size` (#866)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Devon Hollowood · committed by GitHub · parent 7b5b3ec · Browse files · 9 files·+97 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+97 added−2 removed
- tests:+42 added−0 removed
- other:+55 added−2 removed
Provenance
- Commit
17dd2a6- Tree
bce0278- Author
Devon Hollowood <devonhollowood@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
7b5b3ec
9 files changed+97 added−2 removed
modified:contrib/completion/_fd+1 added
136 unmodified lines137 138 + '(exec-cmds)' # execute command139 '(long-listing max-results)'{-x+,--exec=}'[execute command for each search result]:command: _command_names -e:*\;::program arguments: _normal'140 '(long-listing max-results)'{-X+,--exec-batch=}'[execute command for all search results at once]:command: _command_names -e:*\;::program arguments: _normal'141+ '(long-listing max-results)'{--batch-size=}'[max number of args for each -X call]:size'142 143 + other144 '!(--max-buffer-time)--max-buffer-time=[set amount of time to buffer before showing output]:time (ms)'145 124 unmodified linesmodified:doc/fd.1+6 added
403 unmodified lines404 $ fd -e py405 .TP406 .RI "Open all search results with vim:"407 $ fd pattern -X vim408+.TP409+.BI "\-\-batch\-size " size410+Pass at most411+.I size412+arguments to each call to the command given with -X.413+.TP414 .SH SEE ALSO415 .BR find (1)modified:src/exec/job.rs+14 added-1 removed
48 unmodified lines49 rx: Receiver<WorkerResult>,50 cmd: &CommandTemplate,51 show_filesystem_errors: bool,52 buffer_output: bool,53+ limit: usize,54 ) -> ExitCode {55 let paths = rx.iter().filter_map(|value| match value {56 WorkerResult::Entry(val) => Some(val),57 WorkerResult::Error(err) => {2 unmodified lines60 }61 None62 }63 });63- cmd.generate_and_execute_batch(paths, buffer_output)64+ if limit == 0 {65+ // no limit66+ return cmd.generate_and_execute_batch(paths, buffer_output);67+ }68+69+ let mut exit_codes = Vec::new();70+ let mut peekable = paths.peekable();71+ while peekable.peek().is_some() {72+ let limited = peekable.by_ref().take(limit);73+ let exit_code = cmd.generate_and_execute_batch(limited, buffer_output);74+ exit_codes.push(exit_code);75+ }76+ merge_exitcodes(exit_codes)77 }modified:src/app.rs+15 added
364 unmodified lines365 "366 ),367 )368 .arg(369+ Arg::with_name("batch-size")370+ .long("batch-size")371+ .takes_value(true)372+ .value_name("size")373+ .hidden_short_help(true)374+ .requires("exec-batch")375+ .help("Max number of arguments to run as a batch with -X")376+ .long_help(377+ "Maximum number of arguments to pass to the command given with -X. \378+ If the number of results is greater than the given size, \379+ the command given with -X is run again with remaining arguments. \380+ A batch size of zero means there is no limit.",381+ ),382+ )383+ .arg(384 Arg::with_name("exclude")385 .long("exclude")386 .short("E")387 .takes_value(true)288 unmodified linesmodified:src/config.rs+4 added
83 unmodified lines84 85 /// If a value is supplied, each item found will be used to generate and execute commands.86 pub command: Option<Arc<CommandTemplate>>,87 88+ /// Maximum number of search results to pass to each `command`. If zero, the number is89+ /// unlimited.90+ pub batch_size: usize,91+92 /// A list of glob patterns that should be excluded from the search.93 pub exclude_patterns: Vec<String>,94 95 /// A list of custom ignore files.21 unmodified linesmodified:src/main.rs+6 added
346 unmodified lines347 .build()348 })349 .transpose()?,350 command: command.map(Arc::new),351+ batch_size: matches352+ .value_of("batch-size")353+ .map(|n| n.parse::<usize>())354+ .transpose()355+ .context("Failed to parse --batch-size argument")?356+ .unwrap_or_default(),357 exclude_patterns: matches358 .values_of("exclude")359 .map(|v| v.map(|p| String::from("!") + p).collect())360 .unwrap_or_else(Vec::new),206 unmodified linesmodified:src/walk.rs+7 added-1 removed
177 unmodified lines178 thread::spawn(move || {179 // This will be set to `Some` if the `--exec` argument was supplied.180 if let Some(ref cmd) = config.command {181 if cmd.in_batch_mode() {182- exec::batch(rx, cmd, show_filesystem_errors, enable_output_buffering)182+ exec::batch(183+ rx,184+ cmd,185+ show_filesystem_errors,186+ enable_output_buffering,187+ config.batch_size,188+ )189 } else {190 let shared_rx = Arc::new(Mutex::new(rx));191 192 let out_perm = Arc::new(Mutex::new(()));333 unmodified linesmodified:tests/tests.rs+42 added
1416 unmodified lines1417 );1418 }1419 }1420 1421+#[test]1422+fn test_exec_batch_with_limit() {1423+ // TODO Test for windows1424+ if cfg!(windows) {1425+ return;1426+ }1427+1428+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1429+1430+ te.assert_output(1431+ &["foo", "--batch-size", "0", "--exec-batch", "echo", "{}"],1432+ "a.foo one/b.foo one/two/C.Foo2 one/two/c.foo one/two/three/d.foo one/two/three/directory_foo",1433+ );1434+1435+ let output = te.assert_success_and_get_output(1436+ ".",1437+ &["foo", "--batch-size=2", "--exec-batch", "echo", "{}"],1438+ );1439+ let stdout = String::from_utf8_lossy(&output.stdout);1440+1441+ for line in stdout.lines() {1442+ assert_eq!(2, line.split_whitespace().count());1443+ }1444+1445+ let mut paths: Vec<_> = stdout1446+ .lines()1447+ .flat_map(|line| line.split_whitespace())1448+ .collect();1449+ paths.sort_unstable();1450+ assert_eq!(1451+ &paths,1452+ &[1453+ "a.foo",1454+ "one/b.foo",1455+ "one/two/C.Foo2",1456+ "one/two/c.foo",1457+ "one/two/three/d.foo",1458+ "one/two/three/directory_foo"1459+ ],1460+ );1461+}1462+1463 /// Shell script execution (--exec) with a custom --path-separator1464 #[test]1465 fn test_exec_with_separator() {1466 let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);479 unmodified linesmodified:CHANGELOG.md+2 added
10 unmodified lines11 - Add new `-q, --quiet` flag, see #303 (@Asha20)12 13 - Add new `--no-ignore-parent` flag, see #787 (@will459)14 15+- Add new `--batch-size` flag, see #410 (@devonhollowood)16+17 ## Bugfixes18 19 - Set default path separator to `/` in MSYS, see #537 and #730 (@aswild)20 - fd cannot search files under a RAM disk, see #752519 unmodified lines