add -P option to strip prefix to tty

This commit is contained in:
Jonah Caplan 2021-10-17 00:21:52 -04:00
parent 46db1c4ef3
commit bf9e6fd36e
5 changed files with 32 additions and 29 deletions

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("strip-prefix")
.long("strip-prefix")
.short("P")
.conflicts_with_all(&["path", "search-path"])
.help("When no search path is provided and output is non-tty, strip './' prefix from results")
.long_help(
"By default, relative results are prefixed with './' when output to non-ttys. \
Use this flag to disable this behaviour."
)
);
if cfg!(unix) {

View file

@ -21,7 +21,6 @@ pub use self::job::{batch, job};
use self::token::Token;
use crate::filesystem::strip_current_dir;
/// Execution mode of the command
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ExecutionMode {
@ -74,7 +73,7 @@ impl CommandTemplate {
S: AsRef<str>,
{
lazy_static! {
static ref PLACEHOLDER_PATTERN: Regex = Regex::new(r"\{(/?\.?|//|-)\}").unwrap();
static ref PLACEHOLDER_PATTERN: Regex = Regex::new(r"\{(/?\.?|//|-|//-)\}").unwrap();
}
let mut args = Vec::new();
@ -101,6 +100,7 @@ impl CommandTemplate {
"{//}" => tokens.push(Token::Parent),
"{/.}" => tokens.push(Token::BasenameNoExt),
"{-}" => tokens.push(Token::StripPrefix),
"{//-}" => tokens.push(Token::ParentStripPrefix),
_ => unreachable!("Unhandled placeholder"),
}
@ -238,6 +238,10 @@ impl ArgumentTemplate {
let path = strip_current_dir(path);
s.push(Self::replace_separator(path.as_ref(), path_separator))
}
ParentStripPrefix => {
let path = strip_current_dir(path);
s.push(Self::replace_separator(&dirname(path), path_separator))
}
Text(ref string) => s.push(string),
}
}

View file

@ -12,6 +12,7 @@ pub enum Token {
NoExt,
BasenameNoExt,
StripPrefix,
ParentStripPrefix,
Text(String),
}
@ -24,6 +25,7 @@ impl Display for Token {
Token::NoExt => f.write_str("{.}")?,
Token::BasenameNoExt => f.write_str("{/.}")?,
Token::StripPrefix => f.write_str("{-}")?,
Token::ParentStripPrefix => f.write_str("{//-}")?,
Token::Text(ref string) => f.write_str(string)?,
}
Ok(())

View file

@ -377,7 +377,7 @@ fn construct_config(matches: clap::ArgMatches, pattern_regex: &str) -> Result<Co
}),
no_strip: matches.is_present("path")
|| matches.is_present("search-path")
|| !interactive_terminal,
|| (!interactive_terminal && !matches.is_present("strip-prefix")),
})
}

View file

@ -1928,33 +1928,19 @@ fn test_error_if_hidden_not_set_and_pattern_starts_with_dot() {
#[test]
fn test_no_strip() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
te.assert_output(
&[".", "."],
"./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(
&["--search-path=."],
"./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",
&["-P", "."],
"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",
);
}