perf(bash-v2): standard completion optimizations (#1683)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Refactor to remove two loops over the entire list of candidates. Format descriptions only for completions that are actually going to be displayed, instead of for all candidates. Format descriptions inline in completions array, removing need for a command substitution/subshell and a printf escape per displayed completion.
authored by Ville Skyttä · committed by GitHub · parent 4f0facb · Browse files · 1 file·+44 added−52 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+44 added−52 removed
- other:+44 added−52 removed
Provenance
- Commit
09d6ba6- Tree
5d5decf- Author
Ville Skyttä <ville.skytta@upcloud.com>- Committer
GitHub <noreply@github.com>- Parent
4f0facb
1 file changed+44 added−52 removed
modified:bash_completionsV2.go+44 added-52 removed
175 unmodified lines176 __%[1]s_handle_standard_completion_case() {177 local tab=$'\t' comp178 179 local longest=0180+ local compline181 # Look for the longest completion so that we can format things nicely181- while IFS='' read -r comp; do182+ while IFS='' read -r compline; do183 # Strip any description before checking the length183- comp=${comp%%%%$tab*}184+ comp=${compline%%%%$tab*}185 # Only consider the completions that match186 comp=$(compgen -W "$comp" -- "$cur")187+ [[ -z $comp ]] && continue188+ COMPREPLY+=("$compline")189 if ((${#comp}>longest)); then190 longest=${#comp}191 fi192 done < <(printf "%%s\n" "${out}")193 191- local completions=()192- while IFS='' read -r comp; do193- if [ -z "$comp" ]; then194- continue195- fi196-197- __%[1]s_debug "Original comp: $comp"198- comp="$(__%[1]s_format_comp_descriptions "$comp" "$longest")"199- __%[1]s_debug "Final comp: $comp"200- completions+=("$comp")201- done < <(printf "%%s\n" "${out}")202-203- while IFS='' read -r comp; do204- COMPREPLY+=("$comp")205- done < <(compgen -W "${completions[*]}" -- "$cur")206-194 # If there is a single completion left, remove the description text195 if [ ${#COMPREPLY[*]} -eq 1 ]; then196 __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"210- comp="${COMPREPLY[0]%%%% *}"197+ comp="${COMPREPLY[0]%%%%$tab*}"198 __%[1]s_debug "Removed description from single completion, which is now: ${comp}"212- COMPREPLY=()213- COMPREPLY+=("$comp")199+ COMPREPLY[0]=$comp200+ else # Format the descriptions201+ __%[1]s_format_comp_descriptions $longest202 fi203 }204 205 __%[1]s_handle_special_char()11 unmodified lines217 218 __%[1]s_format_comp_descriptions()219 {220 local tab=$'\t'233- local comp="$1"234- local longest=$2221+ local comp desc maxdesclength222+ local longest=$1223 236- # Properly format the description string which follows a tab character if there is one237- if [[ "$comp" == *$tab* ]]; then238- desc=${comp#*$tab}239- comp=${comp%%%%$tab*}224+ local i ci225+ for ci in ${!COMPREPLY[*]}; do226+ comp=${COMPREPLY[ci]}227+ # Properly format the description string which follows a tab character if there is one228+ if [[ "$comp" == *$tab* ]]; then229+ __%[1]s_debug "Original comp: $comp"230+ desc=${comp#*$tab}231+ comp=${comp%%%%$tab*}232 241- # $COLUMNS stores the current shell width.242- # Remove an extra 4 because we add 2 spaces and 2 parentheses.243- maxdesclength=$(( COLUMNS - longest - 4 ))233+ # $COLUMNS stores the current shell width.234+ # Remove an extra 4 because we add 2 spaces and 2 parentheses.235+ maxdesclength=$(( COLUMNS - longest - 4 ))236 245- # Make sure we can fit a description of at least 8 characters246- # if we are to align the descriptions.247- if [[ $maxdesclength -gt 8 ]]; then248- # Add the proper number of spaces to align the descriptions249- for ((i = ${#comp} ; i < longest ; i++)); do250- comp+=" "251- done252- else253- # Don't pad the descriptions so we can fit more text after the completion254- maxdesclength=$(( COLUMNS - ${#comp} - 4 ))255- fi237+ # Make sure we can fit a description of at least 8 characters238+ # if we are to align the descriptions.239+ if [[ $maxdesclength -gt 8 ]]; then240+ # Add the proper number of spaces to align the descriptions241+ for ((i = ${#comp} ; i < longest ; i++)); do242+ comp+=" "243+ done244+ else245+ # Don't pad the descriptions so we can fit more text after the completion246+ maxdesclength=$(( COLUMNS - ${#comp} - 4 ))247+ fi248 257- # If there is enough space for any description text,258- # truncate the descriptions that are too long for the shell width259- if [ $maxdesclength -gt 0 ]; then260- if [ ${#desc} -gt $maxdesclength ]; then261- desc=${desc:0:$(( maxdesclength - 1 ))}262- desc+="…"249+ # If there is enough space for any description text,250+ # truncate the descriptions that are too long for the shell width251+ if [ $maxdesclength -gt 0 ]; then252+ if [ ${#desc} -gt $maxdesclength ]; then253+ desc=${desc:0:$(( maxdesclength - 1 ))}254+ desc+="…"255+ fi256+ comp+=" ($desc)"257 fi264- comp+=" ($desc)"258+ COMPREPLY[ci]=$comp259+ __%[1]s_debug "Final comp: $comp"260 fi266- fi267-268- # Must use printf to escape all special characters269- printf "%%q" "${comp}"261+ done262 }263 264 __start_%[1]s()265 {55 unmodified lines