Automatically escape all special characters as soon as completion would print them to the command line (COMP_TYPE=9)

This commit is contained in:
Jeffrey Faer 2024-12-30 14:27:30 -07:00
parent 7a9725b587
commit 2fc6dad66e

View file

@ -59,18 +59,19 @@ __%[1]s_get_completion_results() {
# Prepare the command to request completions for the program. # Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly %[1]s allows handling aliases # Calling ${words[0]} instead of directly %[1]s allows handling aliases
args=("${words[@]:1}") args=("${words[@]:1}")
requestComp="${words[0]} %[2]s" requestComp="${words[0]} %[2]s ${args[*]}"
if [[ "${#args[@]}" -gt 0 ]]; then
# Previous args should already be escaped...
requestComp+=" ${args[*]::${#args[@]}-1}"
# ...but the current arg might not yet be escaped.
requestComp+=" $(printf "%%q" "${args[${#args[@]}-1]}")"
fi
lastParam=${words[$((${#words[@]}-1))]} lastParam=${words[$((${#words[@]}-1))]}
lastChar=${lastParam:$((${#lastParam}-1)):1} lastChar=${lastParam:$((${#lastParam}-1)):1}
__%[1]s_debug "lastParam ${lastParam}, lastChar ${lastChar}" __%[1]s_debug "lastParam ${lastParam}, lastChar ${lastChar}"
if [[ -z ${cur} && ${lastChar} != = ]]; then
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method.
__%[1]s_debug "Adding extra empty parameter"
requestComp="${requestComp} ''"
fi
# When completing a flag with an = (e.g., %[1]s -n=<TAB>) # When completing a flag with an = (e.g., %[1]s -n=<TAB>)
# bash focuses on the part after the =, so we need to remove # bash focuses on the part after the =, so we need to remove
# the flag part from $cur # the flag part from $cur
@ -234,18 +235,18 @@ __%[1]s_handle_completion_types() {
fi fi
done done
IFS=$'\n' read -ra COMPREPLY -d '' < <(printf "%%q\n" "${COMPREPLY[@]}") __%[1]s_escape_compreply
;;
63)
# Type: Listing completions after successive tabs
__%[1]s_handle_standard_completion_case
;; ;;
*) *)
# Type: complete (normal completion) # Type: complete (normal completion)
__%[1]s_handle_standard_completion_case __%[1]s_handle_standard_completion_case
__%[1]s_escape_compreply
# If there is a single completion left, escape the completion
if ((${#COMPREPLY[@]} == 1)); then
COMPREPLY[0]="$(printf "%%q" "${COMPREPLY[0]}")"
fi
;; ;;
esac esac
} }
@ -256,11 +257,12 @@ __%[1]s_handle_standard_completion_case() {
# Short circuit to optimize if we don't have descriptions # Short circuit to optimize if we don't have descriptions
if [[ "${completions[*]}" != *$tab* ]]; then if [[ "${completions[*]}" != *$tab* ]]; then
# compgen's -W option respects shell quoting, so we need to escape. # compgen's -W option respects shell quoting, so we need to escape.
local compgen_words="$(printf "%%q\n" "${completions[@]}")" local compgen_words="$(__%[1]s_escape_suggestions "${completions[@]}")"
# compgen appears to respect shell quoting _after_ checking whether if [[ "${BASH_VERSION}" = 3* ]]; then
# they have the right prefix, so we also need to quote cur. # bash3 compgen doesn't handle escaped # symbols correctly.
local compgen_cur="$(printf "%%q" "${cur}")" compgen_words="${compgen_words//\\#/#}"
IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${compgen_words}" -- "${compgen_cur}") fi
IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${compgen_words}" -- "${cur}")
return 0 return 0
fi fi
@ -290,10 +292,29 @@ __%[1]s_handle_standard_completion_case() {
fi fi
} }
__%[1]s_escape_compreply() {
IFS=$'\n' read -ra COMPREPLY -d '' < <(__%[1]s_escape_suggestions "${COMPREPLY[@]}")
}
__%[1]s_escape_suggestions() {
if (( $# == 0 )); then
return
fi
local suggestions=( "$@" )
IFS=$'\n' read -ra suggestions -d '' < <(printf "%%q\n" "${suggestions[@]}")
# Additionally escape # symbols.
local suggestion
for suggestion in "${suggestions[@]}"; do
echo "${suggestion//#/\\#}"
done
}
__%[1]s_handle_wordbreaks() __%[1]s_handle_wordbreaks()
{ {
if ((${#COMPREPLY[@]} == 0)); then if ((${#COMPREPLY[@]} == 0)); then
return; return
fi fi
local comp="$1" local comp="$1"
@ -364,21 +385,6 @@ __start_%[1]s()
COMPREPLY=() COMPREPLY=()
# Omit wordbreaks that would need to be escaped.
local wordbreaks i
for ((i=0; i < ${#COMP_WORDBREAKS}; i++)); do
local char="${COMP_WORDBREAKS:$i:1}"
if [[ $'\n\t ' == *"${char}"* ]]; then
wordbreaks+="${char}"
continue
fi
if [[ "${char}" == "$(printf "%%q" "${char}")" ]]; then
wordbreaks+="${char}"
continue
fi
done
COMP_WORDBREAKS="${wordbreaks}"
# Call _init_completion from the bash-completion package # Call _init_completion from the bash-completion package
# to prepare the arguments properly # to prepare the arguments properly
if declare -F _init_completion >/dev/null 2>&1; then if declare -F _init_completion >/dev/null 2>&1; then