style(bash-v2): various cleanups (#1702)

* use arithmetic evaluation in numeric context

* remove unnecessary $ from array index variables

* [[ ]] over [ ], == over =, remove unnecessary quoting

* use ${foo-} rather than ${foo:-} in emptiness check

The result of the expansion is null no matter if the variable is unset
or null in both cases; the former form is arguably easier on the eye.

* remove unnecessary trailing linefeed removal

No longer needed as of f464d6c82e, saves
a subshell.

* use herestring in activehelp extraction

Herestrings read cleaner than process substitutions, and work in posix
mode (but we do and will have some process substitutions so this doesn't
matter much). Both approaches may end up using temporary files.
This commit is contained in:
Ville Skyttä 2022-10-17 22:24:27 +03:00 committed by GitHub
parent 1424b7b927
commit badcce14f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -38,7 +38,7 @@ func genBashComp(buf io.StringWriter, name string, includeDesc bool) {
__%[1]s_debug() __%[1]s_debug()
{ {
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
echo "$*" >> "${BASH_COMP_DEBUG_FILE}" echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
fi fi
} }
@ -65,7 +65,7 @@ __%[1]s_get_completion_results() {
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 [[ -z ${cur} && ${lastChar} != = ]]; then
# If the last parameter is complete (there is a space following it) # 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. # We add an extra empty parameter so we can indicate this to the go method.
__%[1]s_debug "Adding extra empty parameter" __%[1]s_debug "Adding extra empty parameter"
@ -75,7 +75,7 @@ __%[1]s_get_completion_results() {
# 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
if [[ "${cur}" == -*=* ]]; then if [[ ${cur} == -*=* ]]; then
cur="${cur#*=}" cur="${cur#*=}"
fi fi
@ -87,7 +87,7 @@ __%[1]s_get_completion_results() {
directive=${out##*:} directive=${out##*:}
# Remove the directive # Remove the directive
out=${out%%:*} out=${out%%:*}
if [ "${directive}" = "${out}" ]; then if [[ ${directive} == "${out}" ]]; then
# There is not directive specified # There is not directive specified
directive=0 directive=0
fi fi
@ -102,21 +102,21 @@ __%[1]s_process_completion_results() {
local shellCompDirectiveFilterFileExt=%[6]d local shellCompDirectiveFilterFileExt=%[6]d
local shellCompDirectiveFilterDirs=%[7]d local shellCompDirectiveFilterDirs=%[7]d
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then if (((directive & shellCompDirectiveError) != 0)); then
# Error code. No completion. # Error code. No completion.
__%[1]s_debug "Received error from custom completion go code" __%[1]s_debug "Received error from custom completion go code"
return return
else else
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then if (((directive & shellCompDirectiveNoSpace) != 0)); then
if [[ $(type -t compopt) = "builtin" ]]; then if [[ $(type -t compopt) == builtin ]]; then
__%[1]s_debug "Activating no space" __%[1]s_debug "Activating no space"
compopt -o nospace compopt -o nospace
else else
__%[1]s_debug "No space directive not supported in this version of bash" __%[1]s_debug "No space directive not supported in this version of bash"
fi fi
fi fi
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then if (((directive & shellCompDirectiveNoFileComp) != 0)); then
if [[ $(type -t compopt) = "builtin" ]]; then if [[ $(type -t compopt) == builtin ]]; then
__%[1]s_debug "Activating no file completion" __%[1]s_debug "Activating no file completion"
compopt +o default compopt +o default
else else
@ -130,7 +130,7 @@ __%[1]s_process_completion_results() {
local activeHelp=() local activeHelp=()
__%[1]s_extract_activeHelp __%[1]s_extract_activeHelp
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then if (((directive & shellCompDirectiveFilterFileExt) != 0)); then
# File extension filtering # File extension filtering
local fullFilter filter filteringCmd local fullFilter filter filteringCmd
@ -143,13 +143,12 @@ __%[1]s_process_completion_results() {
filteringCmd="_filedir $fullFilter" filteringCmd="_filedir $fullFilter"
__%[1]s_debug "File filtering command: $filteringCmd" __%[1]s_debug "File filtering command: $filteringCmd"
$filteringCmd $filteringCmd
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then elif (((directive & shellCompDirectiveFilterDirs) != 0)); then
# File completion for directories only # File completion for directories only
# Use printf to strip any trailing newline
local subdir local subdir
subdir=$(printf "%%s" "${completions[0]}") subdir=${completions[0]}
if [ -n "$subdir" ]; then if [[ -n $subdir ]]; then
__%[1]s_debug "Listing directories in $subdir" __%[1]s_debug "Listing directories in $subdir"
pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
else else
@ -164,7 +163,7 @@ __%[1]s_process_completion_results() {
__%[1]s_handle_special_char "$cur" = __%[1]s_handle_special_char "$cur" =
# Print the activeHelp statements before we finish # Print the activeHelp statements before we finish
if [ ${#activeHelp[*]} -ne 0 ]; then if ((${#activeHelp[*]} != 0)); then
printf "\n"; printf "\n";
printf "%%s\n" "${activeHelp[@]}" printf "%%s\n" "${activeHelp[@]}"
printf "\n" printf "\n"
@ -188,17 +187,17 @@ __%[1]s_extract_activeHelp() {
local endIndex=${#activeHelpMarker} local endIndex=${#activeHelpMarker}
while IFS='' read -r comp; do while IFS='' read -r comp; do
if [ "${comp:0:endIndex}" = "$activeHelpMarker" ]; then if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then
comp=${comp:endIndex} comp=${comp:endIndex}
__%[1]s_debug "ActiveHelp found: $comp" __%[1]s_debug "ActiveHelp found: $comp"
if [ -n "$comp" ]; then if [[ -n $comp ]]; then
activeHelp+=("$comp") activeHelp+=("$comp")
fi fi
else else
# Not an activeHelp line but a normal completion # Not an activeHelp line but a normal completion
completions+=("$comp") completions+=("$comp")
fi fi
done < <(printf "%%s\n" "${out}") done <<<"${out}"
} }
__%[1]s_handle_completion_types() { __%[1]s_handle_completion_types() {
@ -254,7 +253,7 @@ __%[1]s_handle_standard_completion_case() {
done < <(printf "%%s\n" "${completions[@]}") done < <(printf "%%s\n" "${completions[@]}")
# If there is a single completion left, remove the description text # If there is a single completion left, remove the description text
if [ ${#COMPREPLY[*]} -eq 1 ]; then if ((${#COMPREPLY[*]} == 1)); then
__%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}" __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
comp="${COMPREPLY[0]%%%%$tab*}" comp="${COMPREPLY[0]%%%%$tab*}"
__%[1]s_debug "Removed description from single completion, which is now: ${comp}" __%[1]s_debug "Removed description from single completion, which is now: ${comp}"
@ -271,8 +270,8 @@ __%[1]s_handle_special_char()
if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
local word=${comp%%"${comp##*${char}}"} local word=${comp%%"${comp##*${char}}"}
local idx=${#COMPREPLY[*]} local idx=${#COMPREPLY[*]}
while [[ $((--idx)) -ge 0 ]]; do while ((--idx >= 0)); do
COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"} COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}
done done
fi fi
} }
@ -298,7 +297,7 @@ __%[1]s_format_comp_descriptions()
# Make sure we can fit a description of at least 8 characters # Make sure we can fit a description of at least 8 characters
# if we are to align the descriptions. # if we are to align the descriptions.
if [[ $maxdesclength -gt 8 ]]; then if ((maxdesclength > 8)); then
# Add the proper number of spaces to align the descriptions # Add the proper number of spaces to align the descriptions
for ((i = ${#comp} ; i < longest ; i++)); do for ((i = ${#comp} ; i < longest ; i++)); do
comp+=" " comp+=" "
@ -310,8 +309,8 @@ __%[1]s_format_comp_descriptions()
# If there is enough space for any description text, # If there is enough space for any description text,
# truncate the descriptions that are too long for the shell width # truncate the descriptions that are too long for the shell width
if [ $maxdesclength -gt 0 ]; then if ((maxdesclength > 0)); then
if [ ${#desc} -gt $maxdesclength ]; then if ((${#desc} > maxdesclength)); then
desc=${desc:0:$(( maxdesclength - 1 ))} desc=${desc:0:$(( maxdesclength - 1 ))}
desc+="…" desc+="…"
fi fi
@ -332,9 +331,9 @@ __start_%[1]s()
# 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
_init_completion -n "=:" || return _init_completion -n =: || return
else else
__%[1]s_init_completion -n "=:" || return __%[1]s_init_completion -n =: || return
fi fi
__%[1]s_debug __%[1]s_debug