Add similar whitespace escape logic to bash v2 completions than in other completions (#1743)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Toni Kangas · committed by GitHub · parent 6c3c116 · Browse files · 1 file·+56 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Toni Kangas <toni.kangas@upcloud.com>
- Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
- Co-authored-by: Marc Khouzam <marc.khouzam@gmail.com>
- Co-authored-by: Jeffrey Faer <jeffrey.faer@gmail.com>
Diff stats
1 file+56 added−19 removed
- other:+56 added−19 removed
Provenance
- Commit
09d5664- Tree
7fa66fa- Author
Toni Kangas <kangasta@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
6c3c116
1 file changed+56 added−19 removed
modified:bash_completionsV2.go+56 added-19 removed
199 unmodified lines200 local activeHelpMarker="%[9]s"201 local endIndex=${#activeHelpMarker}202 203 while IFS='' read -r comp; do204+ [[ -z $comp ]] && continue205+206 if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then207 comp=${comp:endIndex}208 __%[1]s_debug "ActiveHelp found: $comp"209 if [[ -n $comp ]]; then14 unmodified lines224 # Type: menu-complete/menu-complete-backward and insert-completions225 # If the user requested inserting one completion at a time, or all226 # completions at once on the command-line we must remove the descriptions.227 # https://github.com/spf13/cobra/issues/1508226- local tab=$'\t' comp227- while IFS='' read -r comp; do228- [[ -z $comp ]] && continue229- # Strip any description230- comp=${comp%%%%$tab*}231- # Only consider the completions that match232- if [[ $comp == "$cur"* ]]; then233- COMPREPLY+=("$comp")234- fi235- done < <(printf "%%s\n" "${completions[@]}")228+229+ # If there are no completions, we don't need to do anything230+ (( ${#completions[@]} == 0 )) && return 0231+232+ local tab=$'\t'233+234+ # Strip any description and escape the completion to handled special characters235+ IFS=$'\n' read -ra completions -d '' < <(printf "%%q\n" "${completions[@]%%%%$tab*}")236+237+ # Only consider the completions that match238+ IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${completions[*]}" -- "${cur}")239+240+ # compgen looses the escaping so we need to escape all completions again since they will241+ # all be inserted on the command-line.242+ IFS=$'\n' read -ra COMPREPLY -d '' < <(printf "%%q\n" "${COMPREPLY[@]}")243 ;;244 245 *)246 # Type: complete (normal completion)2 unmodified lines249 esac250 }251 252 __%[1]s_handle_standard_completion_case() {246- local tab=$'\t' comp253+ local tab=$'\t'254 255+ # If there are no completions, we don't need to do anything256+ (( ${#completions[@]} == 0 )) && return 0257+258 # Short circuit to optimize if we don't have descriptions259 if [[ "${completions[*]}" != *$tab* ]]; then250- IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur")260+ # First, escape the completions to handle special characters261+ IFS=$'\n' read -ra completions -d '' < <(printf "%%q\n" "${completions[@]}")262+ # Only consider the completions that match what the user typed263+ IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${completions[*]}" -- "${cur}")264+265+ # compgen looses the escaping so, if there is only a single completion, we need to266+ # escape it again because it will be inserted on the command-line. If there are multiple267+ # completions, we don't want to escape them because they will be printed in a list268+ # and we don't want to show escape characters in that list.269+ if (( ${#COMPREPLY[@]} == 1 )); then270+ COMPREPLY[0]=$(printf "%%q" "${COMPREPLY[0]}")271+ fi272 return 0273 fi274 275 local longest=0276 local compline277 # Look for the longest completion so that we can format things nicely278 while IFS='' read -r compline; do279 [[ -z $compline ]] && continue259- # Strip any description before checking the length260- comp=${compline%%%%$tab*}280+281+ # Before checking if the completion matches what the user typed,282+ # we need to strip any description and escape the completion to handle special283+ # characters because those escape characters are part of what the user typed.284+ # Don't call "printf" in a sub-shell because it will be much slower285+ # since we are in a loop.286+ printf -v comp "%%q" "${compline%%%%$tab*}" &>/dev/null || comp=$(printf "%%q" "${compline%%%%$tab*}")287+288 # Only consider the completions that match289 [[ $comp == "$cur"* ]] || continue290+291+ # The completions matches. Add it to the list of full completions including292+ # its description. We don't escape the completion because it may get printed293+ # in a list if there are more than one and we don't want show escape characters294+ # in that list.295 COMPREPLY+=("$compline")296+297+ # Strip any description before checking the length, and again, don't escape298+ # the completion because this length is only used when printing the completions299+ # in a list and we don't want show escape characters in that list.300+ comp=${compline%%%%$tab*}301 if ((${#comp}>longest)); then302 longest=${#comp}303 fi304 done < <(printf "%%s\n" "${completions[@]}")305 269- # If there is a single completion left, remove the description text306+ # If there is a single completion left, remove the description text and escape any special characters307 if ((${#COMPREPLY[*]} == 1)); then308 __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"272- comp="${COMPREPLY[0]%%%%$tab*}"273- __%[1]s_debug "Removed description from single completion, which is now: ${comp}"274- COMPREPLY[0]=$comp275- else # Format the descriptions309+ COMPREPLY[0]=$(printf "%%q" "${COMPREPLY[0]%%%%$tab*}")310+ __%[1]s_debug "Removed description from single completion, which is now: ${COMPREPLY[0]}"311+ else312+ # Format the descriptions313 __%[1]s_format_comp_descriptions $longest314 fi315 }316 117 unmodified lines