Skip to content

Commit

Permalink
Impl generate-completion subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
plustik committed Nov 10, 2023
1 parent 7375f7a commit 08de951
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
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 @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions src/subcommands/generate_completion.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
1 change: 1 addition & 0 deletions src/subcommands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod diff;
pub mod generate_completion;
pub mod list_syntax_themes;
pub mod parse_ansi;
mod sample_diff;
Expand Down

0 comments on commit 08de951

Please sign in to comment.