-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Shell completion requires double TAB on first usage #1874
Comments
I did some digging and noticed commit e66017d This bug does not occur with commits before e66017d Completion script not exhibiting the bug (before e66017d)#compdef emu
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi Completion script exhibiting the bug#compdef emu
_cli_zsh_autocomplete() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}
compdef _cli_zsh_autocomplete emu |
@bartekpacia I'm not a zsh expert. Is there a way you can fix it ? |
Yes, I have fixed it (see this comment for a version of completion script that works fine). My suggestion is to eliminate the For example, I have a program called
What do you think? I would be happy to update the docs accordingly.
|
Yeah go ahead. |
Below is an even better version of the script above. The advantage is that it can be . ./autocomplete/zsh_autocomplete Warning The completion script must be sourced. If you don't source it, the following happens:
#compdef emu
compdef _emu emu
_emu() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}
# don't run the completion function when being source-ed or eval-ed
if [ "$funcstack[1]" = "_emu" ]; then
_emu
fi |
This issue is fixed in v3, especially after #1998 For examples of CLI programs built with this module and that have shell completion properly set up, see: |
My urfave/cli version is
v2.27.1
Checklist
Dependency Management
My project is using Go modules.
Describe the bug
TL;DR After shell startup, I have to click tab twice for completions to show up.
I noticed that the provided zsh completion script doesn't work as expected when it is placed in the conventional directory for shell completion function
/opt/homebrew/share/zsh/site-functions
(Myzsh
is installed through Homebrew, to keep it up-to-date).To reproduce expected behavior
All code is available in a public repository of mine.
Clone it
Build executable:
Source shell completion:
Run
./emu
followed by<SPACE>
and<TAB>
, and notice that shell completions show up just fine:Video demo:
completion.mp4
To reproduce wrong behavior
Do step 1 and 2 from above, but instead of sourcing the completion script directly, put it into the conventional location for shell completion scripts:
Now reload shell so it picks up new completions from that directory:
Note
At this point, you can run
which _emu
and see it output something like this:which is strange and I don't understand it. It's as if it was not initialized?
Now, try to trigger shell completion by typing this in the cloned project:
and notice no shell completions show up. But if you click
<TAB>
second time, they will show up.This video demonstrates this behavior:
completion_broken.mp4
Notice that at the end of the video I clicked
<TAB>
but it didn't do anything.But interesting thing is that after the first
<TAB>
, but before the second<TAB>
, the output ofwhich _emu
changes:New which _emu output
Expected behavior
I expect shell completion suggestions to be shown immediately on first TAB.
Additional context
I noticed this only occurs with urfave/cli.
It does not occur with zsh completions generated by Cobra.
I did some googling and found
<TAB>
twice - this question explains my exact issue.Want to fix this yourself?
I don't think I have enough CLI completion expertise and experience. But I have time, if someone could guide me into some resources, I'd be happy to try!
Also, thanks a ton for this wonderful library, I truly love it as it's much more lightweight than Cobra IMHO. If this small issue was fixed, I'd be on cloud 9.
Run
go version
and paste its output hereRun
go env
and paste its output hereThe text was updated successfully, but these errors were encountered: