add -N/--no-strip option to show "./" prefix on relative paths

This commit is contained in:
Jonah Caplan 2021-10-10 00:06:04 -04:00
parent 7b5b3ec47b
commit 6b92c5db90
6 changed files with 55 additions and 1 deletions

View File

@ -12,6 +12,8 @@
- Add new `--no-ignore-parent` flag, see #787 (@will459)
- Add new `-N, --no-strip` flag, see #760 (@jcaplan)
## Bugfixes
- Set default path separator to `/` in MSYS, see #537 and #730 (@aswild)

View File

@ -615,6 +615,17 @@ pub fn build_app() -> App<'static, 'static> {
argument. Changes the usage to `fd [FLAGS/OPTIONS] --search-path <path> \
--search-path <path2> [<pattern>]`",
),
)
.arg(
Arg::with_name("no-strip")
.long("no-strip")
.short("N")
.conflicts_with_all(&["absolute-path", "list-details"])
.help("Show all non-absolute paths with './' prefix")
.long_help(
"Show all non-absolute paths with './' prefix. This flag only affects \
the output when no search path is provided.")
);
if cfg!(unix) {

View File

@ -109,4 +109,7 @@ pub struct Config {
/// The maximum number of search results
pub max_results: Option<usize>,
/// Whether to prefix each relative path with "./"
pub no_strip: bool,
}

View File

@ -375,6 +375,7 @@ fn construct_config(matches: clap::ArgMatches, pattern_regex: &str) -> Result<Co
None
}
}),
no_strip: matches.is_present("no-strip"),
})
}

View File

@ -22,7 +22,7 @@ pub fn print_entry(
config: &Config,
wants_to_quit: &Arc<AtomicBool>,
) {
let path = if entry.is_absolute() {
let path = if entry.is_absolute() || config.no_strip {
entry
} else {
strip_current_dir(entry)

View File

@ -1901,3 +1901,40 @@ fn test_error_if_hidden_not_set_and_pattern_starts_with_dot() {
te.assert_output(&["--hidden", "--glob", ".gitignore"], ".gitignore");
te.assert_output(&[".gitignore"], "");
}
/// Show "./" prefix if the --no-strip flag is provided
#[test]
fn test_no_strip() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
te.assert_output(
&["--no-strip"],
"./a.foo
./e1 e2
./one
./one/b.foo
./one/two
./one/two/c.foo
./one/two/C.Foo2
./one/two/three
./one/two/three/d.foo
./one/two/three/directory_foo
./symlink"
);
te.assert_output(
&["--no-strip", "foo", "./one"],
"./one/b.foo
./one/two/c.foo
./one/two/C.Foo2
./one/two/three/d.foo
./one/two/three/directory_foo"
);
te.assert_output(
&["--no-strip", "foo", "one"],
"one/b.foo
one/two/c.foo
one/two/C.Foo2
one/two/three/d.foo
one/two/three/directory_foo"
);
}