From b1f83a0bb070e3fe96139706f759c62bf9e45c4b Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Sat, 27 Apr 2024 22:10:55 -0600 Subject: [PATCH 1/2] feat: Add option to always include cwd prefix Fixes: #1243 Fixes: #1331 --- CHANGELOG.md | 1 + contrib/completion/_fd | 2 +- doc/fd.1 | 17 ++++++++++++++--- src/cli.rs | 27 ++++++++++++++++++++++++--- src/main.rs | 3 +-- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fc85a5..55abe28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Features +- Allow passing an optional argument to `--strip-cwd-prefix` of "always", "never", or "auto". to force whether the cwd prefix is stripped or not. ## Bugfixes diff --git a/contrib/completion/_fd b/contrib/completion/_fd index 28826a9..dc7e94d 100644 --- a/contrib/completion/_fd +++ b/contrib/completion/_fd @@ -162,7 +162,7 @@ _fd() { $no'(*)*--search-path=[set search path (instead of positional arguments)]:directory:_files -/' + strip-cwd-prefix - $no'(strip-cwd-prefix exec-cmds)--strip-cwd-prefix[Strip ./ prefix when output is redirected]' + $no'(strip-cwd-prefix exec-cmds)--strip-cwd-prefix=[When to strip ./]:when:(always never auto)' + and '--and=[additional required search path]:pattern' diff --git a/doc/fd.1 b/doc/fd.1 index 498981f..5893c5c 100644 --- a/doc/fd.1 +++ b/doc/fd.1 @@ -156,9 +156,20 @@ can be used as an alias. Enable the display of filesystem errors for situations such as insufficient permissions or dead symlinks. .TP -.B \-\-strip-cwd-prefix -By default, relative paths are prefixed with './' when the output goes to a non interactive terminal -(TTY). Use this flag to disable this behaviour. +.B \-\-strip-cwd-prefix [when] +By default, relative paths are prefixed with './' when -x/--exec, +-X/--exec-batch, or -0/--print0 are given, to reduce the risk of a +path starting with '-' being treated as a command line option. Use +this flag to change this behavior. If this flag is used without a value, +it is equivalent to passing "always". Possible values are: +.RS +.IP auto +Use the default behavior. +.IP never +Never strip the ./ at the beginning of paths +.IP always +Always strip the ./ at the beginning of paths +.RE .TP .B \-\-one\-file\-system, \-\-mount, \-\-xdev 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). diff --git a/src/cli.rs b/src/cli.rs index d35d2fb..1b91862 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -617,9 +617,10 @@ pub struct Opts { /// By default, relative paths are prefixed with './' when -x/--exec, /// -X/--exec-batch, or -0/--print0 are given, to reduce the risk of a /// path starting with '-' being treated as a command line option. Use - /// this flag to disable this behaviour. - #[arg(long, conflicts_with_all(&["path", "search_path"]), hide_short_help = true, long_help)] - pub strip_cwd_prefix: bool, + /// this flag to change this behavior. If this flag is used without a value, + /// it is equivalent to passing "always". + #[arg(long, conflicts_with_all(&["path", "search_path"]), value_name = "when", hide_short_help = true, require_equals = true, long_help)] + strip_cwd_prefix: Option>, /// 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 @@ -700,6 +701,16 @@ impl Opts { .or_else(|| self.max_one_result.then_some(1)) } + pub fn strip_cwd_prefix bool>(&self, auto_pred: P) -> bool { + use self::StripCwdWhen::*; + self.no_search_paths() + && match self.strip_cwd_prefix.map_or(Auto, |o| o.unwrap_or(Always)) { + Auto => auto_pred(), + Always => true, + Never => false, + } + } + #[cfg(feature = "completions")] pub fn gen_completions(&self) -> anyhow::Result> { self.gen_completions @@ -760,6 +771,16 @@ pub enum ColorWhen { Never, } +#[derive(Copy, Clone, PartialEq, Eq, Debug, ValueEnum)] +pub enum StripCwdWhen { + /// Use the default behavior + Auto, + /// Always strip the ./ at the beginning of paths + Always, + /// Never strip the ./ + Never, +} + // there isn't a derive api for getting grouped values yet, // so we have to use hand-rolled parsing for exec and exec-batch pub struct Exec { diff --git a/src/main.rs b/src/main.rs index bef4120..eacf02e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -311,8 +311,7 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result Date: Sun, 5 May 2024 23:57:25 -0600 Subject: [PATCH 2/2] docs: Make auto option for --strip-cwd-prefix more clear --- doc/fd.1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/fd.1 b/doc/fd.1 index 5893c5c..b7abcda 100644 --- a/doc/fd.1 +++ b/doc/fd.1 @@ -163,12 +163,12 @@ path starting with '-' being treated as a command line option. Use this flag to change this behavior. If this flag is used without a value, it is equivalent to passing "always". Possible values are: .RS -.IP auto -Use the default behavior. .IP never Never strip the ./ at the beginning of paths .IP always Always strip the ./ at the beginning of paths +.IP auto +Only strip if used with --exec, --exec-batch, or --print0. That is, it resets to the default behavior. .RE .TP .B \-\-one\-file\-system, \-\-mount, \-\-xdev