feat: Add option to always include cwd prefix

Fixes: #1243
Fixes: #1331
This commit is contained in:
Thayne McCombs 2024-04-27 22:10:55 -06:00
parent cd96ca071d
commit 11e328cecd
5 changed files with 41 additions and 9 deletions

View File

@ -8,6 +8,7 @@
was often useful, it also broke some existing workflows, and there wasn't a good way to opt out of it. And there isn't really a good way for us to add was often useful, it also broke some existing workflows, and there wasn't a good way to opt out of it. And there isn't really a good way for us to add
a way to opt out of it. And you can easily get similar behavior by adding `.git/` to your global fdignore file. a way to opt out of it. And you can easily get similar behavior by adding `.git/` to your global fdignore file.
See #1457. See #1457.
- 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 ## Bugfixes

View File

@ -162,7 +162,7 @@ _fd() {
$no'(*)*--search-path=[set search path (instead of positional <path> arguments)]:directory:_files -/' $no'(*)*--search-path=[set search path (instead of positional <path> arguments)]:directory:_files -/'
+ strip-cwd-prefix + 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
'--and=[additional required search path]:pattern' '--and=[additional required search path]:pattern'

17
doc/fd.1 vendored
View File

@ -156,9 +156,20 @@ can be used as an alias.
Enable the display of filesystem errors for situations such as insufficient Enable the display of filesystem errors for situations such as insufficient
permissions or dead symlinks. permissions or dead symlinks.
.TP .TP
.B \-\-strip-cwd-prefix .B \-\-strip-cwd-prefix [when]
By default, relative paths are prefixed with './' when the output goes to a non interactive terminal By default, relative paths are prefixed with './' when -x/--exec,
(TTY). Use this flag to disable this behaviour. -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 .TP
.B \-\-one\-file\-system, \-\-mount, \-\-xdev .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). 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).

View File

@ -617,9 +617,10 @@ pub struct Opts {
/// By default, relative paths are prefixed with './' when -x/--exec, /// By default, relative paths are prefixed with './' when -x/--exec,
/// -X/--exec-batch, or -0/--print0 are given, to reduce the risk of a /// -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 /// path starting with '-' being treated as a command line option. Use
/// this flag to disable this behaviour. /// this flag to change this behavior. If this flag is used without a value,
#[arg(long, conflicts_with_all(&["path", "search_path"]), hide_short_help = true, long_help)] /// it is equivalent to passing "always".
pub strip_cwd_prefix: bool, #[arg(long, conflicts_with_all(&["path", "search_path"]), value_name = "when", hide_short_help = true, require_equals = true, long_help)]
strip_cwd_prefix: Option<Option<StripCwdWhen>>,
/// By default, fd will traverse the file system tree as far as other options /// 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 /// 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)) .or_else(|| self.max_one_result.then_some(1))
} }
pub fn strip_cwd_prefix<P: FnOnce() -> 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")] #[cfg(feature = "completions")]
pub fn gen_completions(&self) -> anyhow::Result<Option<Shell>> { pub fn gen_completions(&self) -> anyhow::Result<Option<Shell>> {
self.gen_completions self.gen_completions
@ -760,6 +771,16 @@ pub enum ColorWhen {
Never, 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, // there isn't a derive api for getting grouped values yet,
// so we have to use hand-rolled parsing for exec and exec-batch // so we have to use hand-rolled parsing for exec and exec-batch
pub struct Exec { pub struct Exec {

View File

@ -311,8 +311,7 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
path_separator, path_separator,
actual_path_separator, actual_path_separator,
max_results: opts.max_results(), max_results: opts.max_results(),
strip_cwd_prefix: (opts.no_search_paths() strip_cwd_prefix: opts.strip_cwd_prefix(|| !(opts.null_separator || has_command)),
&& (opts.strip_cwd_prefix || !(opts.null_separator || has_command))),
}) })
} }