From e05f5010dafda6923561d83d3c9129a82b75447b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 3 Jun 2021 17:57:05 +0300 Subject: [PATCH] Add bash completion Closes https://github.com/sharkdp/bat/issues/1010 --- .github/workflows/CICD.yml | 1 + .gitignore | 1 + assets/completions/bat.bash.in | 84 ++++++++++++++++++++++++++++++++++ build.rs | 5 ++ 4 files changed, 91 insertions(+) create mode 100644 assets/completions/bat.bash.in diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index 11119192..22f114a7 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -268,6 +268,7 @@ jobs: cp "README.md" "LICENSE-MIT" "LICENSE-APACHE" "CHANGELOG.md" "$ARCHIVE_DIR" # Autocompletion files + cp 'target/${{ matrix.job.target }}/release/build/${{ env.PROJECT_NAME }}'-*/out/assets/completions/bat.bash "$ARCHIVE_DIR/autocomplete/${{ env.PROJECT_NAME }}.bash" cp 'target/${{ matrix.job.target }}/release/build/${{ env.PROJECT_NAME }}'-*/out/assets/completions/bat.fish "$ARCHIVE_DIR/autocomplete/${{ env.PROJECT_NAME }}.fish" cp 'target/${{ matrix.job.target }}/release/build/${{ env.PROJECT_NAME }}'-*/out/assets/completions/bat.zsh "$ARCHIVE_DIR/autocomplete/${{ env.PROJECT_NAME }}.zsh" diff --git a/.gitignore b/.gitignore index ea15e472..a3ea8cff 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ **/*.rs.bk # Generated files +/assets/completions/bat.bash /assets/completions/bat.fish /assets/completions/bat.zsh /assets/manual/bat.1 diff --git a/assets/completions/bat.bash.in b/assets/completions/bat.bash.in new file mode 100644 index 00000000..12dda749 --- /dev/null +++ b/assets/completions/bat.bash.in @@ -0,0 +1,84 @@ +# shellcheck disable=SC2207 + +_bat() { + if [[ ${COMP_WORDS[1]-} == cache ]]; then + case $3 in + --source | --target) + local IFS=$'\n' + COMPREPLY=($(compgen -d -- "$2")) + compopt -o filenames + return 0 + ;; + esac + COMPREPLY=($(compgen -W " + --build --clear --source --target --blank --help + " -- "$2")) + return 0 + fi + + case $3 in + -l | --language) + local IFS=$'\n' + COMPREPLY=($(compgen -W "$( + "$1" --list-languages | while IFS=: read -r lang _; do + printf "%s\n" "$lang" + done + )" -- "$2")) + compopt -o filenames # for escaping + return 0 + ;; + -H | --highlight-line | --diff-context | --tabs | --terminal-width | \ + -m | --map-syntax | --style | --line-range | -h | --help | -V | \ + --version) + # argument required but no completion available, or argument + # causes an exit + return 0 + ;; + --file-name) + local IFS=$'\n' + COMPREPLY=($(compgen -f -- "$2")) + compopt -o filenames + return 0 + ;; + --wrap) + COMPREPLY=($(compgen -W "auto never character" -- "$2")) + return 0 + ;; + --color | --decorations | --paging) + COMPREPLY=($(compgen -W "auto never always" -- "$2")) + return 0 + ;; + --italic-text) + COMPREPLY=($(compgen -W "always never" -- "$2")) + return 0 + ;; + --pager) + COMPREPLY=($(compgen -c -- "$2")) + return 0 + ;; + --theme) + local IFS=$'\n' + COMPREPLY=($(compgen -W "$("$1" --list-themes)" -- "$2")) + compopt -o filenames + return 0 + ;; + esac + + if [[ $2 == -* ]]; then + COMPREPLY=($(compgen -W " + --show-all --plain --language --highlight-line + --file-name --diff --diff-context --tabs --wrap + --terminal-width --number --color --italic-text + --decorations --paging --pager --map-syntax --theme + --list-themes --style --line-range --list-languages + --help --version + " -- "$2")) + return 0 + fi + + local IFS=$'\n' + COMPREPLY=($(compgen -f -- "$2")) + compopt -o filenames + ((COMP_CWORD == 1)) && COMPREPLY+=($(compgen -W cache -- "$2")) + +} && complete -F _bat {{PROJECT_EXECUTABLE}} diff --git a/build.rs b/build.rs index 34c2e5f5..f969d34d 100644 --- a/build.rs +++ b/build.rs @@ -54,6 +54,11 @@ fn main() -> Result<(), Box> { "assets/manual/bat.1.in", out_dir.join("assets/manual/bat.1"), )?; + template( + &variables, + "assets/completions/bat.bash.in", + out_dir.join("assets/completions/bat.bash"), + )?; template( &variables, "assets/completions/bat.fish.in",