Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shell completions #1054

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ gix = { version = "^0.54.1", path = "gix", default-features = false }
time = "0.3.23"

clap = { version = "4.1.1", features = ["derive", "cargo"] }
clap_complete = "4.4.3"
prodash = { workspace = true, optional = true }
is-terminal = { version = "0.4.0", optional = true }
env_logger = { version = "0.10.0", default-features = false }
Expand Down
16 changes: 14 additions & 2 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::{
},
};

use anyhow::{Context, Result};
use clap::Parser;
use anyhow::{anyhow, Context, Result};
use clap::{CommandFactory, Parser};
use gitoxide_core as core;
use gitoxide_core::{pack::verify, repository::PathsOrPatterns};
use gix::bstr::{io::BufReadExt, BString};
Expand Down Expand Up @@ -1219,6 +1219,18 @@ pub fn main() -> Result<()> {
},
),
},
Subcommands::Completions { shell, out_dir } => {
let mut app = Args::command();
let shell = shell
.or_else(clap_complete::Shell::from_env)
.ok_or_else(|| anyhow!("The shell could not be derived from the environment"))?;
if let Some(out_dir) = out_dir {
clap_complete::generate_to(shell, &mut app, env!("CARGO_PKG_NAME"), &out_dir)?;
} else {
clap_complete::generate(shell, &mut app, env!("CARGO_PKG_NAME"), &mut std::io::stdout());
}
Ok(())
}
}?;
Ok(())
}
Expand Down
10 changes: 10 additions & 0 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;

use clap_complete::Shell;
use gitoxide_core as core;
use gix::bstr::BString;

Expand Down Expand Up @@ -134,6 +135,15 @@ pub enum Subcommands {
/// Subcommands that need no git repository to run.
#[clap(subcommand)]
Free(free::Subcommands),
/// Generate shell completions to stdout or a directory.
#[clap(visible_alias = "generate-completions", visible_alias = "shell-completions")]
Completions {
/// The shell to generate completions for. Otherwise it's derived from the environment.
#[clap(long, short)]
shell: Option<Shell>,
/// The output directory in case multiple files are generated. If not provided, will write to stdout.
out_dir: Option<String>,
},
}

#[cfg(feature = "gitoxide-core-tools-archive")]
Expand Down