click-contrib / click-completion

Add or enhance bash, fish, zsh and powershell completion in Click
MIT License
288 stars 33 forks source link

support colons in bash completion suggestions #40

Closed temporaer closed 2 years ago

temporaer commented 3 years ago

In the release version, completions containing colons won't work properly, because bash treats colons as word separators.

This PR uses a bash native function to make colons word characters. It also -- and this may be a matter of taste -- trims completions at the colon with another bash native function.

Konubinix commented 2 years ago

I'm not sure what you mean. I use colons in completions without trouble.

Could you provide an example and tell what you get and what you expect?

temporaer commented 2 years ago

Sure, try completing the values in bash:

@click.option('-f', type=click.Choice(":foo :bar".split()), expose_value=False)

I'd expect -f :TAB to show foo and bar, but it just adds another colon.

Konubinix commented 2 years ago

I see. Somehow the completion is not well handled only for leading TAB.

I found out that this is indeed related to the way bash splits words. Fortunately, this is customizable using COMP_WORDBREAKS.

COMP_WORDBREAKS
              The  set  of  characters  that  the  readline  library  treats as word separators when performing word completion. 

Try removing the colon from this variable and the completion will work as expected.

COMP_WORDBREAKS="$(echo "'$COMP_WORDBREAKS'" |tr -d :)"

Like you mentioned, this is more a matter of taste and I think it is more elegant to let users decide their COMP_WORDBREAKS settings rather than imposing one in click-completion.

I will close the PR because I believe setting COMP_WORDBREAKS works for your use case. Otherwise, please reopen it and continue the discussion so that we understand better your issue.

Anyway, thank you for giving me the opportunity of discovering COMP_WORDBREAKS :-).

temporaer commented 2 years ago

hmm, this depends on your tool, right? we have one that happens to expect leading colons. For the user it would be a hassle to modify COMP_WORDBREAKS every time they want to complete a different tool. Also note that bash acts different from other shells here.

Or is there a mechanism by which you propose to modify COMP_WORDBREAKS?

Konubinix commented 2 years ago

Hannes Schulz @.***> writes:

hmm, this depends on your tool, right? we have one that happens to expect leading colons. For the user it would be a hassle to modify COMP_WORDBREAKS every time they want to complete a different tool. Also note that bash is the only one here that acts incorrectly.

I don't think that bash acts "incorrectly" in deciding to consider ":" as being a word separator by default. In my mind, it is more a matter of taste. Also, to me, bash does the "right" thing in letting the user adjust COMP_WORDBREAKS to fit per needs.

I also totally agree with you that the wanted behavior eventually depends on your tool. Hence I understand that we both agree that deciding whether or not ":" is a word separator don't belong in click-completion, but rather in the tool that uses it.

May be one could someday provide a PR to make the install function accept some configuration that eventually leads to editing the COMP_WORDBREAKS appropriately. That way, the tool could install the completion that makes sense for it.

-- Konubinix GPG Key : 7439106A Fingerprint: 5993 BE7A DA65 E2D9 06CE 5C36 75D2 3CED 7439 106A