-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Improve host completion #3454
Improve host completion #3454
Conversation
fbe4c54
to
5a95183
Compare
If you'd agree to the idea with Perhaps one should also name it rather |
9d96d5f
to
8b5074d
Compare
Last force push was a minor update to one of the commits. I think with the way bash implements Still |
Maybe that could/should be further extended:
I think it wouldn't be too hard to actually do that. The default for any But if a user wants to e.g. filter hosts based on what's being completed for, e.g.
or even:
|
8b5074d
to
0da1ff7
Compare
Found a small bug in the version for ZSH, which doesn't have the same Now the tests run through, though locally I still have flaky tests (namely |
btw: The idea of a convenience script that I had over in #3459 (comment) might also be of use here: We could simply ship a copy (which we update from time 2 time) of bash-completions’ Unfortunately, they don't seem to work out of the box for zsh. |
bf42b8c
to
4a1b209
Compare
Signed-off-by: Christoph Anton Mitterer <[email protected]>
`__fzf_list_hosts()` seems like a function a user may want to override with some custom code. For that reason it should be kept as simple as possible, that is printing only hostnames, one per line, optionally in some sorting. The handling of adding a `username@` (which is then the same for each line), if any, would unnecessarily complicate that for people who want to override the function. Therefore this commit moves that to the places where it's actually used (as of now only `_fzf_complete_ssh()`). This also saves any such handling for `_fzf_host_completion()`, where this isn’t needed at all. Right now it comes at a cost, namely an extra invocation of `awk` in the `_fzf_complete_ssh()`-case. However, it should be easily possible to improve `__fzf_list_hosts()` to no longer need the final `awk` in the pipeline there. Signed-off-by: Christoph Anton Mitterer <[email protected]>
Just like it’s already done for `_fzf_compgen_path()` and `_fzf_compgen_dir()` allow a user to easily define his own version of `__fzf_list_hosts()`. Also add some documentation on the expected “interface” of such custom function. Signed-off-by: Christoph Anton Mitterer <[email protected]>
…hostnames If defined, use bash-completions’s `_known_hosts_real()`-function to create the list of hostnames. This obviously requires bash-completion to be sourced before fzf. If not defined, fall back to the previous code. Signed-off-by: Christoph Anton Mitterer <[email protected]>
When bash-completion (and thus `_known_hosts_real()`) is / is not available this will typically not change during the lifetime of a shell. The only exception is if the user would unset `_known_hosts_real()`, but well, that would be his problem. So we can easily define `__fzf_list_hosts()` either using `_known_hosts_real()` or using the old code, and avoid checking every time whether `_known_hosts_real()` is defined. Signed-off-by: Christoph Anton Mitterer <[email protected]>
4a1b209
to
9a86b70
Compare
Rebased that onto all the recent changes in |
@junegunn Any thoughts about this whole series? |
I'm fine with allowing advanced users to override
|
Merged, thanks! |
_known_hosts_real '' | ||
printf '%s\n' "${COMPREPLY[@]}" | command sort -u --version-sort |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine for now. But I'll have to reconsider this if they decide to change the way the function works in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they're consolidating their function naming right now, so me might need to adapt that soon to work with the new name and fall back to the old one... but that shouldn't be too hard.
Other than that... I'd rather not expect any deeper changes to what the function does, do you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they're consolidating their function naming right now, so me might need to adapt that soon to work with the new name and fall back to the old one... but that shouldn't be too hard.
Oh, that's a deal breaker for me. I don't want to ship a product that might not work because of a sudden change in an implicit dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with their code, and I just assumed _known_hosts_real was one of their documented public API that is guaranteed to be backward-compatible. If that's not the case, I'm not going to make fzf depend on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, bash-completion is not the C standard or so... I think it's reasonable to expect that they have some level of stability in their ABI (since people use it), but nothing is ever set fully in stone… not even POSIX and the likes.
I've also only noticed it a few days ago (when looking into that eval
issue we had), that they're renaming their functions... and didn't really think any further that this might also affect _known_hosts_real()
, but it does … in scop/bash-completion@d311921 they’ve renamed it to _comp_compgen_known_hosts()
.
But actually I think this consolidation is quite reasonable for them to do… the many different names and prefixes they've started to use over time were much more messy and likely that people accidentally use such names for something else.
I've already mention that earlier here, I think hat fzf
might also benefit from such systematic consolidation (e.g. names like FZF_CTRL_T_COMMAND
are pretty vague (an ideally the keybinding should be configurable and not fixed to Ctrl-T
).
It may even make sense for fzf
to integrate in the naming schema used by bash-completion to some extent... e.g. if we'd always use _comp_fzf_*
or so for functions.
Anyway I don't see the big deal breaker here?!
It's not that they'd do this every year or so... and that consolidation they do seems to rather make this stable/official now.
APIs do change, or the code is either dead are - less likely - already perfect.
Especially, it's trivially easy to support both (see #3476).
Also, there's already much more complex to read code, that we have now in order to support different versions. The mawk
-version check or the code to support old bash versions.
Even if we would start using more bash-completion functions (e.g. also for the eval
thing), I'd guess their total number would be rather limited, so I think this is pretty manageable.
Similarly, even if I could persuade you to e.g. rename FZF_CTRL_T_COMMAND
and the likes (which I guess might be a tough job to do ;-) ), it would be easy to fall back to the old names, and e.g. print deprecation warnings for a while.
I spent quite some time thinking recently, how fzf's shell/completion integration could be made more generic & powerful and at the same time be simpler for "us" (I mean the fzf side) to maintain. |
Hey @junegunn.
This would fix #3450 and incorporates a number of improvements and ideas:
__fzf_list_hosts()
should be considered to be a function for user customisation, for which we provide however some reasonable default.This includes, that it’s up to that function to sort the host list it produces (if desired so) and the remove duplicates, if any.
username@
prefix handling from it into_fzf_complete_ssh()
in c64fe9d.completion.bash
to use bash-completion’s_known_hosts_real()
, if available, and falls back to the previous code for that, if not.Some open points:
completion.bash
, shall we remove the old host-list code altogether and only use_known_hosts_real()
? I guess probably not… we need the same code anyway for zsh, and in principlefzf
’s completion stuff can be used without bash-completion (though I doubt many do).__fzf_list_hosts()
, so that one could make it easily configurable, whether e.g. natural sort is used or not, or easily hook in something that does what I've described here or perhaps a sorting that places all IPv4/6 addresses in the end, etc. pp..What do you think about introducing e.g.
FZF_COMPLETION_HOSTNAME_SORT_CMD
, which I'd then default tosort -u --version-sort
?_known_hosts_real()
.I went through it’s code, and if I didn’t miss anything, the only side effect is that it sets
COMPREPLY
, which I therefore madelocal
. But of course, any bash-completion version may change that.Another approach would be to invoke
_known_hosts_real
and theprintf
in a( … )
subshell. Would you prefer that?