feat: Add option to always include cwd prefix
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thayne McCombs · committed by Thayne McCombs · parent 3bc7092 · Browse files · 5 files·+41 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Fixes: #1243
- Fixes: #1331
Diff stats
5 files+41 added−9 removed
- other:+41 added−9 removed
Provenance
- Commit
b1f83a0- Tree
ca19e72- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
Thayne McCombs <astrothayne@gmail.com>- Parent
3bc7092
5 files changed+41 added−9 removed
modified:contrib/completion/_fd+1 added-1 removed
160 unmodified lines161 $no'(--base-directory)--base-directory=[change the current working directory to the given path]:directory:_files -/'162 $no'(*)*--search-path=[set search path (instead of positional <path> arguments)]:directory:_files -/'163 164 + strip-cwd-prefix165- $no'(strip-cwd-prefix exec-cmds)--strip-cwd-prefix[Strip ./ prefix when output is redirected]'165+ $no'(strip-cwd-prefix exec-cmds)--strip-cwd-prefix=[When to strip ./]:when:(always never auto)'166 167 + and168 '--and=[additional required search path]:pattern'169 114 unmodified linesmodified:doc/fd.1+14 added-3 removed
154 unmodified lines155 .B \-\-show-errors156 Enable the display of filesystem errors for situations such as insufficient157 permissions or dead symlinks.158 .TP159-.B \-\-strip-cwd-prefix160-By default, relative paths are prefixed with './' when the output goes to a non interactive terminal161-(TTY). Use this flag to disable this behaviour.159+.B \-\-strip-cwd-prefix [when]160+By default, relative paths are prefixed with './' when -x/--exec,161+-X/--exec-batch, or -0/--print0 are given, to reduce the risk of a162+path starting with '-' being treated as a command line option. Use163+this flag to change this behavior. If this flag is used without a value,164+it is equivalent to passing "always". Possible values are:165+.RS166+.IP auto167+Use the default behavior.168+.IP never169+Never strip the ./ at the beginning of paths170+.IP always171+Always strip the ./ at the beginning of paths172+.RE173 .TP174 .B \-\-one\-file\-system, \-\-mount, \-\-xdev175 By default, fd will traverse the file system tree as far as other options dictate. With this flag, fd ensures that it does not descend into a different file system than the one it started in. Comparable to the -mount or -xdev filters of find(1).176 .TP366 unmodified linesmodified:src/cli.rs+24 added-3 removed
615 unmodified lines616 617 /// By default, relative paths are prefixed with './' when -x/--exec,618 /// -X/--exec-batch, or -0/--print0 are given, to reduce the risk of a619 /// path starting with '-' being treated as a command line option. Use620- /// this flag to disable this behaviour.621- #[arg(long, conflicts_with_all(&["path", "search_path"]), hide_short_help = true, long_help)]622- pub strip_cwd_prefix: bool,620+ /// this flag to change this behavior. If this flag is used without a value,621+ /// it is equivalent to passing "always".622+ #[arg(long, conflicts_with_all(&["path", "search_path"]), value_name = "when", hide_short_help = true, require_equals = true, long_help)]623+ strip_cwd_prefix: Option<Option<StripCwdWhen>>,624 625 /// By default, fd will traverse the file system tree as far as other options626 /// dictate. With this flag, fd ensures that it does not descend into a627 /// different file system than the one it started in. Comparable to the -mount72 unmodified lines700 .filter(|&m| m > 0)701 .or_else(|| self.max_one_result.then_some(1))702 }703 704+ pub fn strip_cwd_prefix<P: FnOnce() -> bool>(&self, auto_pred: P) -> bool {705+ use self::StripCwdWhen::*;706+ self.no_search_paths()707+ && match self.strip_cwd_prefix.map_or(Auto, |o| o.unwrap_or(Always)) {708+ Auto => auto_pred(),709+ Always => true,710+ Never => false,711+ }712+ }713+714 #[cfg(feature = "completions")]715 pub fn gen_completions(&self) -> anyhow::Result<Option<Shell>> {716 self.gen_completions717 .map(|maybe_shell| match maybe_shell {52 unmodified lines770 /// do not use colorized output771 Never,772 }773 774+#[derive(Copy, Clone, PartialEq, Eq, Debug, ValueEnum)]775+pub enum StripCwdWhen {776+ /// Use the default behavior777+ Auto,778+ /// Always strip the ./ at the beginning of paths779+ Always,780+ /// Never strip the ./781+ Never,782+}783+784 // there isn't a derive api for getting grouped values yet,785 // so we have to use hand-rolled parsing for exec and exec-batch786 pub struct Exec {787 pub command: Option<CommandSet>,110 unmodified linesmodified:src/main.rs+1 added-2 removed
309 unmodified lines310 show_filesystem_errors: opts.show_errors,311 path_separator,312 actual_path_separator,313 max_results: opts.max_results(),314- strip_cwd_prefix: (opts.no_search_paths()315- && (opts.strip_cwd_prefix || !(opts.null_separator || has_command))),314+ strip_cwd_prefix: opts.strip_cwd_prefix(|| !(opts.null_separator || has_command)),315 })316 }317 318 fn extract_command(opts: &mut Opts, colored_output: bool) -> Result<Option<CommandSet>> {160 unmodified linesmodified:CHANGELOG.md+1 added
1 # Upcoming release2 3 ## Features4 5+- Allow passing an optional argument to `--strip-cwd-prefix` of "always", "never", or "auto". to force whether the cwd prefix is stripped or not.6 7 ## Bugfixes8 9 760 unmodified lines