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

Add --completion <SHELL> to provide shell completions #3126

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Add or remove individual style components without replacing all styles #2929 (@eth-p)
- Automatically choose theme based on the terminal's color scheme, see #2896 (@bash)
- Add option `--binary=as-text` for printing binary content, see issue #2974 and PR #2976 (@einfachIrgendwer0815)
- Make shell completions available via `--completion <shell>`, see issue #2057 and PR #3126 (@einfachIrgendwer0815)

## Bugfixes

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ cargo install --locked bat
Note that additional files like the man page or shell completion
files can not be installed in this way. They will be generated by `cargo` and should be available in the cargo target folder (under `build`).

Shell completions are also available by running:
```bash
bat --completion <shell>
# see --help for supported shells
```

## Customization

### Highlighting theme
Expand Down
17 changes: 17 additions & 0 deletions build/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,22 @@ pub fn gen_man_and_comp() -> anyhow::Result<()> {
out_dir.join("assets/completions/bat.zsh"),
)?;

println!(
"cargo:rustc-env=BAT_GENERATED_COMPLETION_BASH={}",
out_dir.join("assets/completions/bat.bash").display()
);
println!(
"cargo:rustc-env=BAT_GENERATED_COMPLETION_FISH={}",
out_dir.join("assets/completions/bat.fish").display()
);
println!(
"cargo:rustc-env=BAT_GENERATED_COMPLETION_PS1={}",
out_dir.join("assets/completions/_bat.ps1").display()
);
println!(
"cargo:rustc-env=BAT_GENERATED_COMPLETION_ZSH={}",
out_dir.join("assets/completions/bat.zsh").display()
);

Ok(())
}
3 changes: 3 additions & 0 deletions doc/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ Options:
This option exists for POSIX-compliance reasons ('u' is for 'unbuffered'). The output is
always unbuffered - this option is simply ignored.

--completion <SHELL>
Show shell completion for a certain shell. [possible values: bash, fish, zsh, ps1]

--diagnostic
Show diagnostic information for bug reports.

Expand Down
2 changes: 2 additions & 0 deletions doc/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Options:
Only print the lines from N to M.
-L, --list-languages
Display all supported languages.
--completion <SHELL>
Show shell completion for a certain shell. [possible values: bash, fish, zsh, ps1]
-h, --help
Print help (see more with '--help')
-V, --version
Expand Down
11 changes: 11 additions & 0 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,17 @@ pub fn build_app(interactive_output: bool) -> Command {
.help("Do not load custom assets"),
);

#[cfg(feature = "application")]
{
app = app.arg(
Arg::new("completion")
.long("completion")
.value_name("SHELL")
.value_parser(["bash", "fish", "ps1", "zsh"])
.help("Show shell completion for a certain shell. [possible values: bash, fish, zsh, ps1]"),
);
}

#[cfg(feature = "lessopen")]
{
app = app
Expand Down
6 changes: 6 additions & 0 deletions src/bin/bat/completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::env;

pub const BASH_COMPLETION: &str = include_str!(env!("BAT_GENERATED_COMPLETION_BASH"));
pub const FISH_COMPLETION: &str = include_str!(env!("BAT_GENERATED_COMPLETION_FISH"));
pub const PS1_COMPLETION: &str = include_str!(env!("BAT_GENERATED_COMPLETION_PS1"));
pub const ZSH_COMPLETION: &str = include_str!(env!("BAT_GENERATED_COMPLETION_ZSH"));
14 changes: 14 additions & 0 deletions src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
mod app;
mod assets;
mod clap_app;
#[cfg(feature = "application")]
mod completions;
mod config;
mod directories;
mod input;
Expand Down Expand Up @@ -347,6 +349,18 @@ fn run() -> Result<bool> {
return Ok(true);
}

#[cfg(feature = "application")]
if let Some(shell) = app.matches.get_one::<String>("completion") {
match shell.as_str() {
"bash" => println!("{}", completions::BASH_COMPLETION),
"fish" => println!("{}", completions::FISH_COMPLETION),
"ps1" => println!("{}", completions::PS1_COMPLETION),
"zsh" => println!("{}", completions::ZSH_COMPLETION),
_ => unreachable!("No completion for shell '{}' available.", shell),
}
return Ok(true);
}

match app.matches.subcommand() {
Some(("cache", cache_matches)) => {
// If there is a file named 'cache' in the current working directory,
Expand Down
Loading