diff options
-rwxr-xr-x | powerlevel9k.zsh-theme | 61 |
1 files changed, 55 insertions, 6 deletions
diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 7fb465aa..2eec96b3 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -817,9 +817,15 @@ prompt_dir() { for (( ; i > 0; --i )); do local pkg_file='' for pkg_file in $dir/${~pat}(N); do - local pkg_name='' - pkg_name=$(command jq -j '.name' <$pkg_file) && [[ -n $pkg_name ]] || return - parts[1,i]=($pkg_name) + local -H stat=() + zstat -H stat -- $pkg_file 2>/dev/null || return + if ! _p9k_cache_get $0_pkg $stat[inode] $stat[mtime] $stat[size]; then + local pkg_name='' + pkg_name=$(command jq -j '.name' <$pkg_file 2>/dev/null) || pkg_name='' + _p9k_cache_set "$pkg_name" + fi + [[ -n $_P9K_CACHE_VAL[1] ]] || return + parts[1,i]=($_P9K_CACHE_VAL[1]) fake_first=1 return done @@ -2049,6 +2055,35 @@ prompt_dir_writable() { ################################################################ # Kubernetes Current Context/Namespace + +# Set to false to truncate trailing "/default": "mycontext/default" will become "mycontext". +set_default POWERLEVEL9K_KUBECONTEXT_SHOW_DEFAULT_NAMESPACE true + +# Defines context classes for the purpose of applying different styling to different contexts. +# +# POWERLEVEL9K_KUBECONTEXT_CLASSES must be an array with even number of elements. The first +# element in each pair defines a pattern against which the current context (in the format it is +# displayed in the prompt) gets matched. The second element defines context class. Patterns are +# tried in order. The first match wins. +# +# If a non-empty class <C> is assigned to a context, the segment is styled with +# POWERLEVEL9K_KUBECONTEXT_<U>_BACKGROUND and POWERLEVEL9K_KUBECONTEXT_<U>_FOREGROUND where <U> is +# uppercased <C>. Otherwise with POWERLEVEL9K_KUBECONTEXT_BACKGROUND and +# POWERLEVEL9K_KUBECONTEXT_FOREGROUND. +# +# Example: Use red background for contexts containing "prod", green for "testing" and yellow for +# everything else. +# +# POWERLEVEL9K_KUBECONTEXT_CLASSES=( +# '*prod*' prod +# '*testing*' testing +# '*' other) +# +# POWERLEVEL9K_KUBECONTEXT_PROD_BACKGROUND=red +# POWERLEVEL9K_KUBECONTEXT_TESTING_BACKGROUND=green +# POWERLEVEL9K_KUBECONTEXT_OTHER_BACKGROUND=yellow +set_default -a POWERLEVEL9K_KUBECONTEXT_CLASSES + prompt_kubecontext() { (( $+commands[kubectl] )) || return local cfg @@ -2058,17 +2093,31 @@ prompt_kubecontext() { zstat -H stat -- $cfg 2>/dev/null || continue key+=($cfg $stat[inode] $stat[mtime] $stat[size] $stat[mode]) done + if ! _p9k_cache_get $0 "${key[@]}"; then local ctx=$(command kubectl config view -o=jsonpath='{.current-context}') if [[ -n $ctx ]]; then local p="{.contexts[?(@.name==\"$ctx\")].context.namespace}" local ns="${$(command kubectl config view -o=jsonpath=$p):-default}" - [[ $ctx == $ns ]] || ctx+="/$ns" + if [[ $ctx != $ns && ($ns != default || $POWERLEVEL9K_KUBECONTEXT_SHOW_DEFAULT_NAMESPACE == true) ]]; then + ctx+="/$ns" + fi + fi + local suf + if [[ -n $ctx ]]; then + local pat class + for pat class in $POWERLEVEL9K_KUBECONTEXT_CLASSES; do + if [[ $ctx == ${~pat} ]]; then + [[ -n $class ]] && suf=_${(U)class} + break + fi + done fi - _p9k_cache_set "$ctx" + _p9k_cache_set "$ctx" "$suf" fi + [[ -n $_P9K_CACHE_VAL[1] ]] || return - $1_prompt_segment $0 $2 magenta white KUBERNETES_ICON 0 '' "${_P9K_CACHE_VAL[1]//\%/%%}" + $1_prompt_segment $0$_P9K_CACHE_VAL[2] $2 magenta white KUBERNETES_ICON 0 '' "${_P9K_CACHE_VAL[1]//\%/%%}" } ################################################################ |