Skip to content

Commit 6becb66

Browse files
authored
Merge pull request #1445 from tmccombs/cwd-prefix
feat: Add option to always include cwd prefix
2 parents 1a1f057 + 90d3381 commit 6becb66

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Features
44

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.
56

67
## Bugfixes
78

contrib/completion/_fd

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ _fd() {
162162
$no'(*)*--search-path=[set search path (instead of positional <path> arguments)]:directory:_files -/'
163163

164164
+ strip-cwd-prefix
165-
$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)'
166166

167167
+ and
168168
'--and=[additional required search path]:pattern'

doc/fd.1

+14-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,20 @@ can be used as an alias.
156156
Enable the display of filesystem errors for situations such as insufficient
157157
permissions or dead symlinks.
158158
.TP
159-
.B \-\-strip-cwd-prefix
160-
By default, relative paths are prefixed with './' when the output goes to a non interactive terminal
161-
(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 a
162+
path starting with '-' being treated as a command line option. Use
163+
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+
.RS
166+
.IP never
167+
Never strip the ./ at the beginning of paths
168+
.IP always
169+
Always strip the ./ at the beginning of paths
170+
.IP auto
171+
Only strip if used with --exec, --exec-batch, or --print0. That is, it resets to the default behavior.
172+
.RE
162173
.TP
163174
.B \-\-one\-file\-system, \-\-mount, \-\-xdev
164175
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).

src/cli.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,10 @@ pub struct Opts {
617617
/// By default, relative paths are prefixed with './' when -x/--exec,
618618
/// -X/--exec-batch, or -0/--print0 are given, to reduce the risk of a
619619
/// path starting with '-' being treated as a command line option. Use
620-
/// 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>>,
623624

624625
/// By default, fd will traverse the file system tree as far as other options
625626
/// dictate. With this flag, fd ensures that it does not descend into a
@@ -700,6 +701,16 @@ impl Opts {
700701
.or_else(|| self.max_one_result.then_some(1))
701702
}
702703

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+
703714
#[cfg(feature = "completions")]
704715
pub fn gen_completions(&self) -> anyhow::Result<Option<Shell>> {
705716
self.gen_completions
@@ -760,6 +771,16 @@ pub enum ColorWhen {
760771
Never,
761772
}
762773

774+
#[derive(Copy, Clone, PartialEq, Eq, Debug, ValueEnum)]
775+
pub enum StripCwdWhen {
776+
/// Use the default behavior
777+
Auto,
778+
/// Always strip the ./ at the beginning of paths
779+
Always,
780+
/// Never strip the ./
781+
Never,
782+
}
783+
763784
// there isn't a derive api for getting grouped values yet,
764785
// so we have to use hand-rolled parsing for exec and exec-batch
765786
pub struct Exec {

src/main.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
311311
path_separator,
312312
actual_path_separator,
313313
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)),
316315
})
317316
}
318317

0 commit comments

Comments
 (0)