mirror of
https://github.com/sharkdp/bat.git
synced 2025-10-25 05:03:59 +01:00
Merge pull request #3448 from akinomyoga/bash-fullquote
Use `-o fullquote` and `-o noquote` for more robust escaping in Bash completions
This commit is contained in:
@@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
## Other
|
## Other
|
||||||
|
|
||||||
|
- Use more robust approach to escaping in Bash completions, see #3448 (@akinomyoga)
|
||||||
|
|
||||||
## Syntaxes
|
## Syntaxes
|
||||||
|
|
||||||
## Themes
|
## Themes
|
||||||
|
|||||||
42
assets/completions/bat.bash.in
vendored
42
assets/completions/bat.bash.in
vendored
@@ -14,18 +14,36 @@ __bat_escape_completions()
|
|||||||
{
|
{
|
||||||
# Do not escape if completing a quoted value.
|
# Do not escape if completing a quoted value.
|
||||||
[[ $cur == [\"\']* ]] && return 0
|
[[ $cur == [\"\']* ]] && return 0
|
||||||
# printf -v to an array index is available in bash >= 4.1.
|
|
||||||
# Use it if available, as -o filenames is semantically incorrect if
|
|
||||||
# we are not actually completing filenames, and it has side effects
|
|
||||||
# (e.g. adds trailing slash to candidates matching present dirs).
|
|
||||||
if ((
|
if ((
|
||||||
|
BASH_VERSINFO[0] > 5 || \
|
||||||
|
BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3
|
||||||
|
)); then
|
||||||
|
# bash >= 5.3 has "compopt -o fullquote", which exactly does
|
||||||
|
# what this function tries to do.
|
||||||
|
compopt -o fullquote
|
||||||
|
elif ((
|
||||||
BASH_VERSINFO[0] > 4 || \
|
BASH_VERSINFO[0] > 4 || \
|
||||||
BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] > 0
|
BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] > 0
|
||||||
)); then
|
)); then
|
||||||
|
# printf -v to an array index is available in bash >= 4.1.
|
||||||
|
# Use it if available, as -o filenames is semantically
|
||||||
|
# incorrect if we are not actually completing filenames, and it
|
||||||
|
# has side effects (e.g. adds trailing slash to candidates
|
||||||
|
# matching present dirs).
|
||||||
local i
|
local i
|
||||||
for i in ${!COMPREPLY[*]}; do
|
for i in ${!COMPREPLY[*]}; do
|
||||||
printf -v "COMPREPLY[i]" %q "${COMPREPLY[i]}"
|
printf -v "COMPREPLY[i]" %q "${COMPREPLY[i]}"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# We can use "compopt -o noquote" available in bash >= 4.3 to
|
||||||
|
# prevent further quoting by the shell, which would be
|
||||||
|
# unexpectedly applied when a quoted result matches a filename.
|
||||||
|
if ((
|
||||||
|
BASH_VERSINFO[0] > 4 || \
|
||||||
|
BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 3
|
||||||
|
)); then
|
||||||
|
compopt -o noquote
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
compopt -o filenames
|
compopt -o filenames
|
||||||
fi
|
fi
|
||||||
@@ -66,7 +84,7 @@ _bat() {
|
|||||||
printf "%s\n" "$lang"
|
printf "%s\n" "$lang"
|
||||||
done
|
done
|
||||||
)" -- "$cur"))
|
)" -- "$cur"))
|
||||||
__bat_escape_completions
|
__bat_escape_completions
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
-H | --highlight-line | \
|
-H | --highlight-line | \
|
||||||
@@ -130,16 +148,16 @@ _bat() {
|
|||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
--theme)
|
--theme)
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
COMPREPLY=($(compgen -W "auto${IFS}auto:always${IFS}auto:system${IFS}dark${IFS}light${IFS}$("$1" --list-themes)" -- "$cur"))
|
COMPREPLY=($(compgen -W "auto${IFS}auto:always${IFS}auto:system${IFS}dark${IFS}light${IFS}$("$1" --list-themes)" -- "$cur"))
|
||||||
__bat_escape_completions
|
__bat_escape_completions
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
--theme-dark | \
|
--theme-dark | \
|
||||||
--theme-light)
|
--theme-light)
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
COMPREPLY=($(compgen -W "$("$1" --list-themes)" -- "$cur"))
|
COMPREPLY=($(compgen -W "$("$1" --list-themes)" -- "$cur"))
|
||||||
__bat_escape_completions
|
__bat_escape_completions
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
--style)
|
--style)
|
||||||
@@ -158,7 +176,7 @@ _bat() {
|
|||||||
numbers
|
numbers
|
||||||
snip
|
snip
|
||||||
)
|
)
|
||||||
# shellcheck disable=SC2016
|
# shellcheck disable=SC2016
|
||||||
if declare -F _comp_delimited >/dev/null 2>&1; then
|
if declare -F _comp_delimited >/dev/null 2>&1; then
|
||||||
# bash-completion > 2.11
|
# bash-completion > 2.11
|
||||||
_comp_delimited , -W '"${styles[@]}"'
|
_comp_delimited , -W '"${styles[@]}"'
|
||||||
|
|||||||
Reference in New Issue
Block a user