Browse Source

Add k8s context to prompt

Thomas Dy 5 years ago
parent
commit
953696ae9a
1 changed files with 16 additions and 4 deletions
  1. 16 4
      .zsh/prompt.zsh

+ 16 - 4
.zsh/prompt.zsh

@@ -9,10 +9,18 @@ local host='%F{blue}%B%M%b%f'
 local dir='%F{yellow}%~%f'
 local git='$(git_prompt_string)'
 local nix='$(nix_shell_prompt_string)'
+local k8s='$(k8s_prompt_string)'
+local right=''
+if [ -n "$IN_NIX_SHELL" ]; then
+  right+="$nix"
+fi
+if type kubectl > /dev/null; then
+  right+="$k8s"
+fi
 local error='%F{red}%B%?%b%f'
 
 PROMPT="$time $user@$host $dir $git%# "
-RPROMPT="%(?.$nix.$error)"
+RPROMPT="%(?#$right#$error)"
 
 # Show Git branch/tag, or name-rev if on detached head
 parse_git_branch() {
@@ -77,7 +85,11 @@ git_prompt_string() {
 }
 
 nix_shell_prompt_string() {
-  if [ -n "$IN_NIX_SHELL" ]; then
-    echo "[%F{yellow}nix%f]"
-  fi
+  echo "[%F{yellow}nix%f]"
+}
+
+k8s_prompt_string() {
+  local k8s_context=$(kubectl config current-context)
+  local k8s_namespace=$(kubectl config get-contexts --no-headers $k8s_context | awk '{ print $5 }')
+  echo "[%F{magenta}$k8s_context%f|%F{blue}$k8s_namespace%f]"
 }