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

dynamic background color for custom prompt element in Powerlevel9k

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

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

虽然我通过颜色变量有条件地设置前景色,但我想设置背景色,以便在我在生产集群上工作时能够更好地看到。 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

    #"_prompt_segment" "[=10=]" "" "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)

这是当前设置的屏幕截图:

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

不,这在 powerlevel9k 中是不可能的。然而,它在 powerlevel10k 中是可能的。 Powerlevel10k 向后兼容 powerlevel9k 配置,这意味着如果您决定切换,则无需更改任何 POWERLEVEL9K 参数。

Powerlevel10k 与其前身相比有几个优点:

  1. 速度提高了 10 多倍。
  2. 它有一个内置的配置向导。输入 p10k configure 访问它。
  3. 它有很多新功能。其中之一与您有关。内置 kubecontext 支持 context 类 允许您根据当前处于活动状态的 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 configure,您将在 ~/.p10k.zsh 中找到更多信息。哦,kubecontext 在 powerlevel10k 中大约快 1000 倍。