Skip to content
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

custom color depending on context does not update within current shell #23

Closed
ranvit opened this issue Oct 2, 2020 · 3 comments · Fixed by #24
Closed

custom color depending on context does not update within current shell #23

ranvit opened this issue Oct 2, 2020 · 3 comments · Fixed by #24
Assignees

Comments

@ranvit
Copy link

ranvit commented Oct 2, 2020

I have the following in my .zshrc

autoload -U colors; colors
source $(brew ls zsh-kubectl-prompt | grep 'kubectl.zsh')
[[ "$(kubectl config current-context)" =~ "PRODUCTION" ]] && color=red || color=green
RPROMPT='%{$fg[$color]%}[$ZSH_KUBECTL_PROMPT]%{$reset_color%}'
# customization
zstyle ':zsh-kubectl-prompt:' separator '|' 

I want the color of the prompt to be red if my context is the production cluster.

For starters, I was unable to use ZSH_KUBECTL_CONTEXT (it doesn't get set in my environment), so I'm calling kubectl config current-context again

My problem:

Suppose I'm on my development context, the prompt color is green. Then I switch my context to production - I expect to see the prompt color change to red. It remains green instead of changing to red.
However, opening a new shell forces the prompt color to change to red.

I'm probably not doing something correct, please help.

@ranvit
Copy link
Author

ranvit commented Oct 8, 2020

I figured out that this line is not updating the $color variable when we are switching kubernetes contexts.

[[ "$(kubectl config current-context)" =~ "PRODUCTION" ]] && color=red || color=green

Manually setting the $color variable immediately updates the prompt color

color=blue

@ranvit
Copy link
Author

ranvit commented Oct 9, 2020

GOT IT

Someone feel free to correct my work.

So, this is the important line where color is being set into the right-prompt. Whatever RPROMT contains is what gets re-executed on every terminal entry/command.

RPROMPT='%{$fg[$color]%}[$ZSH_KUBECTL_PROMPT]%{$reset_color%}'

And from my previous comment we know that the `$color variable is not getting re-calculated. Thats because this line is not getting re-run on every terminal command

[[ "$(kubectl config current-context)" =~ "PRODUCTION" ]] && color=red || color=green

The solution

is to put that color calculation logic into the RPROMPT. This works for me now! Like so

RPROMPT='%{$fg[$([[ "$(kubectl config current-context)" =~ "aks-prod" ]] && echo red || echo green)]%}[$ZSH_KUBECTL_PROMPT]%{$reset_color%}'

On a different note, I found it nicer to set the background color to red/green as opposed to the foreground. This is what I'm using

RPROMPT='%{$bg[$([[ "$(kubectl config current-context)" =~ "PRODUCTION" ]] && echo red || echo green)]$fg[black]%}[$ZSH_KUBECTL_PROMPT]%{$reset_color%}'

@superbrothers superbrothers self-assigned this Oct 29, 2020
@superbrothers
Copy link
Owner

superbrothers commented Nov 3, 2020

I'm sorry for the late reply.

The solution

is to put that color calculation logic into the RPROMPT. This works for me now! Like so

RPROMPT='%{$fg[$([[ "$(kubectl config current-context)" =~ "aks-prod" ]] && echo red || echo green)]%}[$ZSH_KUBECTL_PROMPT]%{$reset_color%}'

zsh-kubectl-prompt creates the followting 4 variables:

  • ZSH_KUBECTL_CONTEXT
  • ZSH_KUBECTL_NAMESPACE
  • ZSH_KUBECTL_PROMPT
  • ZSH_KUBECTL_USER

So, using ZSH_KUBECTL_CONTEXT, you can do the following:

RPROMPT='%{$fg[$([[ "$ZSH_KUBECTL_PROMPT" =~ "aks-prod" ]] && echo red || echo green)]%}[$ZSH_KUBECTL_PROMPT]%{$reset_color%}'

This is light way because it does not run kubectl config current-context command.

If you have more complicated conditions, I recommend to define it as a function.

function right_prompt() {
  local color="green"

  if [[ "$ZSH_KUBECTL_CONTEXT" =~ "aks-prod" ]]; then
    color="red"
  fi

  echo "%{$fg[$color]%}[$ZSH_KUBECTL_PROMPT]%{$reset_color%}"
}

RPROMPT='$(right_prompt)'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants