diff options
author | Thayne McCombs <astrothayne@gmail.com> | 2024-04-27 22:10:55 -0600 |
---|---|---|
committer | Thayne McCombs <astrothayne@gmail.com> | 2024-05-07 00:24:06 -0600 |
commit | b1f83a0bb070e3fe96139706f759c62bf9e45c4b (patch) | |
tree | ca19e720ef9a8ed58fc64b84e90a77ffdd94b59b /src | |
parent | 3bc70925a98b3eae64a7937e06a699cd104b6d9e (diff) |
feat: Add option to always include cwd prefix
Fixes: #1243
Fixes: #1331
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 27 | ||||
-rw-r--r-- | src/main.rs | 3 |
2 files changed, 25 insertions, 5 deletions
@@ -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<Option<StripCwdWhen>>, /// 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<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")] pub fn gen_completions(&self) -> anyhow::Result<Option<Shell>> { 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<Config path_separator, actual_path_separator, max_results: opts.max_results(), - strip_cwd_prefix: (opts.no_search_paths() - && (opts.strip_cwd_prefix || !(opts.null_separator || has_command))), + strip_cwd_prefix: opts.strip_cwd_prefix(|| !(opts.null_separator || has_command)), }) } |