1
0
mirror of https://github.com/sharkdp/fd synced 2024-07-05 09:19:11 +00:00

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> \ argument. Changes the usage to `fd [FLAGS/OPTIONS] --search-path <path> \
--search-path <path2> [<pattern>]`", --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) { if cfg!(unix) {

View File

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

View File

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

View File

@ -377,7 +377,7 @@ fn construct_config(matches: clap::ArgMatches, pattern_regex: &str) -> Result<Co
}), }),
no_strip: matches.is_present("path") no_strip: matches.is_present("path")
|| matches.is_present("search-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] #[test]
fn test_no_strip() { fn test_no_strip() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES); 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( te.assert_output(
&["--search-path=."], &["-P", "."],
"./a.foo "a.foo
./e1 e2 e1 e2
./one one
./one/b.foo one/b.foo
./one/two one/two
./one/two/c.foo one/two/c.foo
./one/two/C.Foo2 one/two/C.Foo2
./one/two/three one/two/three
./one/two/three/d.foo one/two/three/d.foo
./one/two/three/directory_foo one/two/three/directory_foo
./symlink", symlink",
); );
} }