From 2c3e40c9d9627141a4fd884823db52dabf4463cb Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Mon, 7 Nov 2022 23:54:00 -0700 Subject: [PATCH 1/3] Use new Shell::from_env method in clap_complete Instead of having an fd specific implementation. --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/cli.rs | 22 +--------------------- 3 files changed, 4 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aa9df73..af137b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,9 +129,9 @@ dependencies = [ [[package]] name = "clap_complete" -version = "4.0.3" +version = "4.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfe581a2035db4174cdbdc91265e1aba50f381577f0510d0ad36c7bc59cc84a3" +checksum = "96b0fba905b035a30d25c1b585bf1171690712fbb0ad3ac47214963aa4acc36c" dependencies = [ "clap", ] diff --git a/Cargo.toml b/Cargo.toml index 878c1c5..6f57249 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,7 @@ normpath = "0.3.2" chrono = "0.4" once_cell = "1.15.0" crossbeam-channel = "0.5.6" -clap_complete = {version = "4.0", optional = true} +clap_complete = {version = "4.0.5", optional = true} [dependencies.clap] version = "4.0.12" diff --git a/src/cli.rs b/src/cli.rs index 465473f..004b0cb 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -673,32 +673,12 @@ impl Opts { self.gen_completions .map(|maybe_shell| match maybe_shell { Some(sh) => Ok(sh), - None => guess_shell(), + None => Shell::from_env().ok_or_else(|| anyhow!("Unable to get shell from environment")), }) .transpose() } } -#[cfg(feature = "completions")] -fn guess_shell() -> anyhow::Result { - let env_shell = std::env::var_os("SHELL").map(PathBuf::from); - if let Some(shell) = env_shell - .as_ref() - .and_then(|s| s.file_name()) - .and_then(|s| s.to_str()) - { - shell - .parse::() - .map_err(|_| anyhow!("Unknown shell {}", shell)) - } else { - // Assume powershell on windows - #[cfg(windows)] - return Ok(Shell::PowerShell); - #[cfg(not(windows))] - return Err(anyhow!("Unable to get shell from environment")); - } -} - #[derive(Copy, Clone, PartialEq, Eq, ValueEnum)] pub enum FileType { #[value(alias = "f")] From 84bf65e02384845ae97dac04f89681028246e5f1 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Tue, 8 Nov 2022 00:03:55 -0700 Subject: [PATCH 2/3] Use unit type for negating optins with clap_derive As now supported by https://github.com/clap-rs/clap/pull/4371 and https://github.com/clap-rs/clap/pull/4459 --- Cargo.lock | 8 ++--- Cargo.toml | 2 +- src/cli.rs | 90 ++++++++++++++---------------------------------------- 3 files changed, 28 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index af137b4..cd9f42a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,9 +113,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.0.18" +version = "4.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "335867764ed2de42325fafe6d18b8af74ba97ee0c590fa016f157535b42ab04b" +checksum = "91b9970d7505127a162fdaa9b96428d28a479ba78c9ec7550a63a5d9863db682" dependencies = [ "atty", "bitflags", @@ -138,9 +138,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.0.18" +version = "4.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16a1b0f6422af32d5da0c58e2703320f379216ee70198241c84173a8c5ac28f3" +checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014" dependencies = [ "heck", "proc-macro-error", diff --git a/Cargo.toml b/Cargo.toml index 6f57249..5ae8e9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ crossbeam-channel = "0.5.6" clap_complete = {version = "4.0.5", optional = true} [dependencies.clap] -version = "4.0.12" +version = "4.0.22" features = ["suggestions", "color", "wrap_help", "cargo", "unstable-grouped", "derive"] [target.'cfg(unix)'.dependencies] diff --git a/src/cli.rs b/src/cli.rs index 004b0cb..09630a6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -17,69 +17,6 @@ use crate::filesystem; use crate::filter::OwnerFilter; use crate::filter::SizeFilter; -// Type for options that don't have any values, but are used to negate -// earlier options -struct Negations; - -impl clap::FromArgMatches for Negations { - fn from_arg_matches(_: &ArgMatches) -> clap::error::Result { - Ok(Negations) - } - - fn update_from_arg_matches(&mut self, _: &ArgMatches) -> clap::error::Result<()> { - Ok(()) - } -} - -impl clap::Args for Negations { - fn augment_args(cmd: Command) -> Command { - Self::augment_args_for_update(cmd) - } - - fn augment_args_for_update(cmd: Command) -> Command { - cmd.arg( - Arg::new("no_hidden") - .action(ArgAction::Count) - .long("no-hidden") - .overrides_with("hidden") - .hide(true) - .long_help("Overrides --hidden."), - ) - .arg( - Arg::new("ignore") - .action(ArgAction::Count) - .long("ignore") - .overrides_with("no_ignore") - .hide(true) - .long_help("Overrides --no-ignore."), - ) - .arg( - Arg::new("ignore_vcs") - .action(ArgAction::Count) - .long("ignore-vcs") - .overrides_with("no_ignore_vcs") - .hide(true) - .long_help("Overrides --no-ignore-vcs."), - ) - .arg( - Arg::new("relative_path") - .action(ArgAction::Count) - .long("relative-path") - .overrides_with("absolute_path") - .hide(true) - .long_help("Overrides --absolute-path."), - ) - .arg( - Arg::new("no_follow") - .action(ArgAction::Count) - .long("no-follow") - .overrides_with("follow") - .hide(true) - .long_help("Overrides --follow."), - ) - } -} - #[derive(Parser)] #[command( name = "fd", @@ -103,6 +40,10 @@ pub struct Opts { )] pub hidden: bool, + /// Overrides --hidden + #[arg(long, overrides_with = "hidden", hide = true, action = ArgAction::SetTrue)] + no_hidden: (), + /// Do not respect .(git|fd)ignore files #[arg( long, @@ -113,6 +54,10 @@ pub struct Opts { )] pub no_ignore: bool, + /// Overrides --no-ignore + #[arg(long, overrides_with = "no_ignore", hide = true, action = ArgAction::SetTrue)] + ignore: (), + /// Do not respect .gitignore files #[arg( long, @@ -122,6 +67,10 @@ pub struct Opts { )] pub no_ignore_vcs: bool, + /// Overrides --no-ignore-vcs + #[arg(long, overrides_with = "no_ignore_vcs", hide = true, action = ArgAction::SetTrue)] + ignore_vcs: (), + /// Do not respect .(git|fd)ignore files in parent directories #[arg( long, @@ -204,6 +153,10 @@ pub struct Opts { )] pub absolute_path: bool, + /// Overrides --absolute-path + #[arg(long, overrides_with = "absolute_path", hide = true, action = ArgAction::SetTrue)] + relative_path: (), + /// Use a long listing format with file metadata #[arg( long, @@ -227,6 +180,10 @@ pub struct Opts { )] pub follow: bool, + /// Overrides --follow + #[arg(long, overrides_with = "follow", hide = true, action = ArgAction::SetTrue)] + no_follow: (), + /// Search full abs. path (default: filename only) #[arg( long, @@ -591,9 +548,6 @@ pub struct Opts { #[cfg(feature = "completions")] #[arg(long, hide = true, exclusive = true)] gen_completions: Option>, - - #[clap(flatten)] - _negations: Negations, } impl Opts { @@ -673,7 +627,9 @@ impl Opts { self.gen_completions .map(|maybe_shell| match maybe_shell { Some(sh) => Ok(sh), - None => Shell::from_env().ok_or_else(|| anyhow!("Unable to get shell from environment")), + None => { + Shell::from_env().ok_or_else(|| anyhow!("Unable to get shell from environment")) + } }) .transpose() } From 38d406876d8d416533c1f9b58b038f638a65c18b Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Fri, 11 Nov 2022 00:34:30 -0700 Subject: [PATCH 3/3] Fix a couple small clippy warnings - suppress warning about unused function on windows - remove extraneous & in test --- tests/testenv/mod.rs | 1 + tests/tests.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/testenv/mod.rs b/tests/testenv/mod.rs index 8a5567e..f827e78 100644 --- a/tests/testenv/mod.rs +++ b/tests/testenv/mod.rs @@ -186,6 +186,7 @@ impl TestEnv { } /// Get the path of the fd executable. + #[cfg_attr(windows, allow(unused))] pub fn test_exe(&self) -> &PathBuf { &self.fd_exe } diff --git a/tests/tests.rs b/tests/tests.rs index eb24472..732b61e 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2208,7 +2208,7 @@ fn test_invalid_cwd() { std::env::set_current_dir(&root).unwrap(); fs::remove_dir(&root).unwrap(); - let output = std::process::Command::new(&te.test_exe()) + let output = std::process::Command::new(te.test_exe()) .arg("query") .arg(te.test_root()) .output()