From 80caba1db35c06f839d38c86a34d7baf917d5fbb Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 28 May 2022 07:43:28 -0700 Subject: [PATCH] add `wezterm shell-completion` subcommand to generate shell completions --- Cargo.lock | 10 ++++++++++ wezterm/Cargo.toml | 3 ++- wezterm/src/main.rs | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index b766abae8..e8e3c1fda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -540,6 +540,15 @@ dependencies = [ "textwrap", ] +[[package]] +name = "clap_complete" +version = "3.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da92e6facd8d73c22745a5d3cbb59bdf8e46e3235c923e516527d8e81eec14a4" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "3.1.18" @@ -4630,6 +4639,7 @@ dependencies = [ "anyhow", "chrono", "clap", + "clap_complete", "codec", "config", "env-bootstrap", diff --git a/wezterm/Cargo.toml b/wezterm/Cargo.toml index e4477606f..c5b2a65b3 100644 --- a/wezterm/Cargo.toml +++ b/wezterm/Cargo.toml @@ -12,6 +12,8 @@ anyhow = "1.0" [dependencies] anyhow = "1.0" chrono = "0.4" +clap = {version="3.1", features=["derive"]} +clap_complete = "3.1" codec = { path = "../codec" } config = { path = "../config" } env-bootstrap = { path = "../env-bootstrap" } @@ -26,7 +28,6 @@ serde = {version="1.0", features = ["derive"]} serde_json = "1.0" shell-words = "1.1" smol = "1.2" -clap = {version="3.1", features=["derive"]} tabout = { path = "../tabout" } tempfile = "3.3" termwiz = { path = "../termwiz" } diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index f06ee43cc..4528b6080 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -1,6 +1,7 @@ use anyhow::{anyhow, Context}; use chrono::{DateTime, Utc}; use clap::Parser; +use clap_complete::{generate as generate_completion, Shell}; use config::keyassignment::SpawnTabDomain; use config::wezterm_version; use mux::activity::Activity; @@ -91,6 +92,14 @@ enum SubCommand { #[clap(name = "replay", about = "Replay an asciicast terminal session")] Replay(asciicast::PlayCommand), + + /// Generate shell completion information + #[clap(name = "shell-completion")] + ShellCompletion { + /// Which shell to generate for + #[clap(long, possible_values=["bash", "elvish", "fish", "powershell", "zsh"])] + shell: Shell, + }, } #[derive(Debug, Parser, Clone)] @@ -469,6 +478,13 @@ fn run() -> anyhow::Result<()> { SubCommand::Cli(cli) => run_cli(config, cli), SubCommand::Record(cmd) => cmd.run(config), SubCommand::Replay(cmd) => cmd.run(), + SubCommand::ShellCompletion { shell } => { + use clap::CommandFactory; + let mut cmd = Opt::command(); + let name = cmd.get_name().to_string(); + generate_completion(shell, &mut cmd, name, &mut std::io::stdout()); + Ok(()) + } } }