-
Notifications
You must be signed in to change notification settings - Fork 28
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 #177
Comments
I had briefly looked into that topic a while ago, but never got around to it. In general, I think that would be a nice feature to have. If you’d be willing to set something up, that would be cool 👍 Only caveat is that the klog CLI currently uses kong as CLI framework, and for now I wouldn’t want to switch away from that. kong doesn’t support completions out of the box, but there is a separate library called kongplete which provides support, and which seems like the “recommended” way to go with kong. |
I will try implementing this feature with kongplete and share my progress. |
This is an initial effort to address jotaen#177 With this change, klog uses github.com/posener/complete and github.com/willabides/kongplete to generate completions for klog's own commands. There are two coordinated changes that make this feature work: 1. When the `COMP_LINE` environment variable is set, `klog` runs in a special command completion mode. It parses the value of `COMP_LINE` (the current command line), prints to stdout a list of possible completions based on what is on the command line so far, and exits early. 2. `klog completion` prints to stdout a code snippet that defines a completion specification in the current shell. When this code snippet is executed in a shell session, the shell will thereafter invoke the same `klog` executable (with the `COMP_LINE` environment variable set) to generate possible completions. For example, in bash, completions can be enabled by running source <(log completion) Once bash completions are enabled, pressing tab after typing "klog " klog [Tab] will present the available klog commands. In the current implementation, the generated completions are rudimentary and lack ergonomics. There is zero test coverage for this new feature. Because it relies on interaction with the shell, this feature is not easy to test.
I have a branch with rudimentary implementation of this feature: https://github.com/chairmank/klog/tree/shell-completion There is no test coverage :( I have manually tested a few commands, and only with bash shell. There are no completions for filter options (e.g. |
🎉 Awesome, thanks for figuring this out! I’ve tried this on both bash and zsh (macOS), and it seems to work well:
The code structure you suggested seems to fit in very nicely. It makes sense to me how the predictors are separated into the Regarding tests: my feeling is that as long as the predictors are as straightforward as the To me, the current functionality would already be useful enough for it to be merged in. Feel free to add more completion predictors as you like, and open a PR at any point. |
Thanks for reviewing my change.
kongplete uses complete, which is specifically for bash completion by design. kongplete supports zsh and fish with thin wrappers around bash completion, so it is unable to leverage other shell-specific completion capabilities. For example, in fish shell, available commands are displayed with helpful descriptions, but kongplete can not implement this behavior because bash does not have an equivalent capability. There exists a standalone wrapper for bash completions in PowerShell. I have not investigated whether this wrapper could be used by kongplete to add support for PowerShell. Even if it is feasible, it would lack the richness of "native" PowerShell completions. Cobra has a different design that involves implementing a function for each shell to leverage shell-specific completion capabilities. I think that using Cobra instead of kong would provide superior ergonomics. Of course, I understand that this is a major change and you are not interested in doing it now. Anyway, this implementation with kongplete adds useful functionality. I will continue working on this branch and open a pull request. |
I noticed a difficulty with the I have not determined whether I can override this behavior in kongplete. If it not possible to override, what do you think about removing the dummy filter flags and simply exposing the actual filter flags? |
Another problem that I noticed is that the |
Ah okay, thanks for all that background info, I wasn’t aware of that. I’m generally not married to kong in any way, I was only reluctant to invest all the work to migrate to Cobra, when kong is working well overall currently. (Well, except for the limited completion capabilities, that is.)
What I like about the dummy flags is how they make the help output concise, and also how they resemble the logical pattern of how the flags work. However, I’ve tried it out again on your branch, and seeing how it completes to
vs.
Ah, bummer. There is this stale PR to add support for I agree with you that flawless file path completion would be critical. Would it possible to fallback to the shell’s default completion for file paths? (E.g. via |
I verified that defining the completion specification in bash as
enables default shell expansions for file paths. It would be easy to change the |
Oh terrific, that sounds like a good solution! I have confirmed on zsh that your fix works! ( |
Too bad that the kongplete maintainers don’t respond in the issue that you opened. Do you happen to know how complicated it is to fix the kongplete code? Because I think I would just fork the kongplete project and switch the dependency to my fork, in order to move forward with this issue. |
Quick update: I created a fork of The fork can be dropped in relatively easily via the I’ll try to do some more testing throughout the next weeks. It might be nice to file a PR with |
Thanks for the update! I am sorry that I have not done anything on this issue recently. What do you think about reimplementing the command-line interface with Cobra? I understand that forking kongplete is easier and a more immediate solution, but Cobra is already maintained and has more features than kongplete. Would you accept a switch to Cobra if it preserves the current behavior of the CLI? If you are okay with the idea, I will explore it and report what I learn. |
Oh, don’t worry! You helped tremendously by figuring out all the groundwork, so thanks heaps for that. As far as I see, the remaining todos are:
I don’t mean to take this away from you, so let me know if you want to keep working on it. I’m otherwise also happy to collaborate, or take over, depending on your capacity. There is also no rush with this. (Though I have to say the completions are a really cool usability improvement – I’ve started to use them locally now.) Regarding Cobra, I’ve opened a discussion here, so that we have a dedicated place to discuss: #205. |
Thanks! I recently started using I am also interested in implementing completion for the various Thanks for opening the discussion of Cobra. I don't have time to pursue it right now, but I will be sure to report any progress that I make in the discussion. |
I also played around with fish this weekend, just because I was curious and wanted to check it out in general. While on it, I was able to confirm that the generated completions code seems to work fine 🎉 So I think it’s all good to go now: #209 |
I looked at the changes in #209 and everything looks good to me. Thanks for implementing this feature! |
I would like a command that writes a completion script for any supported
$SHELL
to stdout.For example, to enable completion for klog commands in Bash, one would source the output of
klog completion bash
:If there is interest, I can implement. I think that Cobra automatically generates shell completion.
The text was updated successfully, but these errors were encountered: