Skip to content
Myr

Print ActiveHelp for bash along other completions (#2076)

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

In the bash shell we used to print ActiveHelp messages on every tab-press. In the example below, notice the "Command help" line which is ActiveHelp: bash-5.1$ tanzu context u[tab] Command help: Configure and manage contexts for the Tanzu CLI bash-5.1$ tanzu context u[tab] Command help: Configure and manage contexts for the Tanzu CLI bash-5.1$ tanzu context u unset (Unset the active context so that it is not used by default.) use (Set the context to be used by default) bash-5.1$ tanzu context u Above, on the first [tab] press, only the ActiveHelp is printed. On the second [tab] press, the ActiveHelp is printed again, followed by a re-print of the command-line, followed by the completions choices. The separation between ActiveHelp and completion choices makes the ActiveHelp harder to see. Furthermore, I find the double printing of the ActiveHelp string to look bad. Note that for zsh, the UX is different and that ActiveHelp messages are printed at the same time as the completion choices. This commit aligns the UX for ActiveHelp in bash with the one for zsh: if there are other completions to be shown, the ActiveHelp messages are printed at the same time. New behaviour: 1- ActiveHelp is no longer printed on the first [tab] press. This is better aligned with bash's standard approach. 2- ActiveHelp is printed on the second [tab] press, above the completion choices, with a `--` delimiter. 3- If there are no completion choices, the `--` delimiter is omitted. This behaviour is the same as what is done for zsh (except that for zsh the first [tab] press immediately shows completion choices). Below is the above example, but using this commit. Notice the more concise and easier to read completion output: bash-5.1$ tanzu context u[tab][tab] Command help: Configure and manage contexts for the Tanzu CLI -- unset (Unset the active context so that it is not used by default.) use (Set the context to be used by default) bash-5.1$ tanzu context u

authored by Marc Khouzam · committed by GitHub · parent 611e16c · Browse files · 1 file+62 added−11 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: Marc Khouzam <marc.khouzam@gmail.com>

Diff stats

1 file+62 added−11 removed

  • other:+62 added−11 removed

1 file+62 added−11 removed

modified:bash_completionsV2.go+62 added-11 removed
175 unmodified lines
176 __%[1]s_handle_special_char "$cur" :177 __%[1]s_handle_special_char "$cur" =178179 # Print the activeHelp statements before we finish180 __%[1]s_handle_activeHelp181}182183__%[1]s_handle_activeHelp() {184 # Print the activeHelp statements185 if ((${#activeHelp[*]} != 0)); then186 if [ -z $COMP_TYPE ]; then187 # Bash v3 does not set the COMP_TYPE variable.181 printf "\n";182 printf "%%s\n" "${activeHelp[@]}"183 printf "\n"188 printf "\n";189 printf "%%s\n" "${activeHelp[@]}"190 printf "\n"191 __%[1]s_reprint_commandLine192 return193 fi194185 # The prompt format is only available from bash 4.4.186 # We test if it is available before using it.187 if (x=${PS1@P}) 2> /dev/null; then188 printf "%%s" "${PS1@P}${COMP_LINE[@]}"189 else190 # Can't print the prompt. Just print the191 # text the user had typed, it is workable enough.192 printf "%%s" "${COMP_LINE[@]}"195 # Only print ActiveHelp on the second TAB press196 if [ $COMP_TYPE -eq 63 ]; then197 printf "\n"198 printf "%%s\n" "${activeHelp[@]}"199200 if ((${#COMPREPLY[*]} == 0)); then201 # When there are no completion choices from the program, file completion202 # may kick in if the program has not disabled it; in such a case, we want203 # to know if any files will match what the user typed, so that we know if204 # there will be completions presented, so that we know how to handle ActiveHelp.205 # To find out, we actually trigger the file completion ourselves;206 # the call to _filedir will fill COMPREPLY if files match.207 if (((directive & shellCompDirectiveNoFileComp) == 0)); then208 __%[1]s_debug "Listing files"209 _filedir210 fi211 fi212213 if ((${#COMPREPLY[*]} != 0)); then214 # If there are completion choices to be shown, print a delimiter.215 # Re-printing the command-line will automatically be done216 # by the shell when it prints the completion choices.217 printf -- "--"218 else219 # When there are no completion choices at all, we need220 # to re-print the command-line since the shell will221 # not be doing it itself.222 __%[1]s_reprint_commandLine223 fi224 elif [ $COMP_TYPE -eq 37 ] || [ $COMP_TYPE -eq 42 ]; then225 # For completion type: menu-complete/menu-complete-backward and insert-completions226 # the completions are immediately inserted into the command-line, so we first227 # print the activeHelp message and reprint the command-line since the shell won't.228 printf "\n"229 printf "%%s\n" "${activeHelp[@]}"230231 __%[1]s_reprint_commandLine232 fi233 fi234}235236__%[1]s_reprint_commandLine() {237 # The prompt format is only available from bash 4.4.238 # We test if it is available before using it.239 if (x=${PS1@P}) 2> /dev/null; then240 printf "%%s" "${PS1@P}${COMP_LINE[@]}"241 else242 # Can't print the prompt. Just print the243 # text the user had typed, it is workable enough.244 printf "%%s" "${COMP_LINE[@]}"245 fi246}247248# Separate activeHelp lines from real completions.249# Fills the $activeHelp and $completions arrays.250__%[1]s_extract_activeHelp() {251 local activeHelpMarker="%[9]s"
233 unmodified lines