romkatv / powerlevel10k

A Zsh theme
MIT License
44.9k stars 2.13k forks source link

Disable SHOW_ON_COMMAND on a specific folder only #1327

Closed pietrinoatzenimfm closed 3 years ago

pietrinoatzenimfm commented 3 years ago

Hi, I was wondering if there's a simple way to disable SHOW_ON_COMMAND on specific folders only. I'd use this to enable or disable kubecontext if I'm in a folder that contains a .envrc for direnv. I found that I'm able to unset the variable and reload p10k, but direnv doesn't recognize the p10k script.

romkatv commented 3 years ago

Try this:

  1. Remove or comment out POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND.
  2. Add this:
    function p10k-on-post-widget() {
     local z=$'\0'
     if [[ -n $DIRENV_DIR  ]] || (( P9K_COMMANDS[(I)(|*$z)kubectl] )); then
       p10k display '*/kubectl'=show
     else
       p10k display '*/kubectl'=hide
     fi
    }
pietrinoatzenimfm commented 3 years ago

I tried, but nothing happens (I see the default kubecontext displayed even if I'm not in a folder that's supposed to have kubernetes code).

BTW: I see the p10k-on-post-widget function is executed if I add some echo, but I really don't understand what's doing to debug further. Do you have a guide for low level usage of p10k?

pietrinoatzenimfm commented 3 years ago

Oh, I might better specify the requirement too: what I'd like to do is to have a folder with K8s code, and a .envrc in which I specify/enable the p10k section for kubecontext (i.e. with a var like P10K_ENABLE_KUBECONTEXT). But in all other folders I'd like that to use the default SHOW_ON_COMMAND.

romkatv commented 3 years ago

I tried, but nothing happens (I see the default kubecontext displayed even if I'm not in a folder that's supposed to have kubernetes code).

This is unexpected. What's the output of functions p10k-on-post-widget?

romkatv commented 3 years ago

Maybe I misunderstood what you want. Anyway, you can adjust the condition in the code as you like. (( P9K_COMMANDS[(I)(|*$z)kubectl] )) evaluates to true if the current command invokes kubectl. The rest is self-explanatory.

pietrinoatzenimfm commented 3 years ago

I figured it out, and I almost fixed it with this (note the change in the name of the p10k part):

function p10k-on-post-widget() {
  local z=$'\0'
  if [[ -n $DIRENV_DIR  ]] || (( P9K_COMMANDS[(I)(|*$z)kubectl] )); then
    p10k display '*/kubecontext'=show
  else
    p10k display '*/kubecontext'=hide
  fi
}

The only strange behavior I notice is that this applies only "after" I do something in the prompt (i.e. I start writing any command). I'll try to explain better below:

romkatv commented 3 years ago

How do you "move to a folder"?

pietrinoatzenimfm commented 3 years ago

I mean "cd"...

pietrinoatzenimfm commented 3 years ago

Also, what I'm noticing is that direnv loads (or unloads) its environment after I execute "cd" into a specific folder. I don't know if this might interfere with p10k-on-post-widget.

romkatv commented 3 years ago

Try adding this:

function p10k-on-post-widget() {
  if [[ -n $DIRENV_DIR  ]]; then
    p10k display '*/kubecontext'=show
  else
    p10k display '*/kubecontext'=hide
  fi
}

Also make sure that POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND isn't set.

pietrinoatzenimfm commented 3 years ago

Same thing: changing directory doesn't update the prompt until I press any character (POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND is not set).

romkatv commented 3 years ago

That's unfortunate. I'm afraid I cannot spare any more time on this. Sorry.

pietrinoatzenimfm commented 3 years ago

I think this works:

function p10k-on-post-widget() {
   local z=$'\0'
   if [[ -v P10K_ENABLE_KUBECONTEXT ]] || (( P9K_COMMANDS[(I)(|*$z)kubectl|helm|kubens|kubectx|oc|istioctl|kogito|k9s|helmfile] )); then
     p10k display '*/kubecontext'=show
   else
     p10k display '*/kubecontext'=hide
   fi
 }

just by restarting the terminal and not sourcing ~./zshrc. Thanks a lot!

romkatv commented 3 years ago

just by restarting the terminal and not sourcing ~./zshrc. Thanks a lot!

https://github.com/romkatv/powerlevel10k#weird-things-happen-after-typing-source-zshrc