Skip to content

Commit 41b26ec

Browse files
authored
Print ActiveHelp for bash along other completions (#2076)
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 Signed-off-by: Marc Khouzam <[email protected]>
1 parent 611e16c commit 41b26ec

File tree

1 file changed

+63
-12
lines changed

1 file changed

+63
-12
lines changed

bash_completionsV2.go

+63-12
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,74 @@ __%[1]s_process_completion_results() {
177177
__%[1]s_handle_special_char "$cur" =
178178
179179
# Print the activeHelp statements before we finish
180+
__%[1]s_handle_activeHelp
181+
}
182+
183+
__%[1]s_handle_activeHelp() {
184+
# Print the activeHelp statements
180185
if ((${#activeHelp[*]} != 0)); then
181-
printf "\n";
182-
printf "%%s\n" "${activeHelp[@]}"
183-
printf "\n"
184-
185-
# 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; then
188-
printf "%%s" "${PS1@P}${COMP_LINE[@]}"
189-
else
190-
# Can't print the prompt. Just print the
191-
# text the user had typed, it is workable enough.
192-
printf "%%s" "${COMP_LINE[@]}"
186+
if [ -z $COMP_TYPE ]; then
187+
# Bash v3 does not set the COMP_TYPE variable.
188+
printf "\n";
189+
printf "%%s\n" "${activeHelp[@]}"
190+
printf "\n"
191+
__%[1]s_reprint_commandLine
192+
return
193+
fi
194+
195+
# Only print ActiveHelp on the second TAB press
196+
if [ $COMP_TYPE -eq 63 ]; then
197+
printf "\n"
198+
printf "%%s\n" "${activeHelp[@]}"
199+
200+
if ((${#COMPREPLY[*]} == 0)); then
201+
# When there are no completion choices from the program, file completion
202+
# may kick in if the program has not disabled it; in such a case, we want
203+
# to know if any files will match what the user typed, so that we know if
204+
# 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)); then
208+
__%[1]s_debug "Listing files"
209+
_filedir
210+
fi
211+
fi
212+
213+
if ((${#COMPREPLY[*]} != 0)); then
214+
# If there are completion choices to be shown, print a delimiter.
215+
# Re-printing the command-line will automatically be done
216+
# by the shell when it prints the completion choices.
217+
printf -- "--"
218+
else
219+
# When there are no completion choices at all, we need
220+
# to re-print the command-line since the shell will
221+
# not be doing it itself.
222+
__%[1]s_reprint_commandLine
223+
fi
224+
elif [ $COMP_TYPE -eq 37 ] || [ $COMP_TYPE -eq 42 ]; then
225+
# For completion type: menu-complete/menu-complete-backward and insert-completions
226+
# the completions are immediately inserted into the command-line, so we first
227+
# print the activeHelp message and reprint the command-line since the shell won't.
228+
printf "\n"
229+
printf "%%s\n" "${activeHelp[@]}"
230+
231+
__%[1]s_reprint_commandLine
193232
fi
194233
fi
195234
}
196235
236+
__%[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; then
240+
printf "%%s" "${PS1@P}${COMP_LINE[@]}"
241+
else
242+
# Can't print the prompt. Just print the
243+
# text the user had typed, it is workable enough.
244+
printf "%%s" "${COMP_LINE[@]}"
245+
fi
246+
}
247+
197248
# Separate activeHelp lines from real completions.
198249
# Fills the $activeHelp and $completions arrays.
199250
__%[1]s_extract_activeHelp() {

0 commit comments

Comments
 (0)