Open Lameorc opened 3 years ago
@faho Could you help me out here? Seemed more efficient than gitter 😂
Reading the commandline buffer and parsing it is easy, but the trigger to do so is the hard part. Got any ideas? Or is this not doable?
No, not feasible.
I also really wanted this feature, so I made a hacky attempt to get close. I chose my trigger to just be the user inserting a space:
function _tide_show_on_command
if test (count (commandline -poc)) -eq 0
set -l cmd (commandline -t)
# Check to see if this is an abbr that will be expanded
if abbr -q $cmd
set -l var _fish_abbr_$cmd
set cmd $$var
end
switch $cmd
case kubectl helm kubens kubectx
set -gx tide_show_k8s # export this so our item sees it
commandline -f repaint
case '*'
set -e tide_show_k8s
commandline -f repaint
end
end
end
bind ' ' 'commandline -f expand-abbr; _tide_show_on_command; commandline -i " "'
and then using it would look like:
function _tide_item_k8s --description "Show Kubernetes context"
if set -q tide_show_k8s
# ...display context
end
end
Note, this very naive and far from as elegant as powerlevel10k's version, but it's close enough for me. Still, I'm interested in better ways to achieve this and (if feasible) it being officially added to tide!
Maybe we can break this issue into separate things? I understand that "show on command" is a great feature. But to me it's separate from "showing the k8s context". And the k8s context is super important for many people, it could be given priority if @IlanCosman agrees with it.
One can "unset" the current Kubernetes context from kubectl
, so I don't see as a problem having it always in my prompt. And maybe the default configuration could be to have it disabled.
@douglascamata Sure, I'd welcome a kubernetes item. If you're interested in creating a PR for it, you should do so against the v5 branch. A bunch of stuff has changed, but it shouldn't be too hard to figure out. You might take inspiration from the spacefish kubecontext item.
I packaged up my above idea here:
fisher install branchvincent/tide-show-on-cmd
Feel free to try it out! @IlanCosman also curious how you feel about it and if you think it's worth integrating
I'm sorry I never replied to you @branchvincent, that was rude of me. I did try it out. It's great work 👍
Basically I just felt that only binding on space is a bit annoying. If I change the commandline, I'd like the item to appear or disappear accordingly, not just when I press space.
Things like backspace
, or Ctrl+W
, or selecting from history, or using fzf, don't trigger it, and that's what I'd like before adding this feature to Tide 😞
I packaged up my above idea here:
fisher install branchvincent/tide-show-on-cmd
Feel free to try it out! @IlanCosman also curious how you feel about it and if you think it's worth integrating
Just to add my two cents from a user perspective, the behaviour may not be on par with Powerlevel10k, but it still is miles ahead of not having it at all. In my use case, as the kubectl contexts and namespaces are fairly long, using this plugin is the only way to have kubectl info on the right instead of disabling it at all. Thank you so much!
I'm not using Tide personally, but I'm evaluating switching my default shell from Zsh to Fish. I love P10k's "Show on command" feature and it's not clear how to make that work out of the box with Fish, and a Google search landed me here. I did a bit of tinkering with the proposed solution here and tried to address the issues presented above by @IlanCosman in this comment.
Generally, you can just add extra bindings beyond ' '
. To bind the "show on command" function to regular character insert, you can do something like
bind 'a' 'commandline -i "a"; _tide_show_on_command'
bind 'b' 'commandline -i "b"; _tide_show_on_command'
bind 'c' 'commandline -i "c"; _tide_show_on_command'
...
bind ' ' '_tide_show_on_command; commandline -i " "'
NOTE: It seems like it should be possible to instead
bind '' 'commandline -f self-insert; _tide_show_on_command'
, but in my experience, this will just eat input and not update the command buffer.
For keys with existing bindings, the behavior depends a bit, but generally you can just extend the preset binding that exists by default (see all with bind --all
). For instance, to handle backspace and select from history:
bind -k backspace 'commandline -f backward-delete-char; _tide_show_on_command'
bind \n '_tide_show_on_command; commandline -f execute'
bind \r '_tide_show_on_command; commandline -f execute'
NOTE: Sometimes it makes more sense to update the command buffer first before calling
_tide_show_on_command
- for instance, with backspace, we want to backspace before calling our function so that i.e. backspacingkubectl
will disable the k8s plugin when the buffer containskubect
. If we do it the other way around, you would need another backspace before it disables the plugin - i.e. buffer containskubec
.
You could extend this pattern to support other bindings like Ctrl+w
, arrow through history, etc.
If this gets integrated to Tide, do see @branchvincent repo https://github.com/branchvincent/tide-show-on-cmd - he has some substantial improvements there compared to the initial code snippet above. Kudos to him for his awesome work on the issue.
UPDATE: After playing around and using this for a day, I don't recommend any solution that uses a bind like bind ' ' 'commandline -i " "'
because it interrupts things like history paging. To get a proper solution here, we should probably update Fish's input module upstream so we can hook an event or something when the command buffer updates. Right now they're in the midst of porting the input module to rust, so it's not really a good time to add new features, but someday soon.
Is your feature request related to a problem? Please describe
Certain "info" (eg.
kubectl config current-context
or parts ofgcloud info
) is only relevant when running certain commands. Currently it's pretty easy to show it thanks to custom items but it is irrelevant in plenty of situations and just clutters up the prompt.Describe the solution you'd like
The prompt item is only visible when relevant command is part of the user input.
Additional context
powerlevel10k's section in readme
I'm not sure how feasible this is in fish without some sort of fairly obvious flickering due to the async nature of this.