From a3908ad8c3d37198710947771421871b4cc36afa Mon Sep 17 00:00:00 2001 From: Michal S Date: Tue, 6 Sep 2022 13:07:22 +0100 Subject: [PATCH] Add Fig completion support --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/args.rs | 2 +- src/main.rs | 37 ++++++++++++++++++++++++------------- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 773e75c..7c62e08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,7 @@ dependencies = [ "chrono", "clap", "clap_complete", + "clap_complete_fig", "color-eyre", "colored", "console", @@ -249,6 +250,16 @@ dependencies = [ "clap", ] +[[package]] +name = "clap_complete_fig" +version = "3.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed37b4c0c1214673eba6ad8ea31666626bf72be98ffb323067d973c48b4964b9" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "3.2.18" diff --git a/Cargo.toml b/Cargo.toml index d70db56..a706b68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ textwrap = "0.15.0" crossterm = "0.25.0" toml = "0.5.9" clap_complete = "3.2.4" +clap_complete_fig = "3.2.4" color-eyre = { version = "0.6.2", features = ["issue-url", "url"] } indicatif = { version = "0.17.0", features = ["tokio"] } lazy_static = "1.4.0" diff --git a/src/args.rs b/src/args.rs index bb26676..a304e9e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -125,7 +125,7 @@ pub struct UpgradeArgs { #[derive(Default, Debug, Clone, Parser)] pub struct GenCompArgs { - /// The shell to generate completions for (bash, fish, elvish, pwsh) + /// The shell to generate completions for (bash, fish, elvish, pwsh, fig) #[clap(required = true)] pub shell: String, } diff --git a/src/main.rs b/src/main.rs index e1622aa..a49217b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,8 @@ use crate::logging::get_logger; use crate::logging::Printable; use clap_complete::{Generator, Shell}; +use clap_complete_fig::Fig; + use std::str::FromStr; mod args; @@ -200,19 +202,28 @@ async fn cmd_query(args: QueryArgs) { #[tracing::instrument(level = "trace")] fn cmd_gencomp(args: &GenCompArgs) { - let shell: Shell = Shell::from_str(&args.shell).unwrap_or_else(|e| { - crash!(AppExitCode::Other, "Invalid shell: {}", e); - }); - - if shell == Shell::Zsh { - crash!( - AppExitCode::Other, - "Zsh shell completions are currently unsupported due to a bug in the clap_completion crate" + if args.shell == "fig" { + clap_complete::generate( + Fig, + &mut ::command(), + "ame", + &mut std::io::stderr(), ); - }; + } else { + let shell: Shell = Shell::from_str(&args.shell).unwrap_or_else(|e| { + crash!(AppExitCode::Other, "Invalid shell, {}", e); + }); - shell.generate( - &::command(), - &mut std::io::stderr(), - ); + if shell == Shell::Zsh { + crash!( + AppExitCode::Other, + "Zsh shell completions are currently unsupported due to a bug in the clap_completion crate" + ); + }; + + shell.generate( + &::command(), + &mut std::io::stderr(), + ); + } }