diff --git a/Cargo.lock b/Cargo.lock index 735ac90..51e860c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aad5b1b4de04fead402672b48897030eec1f3bfe1550776322f59f6d6e6a5677" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.40" @@ -278,6 +287,7 @@ version = "0.3.1" dependencies = [ "anyhow", "clap", + "clap_complete", "comfy-table", "git2", "insta", diff --git a/Cargo.toml b/Cargo.toml index 3d9f74c..655d194 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ exclude = ["src/tests/*"] git2 = { version = "0.20", default-features = false, features = ["https", "ssh", "vendored-openssl"] } walkdir = "2.5" clap = { version = "4.5", features = ["derive"] } +clap_complete = "4.5" anyhow = "1" comfy-table = "7.1.4" rayon = "1.10.0" diff --git a/src/cli.rs b/src/cli.rs index 2b138ac..7f4743b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use clap::Parser; +use clap_complete::Shell; /// Scan the given directory for Git repositories and display their status. /// A Repository turns red if it has unpushed changes. @@ -39,4 +40,7 @@ pub struct Args { /// but in a subfolder like `repo-name/checkout` #[arg(short, long)] pub subdir: Option, + /// Generate shell completions + #[arg(long, value_name = "SHELL")] + pub completions: Option, } diff --git a/src/main.rs b/src/main.rs index 72675a2..c0545fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ +use std::io; + use anyhow::Result; -use clap::Parser as _; -use cli::Args; +use clap::{CommandFactory as _, Parser as _}; + +use crate::cli::Args; mod cli; mod gitinfo; @@ -15,6 +18,13 @@ fn main() -> Result<()> { util::initialize_logger()?; let args = Args::parse(); + + if let Some(shell) = args.completions { + let mut cmd = Args::command(); + clap_complete::generate(shell, &mut cmd, env!("CARGO_PKG_NAME"), &mut io::stdout()); + return Ok(()); + } + if args.legend { printer::print_legend(); return Ok(()); diff --git a/src/tests/snapshots/git_statuses__tests__cli_test__git-statuses.snap b/src/tests/snapshots/git_statuses__tests__cli_test__git-statuses.snap index aa68a4b..9c7d846 100644 --- a/src/tests/snapshots/git_statuses__tests__cli_test__git-statuses.snap +++ b/src/tests/snapshots/git_statuses__tests__cli_test__git-statuses.snap @@ -36,6 +36,11 @@ Options: -s, --subdir Look in a specific subdir if it exists for each folder This can be useful, if you don't checkout in a folder directly but in a subfolder like `repo-name/checkout` + --completions + Generate shell completions + + [possible values: bash, elvish, fish, powershell, zsh] + -h, --help Print help