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 <marc.khouzam@gmail.com>
The use in generated bash completion files is getting flagged by
Lintian (the Debian package linting tool).
Signed-off-by: Taavi Väänänen <hi@taavi.wtf>
* use arithmetic evaluation in numeric context
* remove unnecessary $ from array index variables
* [[ ]] over [ ], == over =, remove unnecessary quoting
* use ${foo-} rather than ${foo:-} in emptiness check
The result of the expansion is null no matter if the variable is unset
or null in both cases; the former form is arguably easier on the eye.
* remove unnecessary trailing linefeed removal
No longer needed as of f464d6c82e, saves
a subshell.
* use herestring in activehelp extraction
Herestrings read cleaner than process substitutions, and work in posix
mode (but we do and will have some process substitutions so this doesn't
matter much). Both approaches may end up using temporary files.
```shell
$ set -u
$ foo=()
$ echo ${#foo}
bash: foo: unbound variable
echo ${#foo[*]}
0
```
The above shows that an empty array needs the suffix `[*]` when checking its length, or else it is considered unbound.
Fixes#1734
Tab characters that introduce completion descriptions weren't properly
being handled with bash v3. This change fixes that.
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
Not that it'd really matter that much performancewise given the level we
are at for this case, but this change makes the short circuit roughly
twice as fast on my box as it was for the 1000 rounds done in
marckhouzam/cobra-completion-testing.
Perhaps more importantly, this makes the code arguably slightly cleaner.
If the list of candidates has no descriptions, short circuit all the
description processing logic, basically just do a `compgen -W` for the
whole list and be done with it.
We could conceivably do some optimizations like this and more when
generating the completions with `--no-descriptions` in Go code, by
omitting some parts we know won't be needed, or doing some things
differently. But doing it this way in bash, the improvements are
available also to completions generated with descriptions enabled when
they are invoked for completion cases that produce no
descriptions. The result after this for descriptionless entries seems
fast enough so it seems there's no immediate need to look into doing
that.
Refactor to remove two loops over the entire list of candidates.
Format descriptions only for completions that are actually going to be
displayed, instead of for all candidates.
Format descriptions inline in completions array, removing need for a
command substitution/subshell and a printf escape per displayed
completion.
Using a command substitution, i.e. a subshell, with `printf` is
expensive for this purpose. For example `__*_format_comp_descriptions`
is run once for each completion candidate; the expense adds up and
shows when there are a lot of them.
* Bash completion v2
This v2 version of bash completion is based on Go completions.
It also supports descriptions like the other shells.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Only consider matching completions for formatting
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Use bash compV2 for the default completion command
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Update comments that still referred to bash completion
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>