New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add similar whitespace escape logic to bash v2 completions than in other completions #1743
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -216,7 +216,7 @@ __%[1]s_handle_completion_types() { | |||||
comp=${comp%%%%$tab*} | ||||||
# Only consider the completions that match | ||||||
if [[ $comp == "$cur"* ]]; then | ||||||
COMPREPLY+=("$comp") | ||||||
COMPREPLY+=( "$(printf %%q "$comp")" ) | ||||||
fi | ||||||
done < <(printf "%%s\n" "${completions[@]}") | ||||||
;; | ||||||
|
@@ -233,7 +233,14 @@ __%[1]s_handle_standard_completion_case() { | |||||
|
||||||
# Short circuit to optimize if we don't have descriptions | ||||||
if [[ "${completions[*]}" != *$tab* ]]; then | ||||||
IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur") | ||||||
local compgen_words=$(printf "%%s\n" "${completions[@]}") | ||||||
IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n' compgen -W "${compgen_words}" -- "$cur") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to support other bash special characters, we need more quoting: local compgen_words="$(printf "%%q\n" "${completions[@]}")"
cur="$(printf "%%q" "${cur}")"
IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n' compgen -W "${compgen_words}" -- "${cur}")
Similarly, we need quoting up above: https://github.com/spf13/cobra/blob/main/bash_completionsV2.go#L62-L84 $ args=( echo "foo\>bar" )
$ eval "${args[*]}"
foo>bar
$ args=( echo "foo\>bar" )
$ eval "$(printf "%q " "${args[@]}")"
foo\>bar |
||||||
|
||||||
# If there is a single completion left, escape the completion | ||||||
if ((${#COMPREPLY[*]} == 1)); then | ||||||
COMPREPLY[0]=$(printf %%q "${COMPREPLY[0]}") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can eliminate a subshell here.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, actually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we try to keep it working with bash 3, as much as possible. |
||||||
fi | ||||||
|
||||||
return 0 | ||||||
fi | ||||||
|
||||||
|
@@ -252,12 +259,12 @@ __%[1]s_handle_standard_completion_case() { | |||||
fi | ||||||
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 and escape the completion | ||||||
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]=$comp | ||||||
COMPREPLY[0]=$(printf %%q "${comp}") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above
Suggested change
|
||||||
else # Format the descriptions | ||||||
__%[1]s_format_comp_descriptions $longest | ||||||
fi | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm reading cobra-completion-testing right, this line is the performance regression. We're creating a subshell to quote each individual completion
We could instead batch all of the completions up into another array, and then quote them all at once kinda like how we're doing for the input to this loop in the first place