我为我的Zsh使用了很棒的Powerlevel9k主题。
我定义了一个自定义的kubecontext元素来显示我的kubernetes集群(上下文)和 namespace (请参见下面的代码)。
当我通过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)
这是当前设置的屏幕截图:
最佳答案
免责声明:我是powerlevel10k的作者。
不,这在powerlevel9k中是不可能的。但是,在powerlevel10k中是可能的。 Powerlevel10k与powerlevel9k配置向后兼容,这意味着如果决定切换,则无需更改任何POWERLEVEL9K
参数。
Powerlevel10k与之前的版本相比具有几个优点:
p10k configure
进行访问。 kubecontext
支持上下文类,该类允许您根据当前处于 Activity 状态的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倍。关于kubernetes - Powerlevel9k中自定义提示元素的动态背景色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59301759/