Commit graph

19 commits

Author SHA1 Message Date
Marc Khouzam
15823e76b9 Print ActiveHelp for bash along other completions
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>
2023-11-23 09:47:28 -05:00
Taavi Väänänen
2246fa82e9
Fix grammar: 'allows to' (#1978)
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>
2023-06-13 11:12:49 -04:00
Unai Martinez-Corral
9e6b58afc7
update copyright year (#1927) 2023-03-05 21:28:31 -05:00
Gyanendra Mishra
3daa4b9c36
Add keeporder to shell completion (#1903)
This allows programs to request the shell to maintain the order of completions that was returned by the program
2023-02-25 15:57:12 -05:00
Ville Skyttä
badcce14f8
style(bash-v2): various cleanups (#1702)
* 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.
2022-10-17 15:24:27 -04:00
Unai Martinez-Corral
6d978a911e
add missing license headers (#1809) 2022-09-16 07:55:56 -04:00
Ville Skyttä
f911c0b89c
fix(bash-v2): activeHelp length check syntax (#1762)
```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.
2022-08-13 17:44:47 -04:00
Marc Khouzam
7c9831d376
Fix handling of descriptions for bash v3 (#1735)
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>
2022-06-20 00:11:21 -04:00
Marc Khouzam
f464d6c82e
Add Active Help support (#1482) 2022-06-15 20:08:16 -04:00
Ville Skyttä
5b11656e45
perf(bash-v2): read directly to COMPREPLY on descriptionless short circuit (#1700)
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.
2022-05-17 20:57:08 -04:00
Ville Skyttä
e1ded5cd94
fix(bash-v2): skip empty completions when filtering descriptions (#1691)
`read` gives a last null value following a trailing newline.

Regression from fb8031162c.
2022-05-17 13:47:47 -04:00
Ville Skyttä
9e88759b19
perf(bash-v2): speed up filtering menu-complete descriptions (#1692)
Similarly as fb8031162c (+ the empty entry
fix) did for the "regular", non-menu completion cases.
2022-05-04 16:23:02 -04:00
Ville Skyttä
fb8031162c
perf(bash-v2): speed up filtering entries with descriptions (#1689)
Use simple prefix match instead of single word `compgen -W` command
substitution for each candidate match.
2022-05-03 21:34:55 -04:00
Ville Skyttä
95d7df1858
perf(bash-v2): short-circuit descriptionless candidate lists (#1686)
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.
2022-05-03 21:16:07 -04:00
Ville Skyttä
09d6ba690f
perf(bash-v2): standard completion optimizations (#1683)
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.
2022-05-02 21:00:51 -04:00
Ville Skyttä
4f0facbcee
style(bash-v2): out is not an array variable, do not refer to it as such (#1681)
Even though this to my surprise works, it doesn't accomplish anything
but some confusion. Remove it.
2022-04-27 22:27:52 -04:00
Ville Skyttä
2722a75ba3
perf(bash-v2): use backslash escape string expansion for tab (#1682)
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.
2022-04-27 22:02:30 -04:00
Marc Khouzam
3fed3ef5ad
Support different bash completion options (#1509)
https://github.com/spf13/cobra/issues/1508

Based on the documentation found here
https://www.gnu.org/software/bash/manual/html_node/Commands-For-Completion.html
we remove descriptions for the following completion types:
- menu-complete
- menu-complete-backward
- insert-completions

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2021-12-07 15:59:41 -07:00
Marc Khouzam
b36196066e
Bash completion V2 with completion descriptions (#1146)
* 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>
2021-06-30 15:24:58 -06:00