Skip to content

Commit

Permalink
Must do escaping earlier
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Khouzam <[email protected]>
  • Loading branch information
marckhouzam committed Jan 18, 2025
1 parent 74a2762 commit 3fb6896
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions bash_completionsV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,13 @@ __%[1]s_handle_completion_types() {
[[ -z $comp ]] && continue
# Strip any description
comp=${comp%%%%$tab*}
# Escape the completion in case of special characters
printf -v comp "%%q" "${comp}"
# Only consider the completions that match
if [[ $comp == "$cur"* ]]; then
COMPREPLY+=( "$(printf %%q "$comp")" )
COMPREPLY+=("$comp")
fi
done < <(printf "%%s\n" "${completions[@]}")
;;
Expand All @@ -245,40 +249,41 @@ __%[1]s_handle_completion_types() {
__%[1]s_handle_standard_completion_case() {
local tab=$'\t' comp
# Short circuit to optimize if we don't have descriptions
if [[ "${completions[*]}" != *$tab* ]]; then
local compgen_words=$(printf "%%s\n" "${completions[@]}")
IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n' compgen -W "${compgen_words}" -- "$cur")
# If there is a single completion left, escape the completion
if ((${#COMPREPLY[*]} == 1)); then
COMPREPLY[0]=$(printf %%q "${COMPREPLY[0]}")
fi
return 0
fi
local longest=0
local compline
# Look for the longest completion so that we can format things nicely
while IFS='' read -r compline; do
[[ -z $compline ]] && continue
# Strip any description before checking the length
comp=${compline%%%%$tab*}
# Escape the completion (but not its description) in case of special characters
printf -v comp "%%q" "${comp}"
# Only consider the completions that match
[[ $comp == "$cur"* ]] || continue
COMPREPLY+=("$compline")
# Get the longest completion before adding back the description
if ((${#comp}>longest)); then
longest=${#comp}
fi
# If there is a description insert it back with the escaped completion
if [[ "${compline}" == *$tab* ]]; then
comp="$comp$tab${compline#*$tab}"
fi
__%[1]s_debug "Adding ${comp}"
COMPREPLY+=("$comp")
done < <(printf "%%s\n" "${completions[@]}")
# If there is a single completion left, remove the description text and escape the completion
# If there is a single completion left, remove the description text
if ((${#COMPREPLY[*]} == 1)); then
__%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
comp="${COMPREPLY[0]%%%%$tab*}"
__%[1]s_debug "Removed description from single completion, which is now: ${comp}"
COMPREPLY[0]=$(printf %%q "${comp}")
COMPREPLY[0]="${comp}"
else # Format the descriptions
__%[1]s_format_comp_descriptions $longest
fi
Expand Down

0 comments on commit 3fb6896

Please sign in to comment.