Powerlevel9k中自定义提示元素的动态背景色

时间:2019-12-12 09:49:16

标签: kubernetes themes zsh oh-my-zsh

我正在为我的Zsh使用很棒的Powerlevel9k主题。

我定义了一个自定义kubecontext元素来显示我的kubernetes集群(上下文)和名称空间(请参见下面的代码)。

虽然我通过color变量有条件地设置了前景色,但我想设置背景色,以便能够更好地了解在生产集群上工作时的情况。 Powerlevel9k可以通过某种方式实现吗?我能找到的是,我可以使用POWERLEVEL9K_CUSTOM_KUBECONTEXT_BACKGROUND='075'

静态设置提示元素的背景颜色
# Kubernetes Current Context/Namespace
custom_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="$cur_ctx/$cur_namespace"

    local color='%F{black}'
    [[ $cur_ctx == "prod" ]] && color='%F{196}'
    echo -n "%{$color%}\U2388  $k8s_final_text%{%f%}" # \U2388 is Kubernetes Icon

    #"$1_prompt_segment" "$0" "$2" "magenta" "black" "$k8s_final_text" "KUBERNETES_ICON"
  fi
}

POWERLEVEL9K_CUSTOM_KUBECONTEXT="custom_prompt_kubecontext"

# Powerlevel9k configuration
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir vcs custom_kubecontext)

以下是当前运行中的屏幕截图:

screenshot showing the different kubecontext prompt colors depending on the current kubecontext

1 个答案:

答案 0 :(得分:2)

免责声明:我是powerlevel10k的作者。

否,在powerlevel9k中这是不可能的。但是,在powerlevel10k中是可能的。 Powerlevel10k与powerlevel9k配置向后兼容,这意味着如果决定切换,则不必更改任何POWERLEVEL9K参数。

Powerlevel10k与之前的版本相比有几个优点:

  1. 速度快十倍以上。
  2. 它具有内置的配置向导。键入p10k configure进行访问。
  3. 它具有许多新功能。其中之一与您有关。内置的kubecontext支持上下文类,您可以根据当前处于活动状态的kubernetes上下文来设置此提示段的样式。以下是p10k configure生成的配置的摘录:
# Kubernetes context classes for the purpose of using different colors, icons and expansions with
# different contexts.
#
# POWERLEVEL9K_KUBECONTEXT_CLASSES is an array with even number of elements. The first element
# in each pair defines a pattern against which the current kubernetes context gets matched.
# More specifically, it's P9K_CONTENT prior to the application of context expansion (see below)
# that gets matched. If you unset all POWERLEVEL9K_KUBECONTEXT_*CONTENT_EXPANSION parameters,
# you'll see this value in your prompt. The second element of each pair in
# POWERLEVEL9K_KUBECONTEXT_CLASSES defines the context class. Patterns are tried in order. The
# first match wins.
#
# For example, given these settings:
#
#   typeset -g POWERLEVEL9K_KUBECONTEXT_CLASSES=(
#     '*prod*'  PROD
#     '*test*'  TEST
#     '*'       DEFAULT)
#
# If your current kubernetes context is "deathray-testing/default", its class is TEST
# because "deathray-testing/default" doesn't match the pattern '*prod*' but does match '*test*'.
#
# You can define different colors, icons and content expansions for different classes:
#
#   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_FOREGROUND=0
#   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_BACKGROUND=2
#   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_VISUAL_IDENTIFIER_EXPANSION='⭐'
#   typeset -g POWERLEVEL9K_KUBECONTEXT_TEST_CONTENT_EXPANSION='> ${P9K_CONTENT} <'
typeset -g POWERLEVEL9K_KUBECONTEXT_CLASSES=(
    # '*prod*'  PROD    # These values are examples that are unlikely
    # '*test*'  TEST    # to match your needs. Customize them as needed.
    '*'       DEFAULT)
typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_FOREGROUND=7
typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_BACKGROUND=5
typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_VISUAL_IDENTIFIER_EXPANSION='⎈'

您还可以自定义kubecontext的文本内容。运行~/.p10k.zsh后,您将在p10k configure中找到更多信息。哦,kubecontext的powerlevel10k快约1000倍。