From ad6cae433a6e3d4526ce5a1dbb8a30a34f1529c6 Mon Sep 17 00:00:00 2001 From: Josh Pencheon Date: Thu, 24 Feb 2022 06:57:23 +0000 Subject: [PATCH] fix Bash completion on macOS (#2074) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix Bash completion on macOS The Homebrew-provided bash-completion is version 1.x, which doesn't provide _init_completion. We add a standard shim instead. * Add entry to CHANGELOG * Update CHANGELOG.md Co-authored-by: Ville Skyttä * review: avoid calling _split_longopt repeatedly Co-authored-by: Ville Skyttä --- CHANGELOG.md | 2 ++ assets/completions/bat.bash.in | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56d28a3f..340cfaf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ ## Bugfixes +- Fix bash completion on bash 3.x and bash-completion 1.x. See #2066 (@joshpencheon) + ## Other ## Syntaxes diff --git a/assets/completions/bat.bash.in b/assets/completions/bat.bash.in index 43680a6c..97e32a0a 100644 --- a/assets/completions/bat.bash.in +++ b/assets/completions/bat.bash.in @@ -2,9 +2,22 @@ # Requires https://github.com/scop/bash-completion +# Macs have bash3 for which the bash-completion package doesn't include +# _init_completion. This is a minimal version of that function. +__bat_init_completion() +{ + COMPREPLY=() + _get_comp_words_by_ref "$@" cur prev words cword +} + _bat() { - local cur prev words cword split - _init_completion -s || return 0 + local cur prev words cword split=false + if declare -F _init_completion >/dev/null 2>&1; then + _init_completion -s || return 0 + else + __bat_init_completion -n "=" || return 0 + _split_longopt && split=true + fi if [[ ${words[1]-} == cache ]]; then case $prev in