Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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 f464d6c82e9af74b7a46301a775163984af32cd1, 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.

authored by Ville Skyttä · committed by GitHub · parent 1424b7b · Browse files · 1 file+25 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+25 added−26 removed

  • other:+25 added−26 removed

1 file+25 added−26 removed

modified:bash_completionsV2.go+25 added-26 removed
36 unmodified lines
37 WriteStringAndCheck(buf, fmt.Sprintf(`# bash completion V2 for %-36[1]s -*- shell-script -*-3839__%[1]s_debug()40{41 if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then41 if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then42 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"43 fi44}45
18 unmodified lines
64 lastParam=${words[$((${#words[@]}-1))]}65 lastChar=${lastParam:$((${#lastParam}-1)):1}66 __%[1]s_debug "lastParam ${lastParam}, lastChar ${lastChar}"6768 if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then68 if [[ -z ${cur} && ${lastChar} != = ]]; then69 # If the last parameter is complete (there is a space following it)70 # We add an extra empty parameter so we can indicate this to the go method.71 __%[1]s_debug "Adding extra empty parameter"72 requestComp="${requestComp} ''"
1 unmodified line
7475 # When completing a flag with an = (e.g., %[1]s -n=<TAB>)76 # bash focuses on the part after the =, so we need to remove77 # the flag part from $cur78 if [[ "${cur}" == -*=* ]]; then78 if [[ ${cur} == -*=* ]]; then79 cur="${cur#*=}"80 fi8182 __%[1]s_debug "Calling ${requestComp}"
3 unmodified lines
86 # Extract the directive integer at the very end of the output following a colon (:)87 directive=${out##*:}88 # Remove the directive89 out=${out%%:*}90 if [ "${directive}" = "${out}" ]; then90 if [[ ${directive} == "${out}" ]]; then91 # There is not directive specified92 directive=093 fi94 __%[1]s_debug "The completion directive is: ${directive}"
6 unmodified lines
101 local shellCompDirectiveNoFileComp=%[5]d102 local shellCompDirectiveFilterFileExt=%[6]d103 local shellCompDirectiveFilterDirs=%[7]d104105 if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then105 if (((directive & shellCompDirectiveError) != 0)); then106 # Error code. No completion.107 __%[1]s_debug "Received error from custom completion go code"108 return109 else110 if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then111 if [[ $(type -t compopt) = "builtin" ]]; then110 if (((directive & shellCompDirectiveNoSpace) != 0)); then111 if [[ $(type -t compopt) == builtin ]]; then112 __%[1]s_debug "Activating no space"113 compopt -o nospace114 else115 __%[1]s_debug "No space directive not supported in this version of bash"116 fi117 fi118 if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then119 if [[ $(type -t compopt) = "builtin" ]]; then118 if (((directive & shellCompDirectiveNoFileComp) != 0)); then119 if [[ $(type -t compopt) == builtin ]]; then120 __%[1]s_debug "Activating no file completion"121 compopt +o default122 else123 __%[1]s_debug "No file completion directive not supported in this version of bash"
5 unmodified lines
129 local completions=()130 local activeHelp=()131 __%[1]s_extract_activeHelp132133 if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then133 if (((directive & shellCompDirectiveFilterFileExt) != 0)); then134 # File extension filtering135 local fullFilter filter filteringCmd136137 # Do not use quotes around the $completions variable or else newline
4 unmodified lines
142143 filteringCmd="_filedir $fullFilter"144 __%[1]s_debug "File filtering command: $filteringCmd"145 $filteringCmd146 elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then146 elif (((directive & shellCompDirectiveFilterDirs) != 0)); then147 # File completion for directories only148149 # Use printf to strip any trailing newline149 local subdir151 subdir=$(printf "%%s" "${completions[0]}")152 if [ -n "$subdir" ]; then150 subdir=${completions[0]}151 if [[ -n $subdir ]]; then152 __%[1]s_debug "Listing directories in $subdir"153 pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return154 else155 __%[1]s_debug "Listing directories in ."
6 unmodified lines
162 __%[1]s_handle_special_char "$cur" :163 __%[1]s_handle_special_char "$cur" =164165 # Print the activeHelp statements before we finish167 if [ ${#activeHelp[*]} -ne 0 ]; then166 if ((${#activeHelp[*]} != 0)); then167 printf "\n";168 printf "%%s\n" "${activeHelp[@]}"169 printf "\n"170
15 unmodified lines
186 local activeHelpMarker="%[8]s"187 local endIndex=${#activeHelpMarker}188189 while IFS='' read -r comp; do191 if [ "${comp:0:endIndex}" = "$activeHelpMarker" ]; then190 if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then191 comp=${comp:endIndex}192 __%[1]s_debug "ActiveHelp found: $comp"194 if [ -n "$comp" ]; then193 if [[ -n $comp ]]; then194 activeHelp+=("$comp")195 fi196 else197 # Not an activeHelp line but a normal completion198 completions+=("$comp")199 fi201 done < <(printf "%%s\n" "${out}")200 done <<<"${out}"201}202203__%[1]s_handle_completion_types() {204 __%[1]s_debug "__%[1]s_handle_completion_types: COMP_TYPE is $COMP_TYPE"
47 unmodified lines
252 fi253 done < <(printf "%%s\n" "${completions[@]}")254255 # If there is a single completion left, remove the description text257 if [ ${#COMPREPLY[*]} -eq 1 ]; then256 if ((${#COMPREPLY[*]} == 1)); then257 __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"258 comp="${COMPREPLY[0]%%%%$tab*}"259 __%[1]s_debug "Removed description from single completion, which is now: ${comp}"260 COMPREPLY[0]=$comp
8 unmodified lines
269 local char=$2270 if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then271 local word=${comp%%"${comp##*${char}}"}272 local idx=${#COMPREPLY[*]}274 while [[ $((--idx)) -ge 0 ]]; do275 COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"}273 while ((--idx >= 0)); do274 COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}275 done276 fi277}278
17 unmodified lines
296 maxdesclength=$(( COLUMNS - longest - 4 ))297298 # Make sure we can fit a description of at least 8 characters299 # if we are to align the descriptions.301 if [[ $maxdesclength -gt 8 ]]; then300 if ((maxdesclength > 8)); then301 # Add the proper number of spaces to align the descriptions302 for ((i = ${#comp} ; i < longest ; i++)); do303 comp+=" "304 done
3 unmodified lines
308 fi309310 # If there is enough space for any description text,311 # truncate the descriptions that are too long for the shell width313 if [ $maxdesclength -gt 0 ]; then314 if [ ${#desc} -gt $maxdesclength ]; then312 if ((maxdesclength > 0)); then313 if ((${#desc} > maxdesclength)); then314 desc=${desc:0:$(( maxdesclength - 1 ))}315 desc+="…"316 fi317 comp+=" ($desc)"
12 unmodified lines
330331 # Call _init_completion from the bash-completion package332 # to prepare the arguments properly333 if declare -F _init_completion >/dev/null 2>&1; then335 _init_completion -n "=:" || return334 _init_completion -n =: || return335 else337 __%[1]s_init_completion -n "=:" || return336 __%[1]s_init_completion -n =: || return337 fi338339 __%[1]s_debug340 __%[1]s_debug "========= starting completion logic =========="
42 unmodified lines