Commit Graph

15 Commits

Author SHA1 Message Date
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