From bf9e6fd36ec6d54a09a4ec0aab973bcfc0babcb5 Mon Sep 17 00:00:00 2001 From: Jonah Caplan Date: Sun, 17 Oct 2021 00:21:52 -0400 Subject: [PATCH] add -P option to strip prefix to tty --- src/app.rs | 11 +++++++++++ src/exec/mod.rs | 8 ++++++-- src/exec/token.rs | 2 ++ src/main.rs | 2 +- tests/tests.rs | 38 ++++++++++++-------------------------- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/app.rs b/src/app.rs index b26593b..0ca1fb7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -615,6 +615,17 @@ pub fn build_app() -> App<'static, 'static> { argument. Changes the usage to `fd [FLAGS/OPTIONS] --search-path \ --search-path []`", ), + ) + .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) { diff --git a/src/exec/mod.rs b/src/exec/mod.rs index fd0705d..cff754c 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -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, { 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), } } diff --git a/src/exec/token.rs b/src/exec/token.rs index 2e4b913..9eb68df 100644 --- a/src/exec/token.rs +++ b/src/exec/token.rs @@ -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(()) diff --git a/src/main.rs b/src/main.rs index 2f13981..3c09e88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -377,7 +377,7 @@ fn construct_config(matches: clap::ArgMatches, pattern_regex: &str) -> Result