summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorplustik <plustik@tecks.eu>2023-11-10 22:00:50 +0100
committerplustik <plustik@tecks.eu>2023-11-10 22:00:50 +0100
commit08de95138c53a988c17f226ee18292182e90fe2c (patch)
tree86a7a9121534bfdc7797e858cc62401b5a2dcf8a
parent7375f7a165dabe430e12d531fedd84bb3a027c6b (diff)
Impl generate-completion subcommand
-rw-r--r--Cargo.lock10
-rw-r--r--Cargo.toml1
-rw-r--r--src/cli.rs6
-rw-r--r--src/main.rs6
-rw-r--r--src/subcommands/generate_completion.rs11
-rw-r--r--src/subcommands/mod.rs1
6 files changed, 34 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1f0374ee..6b47c5d1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -303,6 +303,15 @@ dependencies = [
]
[[package]]
+name = "clap_complete"
+version = "4.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bffe91f06a11b4b9420f62103854e90867812cd5d01557f853c5ee8e791b12ae"
+dependencies = [
+ "clap",
+]
+
+[[package]]
name = "clap_derive"
version = "4.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -559,6 +568,7 @@ dependencies = [
"chrono",
"chrono-humanize",
"clap",
+ "clap_complete",
"console",
"ctrlc",
"dirs",
diff --git a/Cargo.toml b/Cargo.toml
index b54ed5d1..5aa04339 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -43,6 +43,7 @@ unicode-segmentation = "1.10.1"
unicode-width = "0.1.10"
vte = "0.11.0"
xdg = "2.4.1"
+clap_complete = "4.4.4"
[dependencies.git2]
version = "0.17.2"
diff --git a/src/cli.rs b/src/cli.rs
index 817ab056..58d8ebef 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
use bat::assets::HighlightingAssets;
use clap::{ColorChoice, CommandFactory, FromArgMatches, Parser};
+use clap_complete::Shell;
use lazy_static::lazy_static;
use syntect::highlighting::Theme as SyntaxTheme;
use syntect::parsing::SyntaxSet;
@@ -390,6 +391,10 @@ pub struct Opt {
/// Sed-style command transforming file paths for display.
pub file_regex_replacement: Option<String>,
+ #[arg(long = "generate-completion")]
+ /// Print completion file for the given shell.
+ pub generate_completion: Option<Shell>,
+
#[arg(long = "grep-context-line-style", value_name = "STYLE")]
/// Style string for non-matching lines of grep output.
///
@@ -1194,6 +1199,7 @@ impl Opt {
// pseudo-flag commands such as --list-languages
lazy_static! {
static ref IGNORED_OPTION_NAMES: HashSet<&'static str> = vec![
+ "generate-completion",
"list-languages",
"list-syntax-themes",
"show-config",
diff --git a/src/main.rs b/src/main.rs
index a1f12622..67ea0ca3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -82,7 +82,11 @@ fn run_app() -> std::io::Result<i32> {
assets,
);
- let subcommand_result = if opt.list_languages {
+ let subcommand_result = if let Some(shell) = opt.generate_completion {
+ Some(subcommands::generate_completion::generate_completion_file(
+ shell,
+ ))
+ } else if opt.list_languages {
Some(list_languages())
} else if opt.list_syntax_themes {
Some(subcommands::list_syntax_themes::list_syntax_themes())
diff --git a/src/subcommands/generate_completion.rs b/src/subcommands/generate_completion.rs
new file mode 100644
index 00000000..00425b7f
--- /dev/null
+++ b/src/subcommands/generate_completion.rs
@@ -0,0 +1,11 @@
+use clap::CommandFactory;
+use clap_complete::{generate, Shell};
+
+use crate::cli;
+
+pub fn generate_completion_file(shell: Shell) -> std::io::Result<()> {
+ let mut cmd = cli::Opt::command();
+ let bin_name = cmd.get_bin_name().unwrap_or(cmd.get_name()).to_string();
+ generate(shell, &mut cmd, bin_name, &mut std::io::stdout());
+ Ok(())
+}
diff --git a/src/subcommands/mod.rs b/src/subcommands/mod.rs
index 26f04233..bb7a1f7c 100644
--- a/src/subcommands/mod.rs
+++ b/src/subcommands/mod.rs
@@ -1,4 +1,5 @@
pub mod diff;
+pub mod generate_completion;
pub mod list_syntax_themes;
pub mod parse_ansi;
mod sample_diff;