From bc58a2c0ba6e825a01d51c2f047af15605882ca4 Mon Sep 17 00:00:00 2001 From: romkatv Date: Wed, 22 May 2019 21:53:09 +0200 Subject: [PATCH] speed up kubecontext prompt --- powerlevel9k.zsh-theme | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index cf6b9d95..9ff19bfc 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -2047,28 +2047,12 @@ prompt_dir_writable() { ################################################################ # Kubernetes Current Context/Namespace prompt_kubecontext() { - local kubectl_version="$(kubectl version --client 2>/dev/null)" - - if [[ -n "$kubectl_version" ]]; then - # Get the current Kuberenetes context - local cur_ctx=$(kubectl config view -o=jsonpath='{.current-context}') - cur_namespace="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" - # If the namespace comes back empty set it default. - if [[ -z "${cur_namespace}" ]]; then - cur_namespace="default" - fi - - local k8s_final_text="" - - if [[ "$cur_ctx" == "$cur_namespace" ]]; then - # No reason to print out the same identificator twice - k8s_final_text="$cur_ctx" - else - k8s_final_text="$cur_ctx/$cur_namespace" - fi - - "$1_prompt_segment" "$0" "$2" "magenta" "white" "KUBERNETES_ICON" 0 '' "${k8s_final_text//\%/%%}" - fi + (( $+commands[kubectl] )) || return + local ctx=$(command kubectl config view -o=jsonpath='{.current-context}') + [[ -n $ctx ]] || return + local ns="${$(command kubectl config view -o=jsonpath="{.contexts[?(@.name==\"$ctx\")].context.namespace}"):-default}" + [[ $ctx == $ns ]] || ctx+="/$ns" + "$1_prompt_segment" "$0" "$2" "magenta" "white" "KUBERNETES_ICON" 0 '' "${ctx//\%/%%}" } ################################################################