New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autocompletion in ZSH #168
Comments
@insign - autocompletion should wok in zsh in a similar way. Zsh needs to be configured to know how to handle bash-style completions. If you are using Oh-My-Zsh, I believe it should work out of the box. # Load bash completion functions
autoload -Uz +X compinit && compinit
autoload -Uz +X bashcompinit && bashcompinit Finally - I suggest the following test case, to try and find out if the problem is something in your bashly configuration, or in the code that generates the completions.
Now, you should be able to see completions for For me, when running this in a containerized ZSH shell, it works. |
I have also noticed this problem in the past. I'm using Oh-My-Zsh, and as you say I also think bash completions should work in general. Following your instructions above, I get partially working suggestions:
But subcommands don't work right:
I get the same suggestions as just for When I was investigating this a few months ago, I believe I tracked it down to the following line in the completions function:
This does not result in the correct value being assigned to |
Thanks for the additional info, but I cannot reproduce the problem. I am running a clean ZSH in a container, here is a screencast of how it works for me. The steps below do not reproduce the problem. If anyone either has a solution, or a way for me to reproduce it, I can try and fix it.
#!/usr/bin/env bash
_cli_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
local comp_line="${COMP_WORDS[*]:1}"
case "$comp_line" in
'download'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;
'upload'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;
''*) COMPREPLY=($(compgen -W "--help --version -h -v download upload" -- "$cur")) ;;
esac
}
complete -F _cli_completions cli
$ source completions.bash
$ cli<tab>
$ cli download<tab>
$ cli download --<tab>
# and so on |
I have now also tried it with a non-docerized zsh, with and without Oh My Zsh - both work as expected. If anyone using zsh can test with the above provided |
I tried following your instructions. I can reproduce the issue that way as well: https://asciinema.org/a/6ksQG74HFh71HYznsdEBrA7OW I believe your recording actually also has the issue. You can make it more obvious by adding flags to the This is consistent with what I saw before. And I believe it's because in ZSH |
Ok, nice - thanks for recording. |
I think I found a solution that seems to work in both bash and zsh as expected: source this file: #!/usr/bin/env bash
_cli_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
# local comp_line="${COMP_WORDS[*]:1}" # OLD
local comp_line="${COMP_WORDS[@]:1}" # NEW
echo "COMP_WORDS: ${COMP_WORDS[*]}" >> out.txt # DEBUG
echo "comp_line: ${comp_line[*]}" >> out.txt # DEBUG
case "$comp_line" in
'download'*) COMPREPLY=($(compgen -W "--force --help -f -h" -- "$cur")) ;;
'upload'*) COMPREPLY=($(compgen -W "--help --password --user -h -p -u" -- "$cur")) ;;
''*) COMPREPLY=($(compgen -W "--help --version -h -v download upload" -- "$cur")) ;;
esac
}
complete -F _cli_completions cli If you can confirm, I will bake this into completely and bashly. |
Yes that's me 😄 Your suggested change works for me as well. |
Cool! I will merge the change into completely, release a version, and then release a bashly version that forces the fixed dependency in its gemspec, followed by a docker release. Thanks for the help! |
Version 0.7.2 is released on RubyGems and Docker Hub with the fix. |
Awesome, thanks for the quick fix. I do think there is another issue with the completions, but probably not ZSH related. With the following
I get the following
So now with the newest bashly/completely, I get the following behavior:
but
right? |
No. This is the intended behavior. You can see the intended output clearly by running ---
cli:
- "--help"
- "--version"
- "-h"
- "-v"
- download
cli download:
- "--arch"
- "--help"
- "-h"
- amd64
- arm64 The completely gem creates a simplified completions script. It only sets completion arrays for commands and subcommands, and these arrays are offered regardless of their allowed position. The reason it is done like this, is simple: In order to also support positional logic, you end up with a completions script that it sometimes thousands of lines long. See the completions script for git or docker. For me, writing this kind of scripts for completion is unacceptable, and this is the reason I created completely: To allow a simple way to create usable completions. The price is that they are not perfect. Also - I found that removing the suggestion list completely from my shell, and just let it complete my line, is much easier to work with. I am doing so by adding this to my # Change tab behavior and add shift tab for old behavior
TAB: menu-complete
"\e[Z": complete so in your case, these will complete nicely:
If there is any suggestion to make this better, I am open to it (and it should be probably done in completely). |
Alright, fair enough. Thanks for the explanation and the tip. |
Originally posted by @insign in #165 (comment)
The text was updated successfully, but these errors were encountered: