From f792a20f7aa81d39078a1403ac257ae19b210228 Mon Sep 17 00:00:00 2001 From: grant0417 Date: Sat, 20 Aug 2022 23:48:04 -0700 Subject: [PATCH] Add `fig` to shells for shell completion --- Cargo.lock | 11 +++++++++++ wezterm/Cargo.toml | 1 + wezterm/src/main.rs | 40 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7563f27db..915e1a6d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -538,6 +538,16 @@ dependencies = [ "clap 3.2.17", ] +[[package]] +name = "clap_complete_fig" +version = "3.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed37b4c0c1214673eba6ad8ea31666626bf72be98ffb323067d973c48b4964b9" +dependencies = [ + "clap 3.2.17", + "clap_complete", +] + [[package]] name = "clap_derive" version = "3.2.17" @@ -5253,6 +5263,7 @@ dependencies = [ "chrono", "clap 3.2.17", "clap_complete", + "clap_complete_fig", "codec", "config", "env-bootstrap", diff --git a/wezterm/Cargo.toml b/wezterm/Cargo.toml index c5b2a65b3..a630bd9a1 100644 --- a/wezterm/Cargo.toml +++ b/wezterm/Cargo.toml @@ -14,6 +14,7 @@ anyhow = "1.0" chrono = "0.4" clap = {version="3.1", features=["derive"]} clap_complete = "3.1" +clap_complete_fig = "3.2" codec = { path = "../codec" } config = { path = "../config" } env-bootstrap = { path = "../env-bootstrap" } diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index aabe58ff9..5ca7ae40a 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Context}; use chrono::{DateTime, Utc}; -use clap::{Parser, ValueHint}; -use clap_complete::{generate as generate_completion, Shell}; +use clap::{Parser, ValueEnum, ValueHint}; +use clap_complete::{generate as generate_completion, shells, Generator as CompletionGenerator}; use config::keyassignment::SpawnTabDomain; use config::wezterm_version; use mux::activity::Activity; @@ -56,6 +56,40 @@ struct Opt { cmd: Option, } +#[derive(Debug, Clone, ValueEnum)] +enum Shell { + Bash, + Elvish, + Fish, + PowerShell, + Zsh, + Fig, +} + +impl CompletionGenerator for Shell { + fn file_name(&self, name: &str) -> String { + match self { + Shell::Bash => shells::Bash.file_name(name), + Shell::Elvish => shells::Elvish.file_name(name), + Shell::Fish => shells::Fish.file_name(name), + Shell::PowerShell => shells::PowerShell.file_name(name), + Shell::Zsh => shells::Zsh.file_name(name), + Shell::Fig => clap_complete_fig::Fig.file_name(name), + } + } + + fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) { + match self { + Shell::Bash => shells::Bash.generate(cmd, buf), + Shell::Elvish => shells::Elvish.generate(cmd, buf), + Shell::Fish => shells::Fish.generate(cmd, buf), + Shell::PowerShell => shells::PowerShell.generate(cmd, buf), + Shell::Zsh => shells::Zsh.generate(cmd, buf), + Shell::Fig => clap_complete_fig::Fig.generate(cmd, buf), + } + } +} + #[derive(Debug, Parser, Clone)] enum SubCommand { #[clap( @@ -102,7 +136,7 @@ enum SubCommand { #[clap(name = "shell-completion")] ShellCompletion { /// Which shell to generate for - #[clap(long, possible_values=["bash", "elvish", "fish", "powershell", "zsh"])] + #[clap(long, value_parser)] shell: Shell, }, }