jonmosco / kube-ps1

Kubernetes prompt info for bash and zsh
Apache License 2.0
3.52k stars 351 forks source link

Differentiate terminal colors based on context #144

Closed briantopping closed 2 years ago

briantopping commented 2 years ago

Hi, thanks for this robust effort! kube-ps1 is one of the first things I set up on a new machine.

I think it could be great if the tool could somehow differentiate between contexts somehow, for instance with a color. In order to make this automatic, it might be great if the color was generated by a hash of the context name modulo the number of colors that are available for display. This might get tricky due to the background color of a terminal though.

That leads me to wonder if the entire terminal might be colorized based on the context. For instance, context names could match a regex to decide whether to the background red for production clusters, blue for development, etc.

Just looking for ways to reduce errors. It's always 4am somewhere in the world!!

jonmosco commented 2 years ago

The prompt can have color options as listed in the README, but having dynamic colors is beyond the scope of this project. If you check out the custom function option, that might be a solution for you as well. Thanks for using the project!

guillaumBrisard commented 2 years ago

@jonmosco I modified your kube-ps1.sh like that to add KUBE_PS1_BEFORE_FUNCTION

(KUBE_PS1_VERSION="master" && sudo echo "" && \
curl --silent https://raw.githubusercontent.com/jonmosco/kube-ps1/${KUBE_PS1_VERSION}/kube-ps1.sh \
| sed '/^[[:space:]]*KUBE_PS1_NAMESPACE_FUNCTION="${KUBE_PS1_NAMESPACE_FUNCTION}"/a KUBE_PS1_BEFORE_FUNCTION="${KUBE_PS1_BEFORE_FUNCTION}"' \
| sed '/^[[:space:]]*local KUBE_PS1$/i [[ ! -z "${KUBE_PS1_BEFORE_FUNCTION}" ]] && $KUBE_PS1_BEFORE_FUNCTION' \
| sudo tee /etc/bash_completion.d/kube-ps1.sh &> /dev/null)

And in my .bashrc

get_kubecluster_color() {
    if [ -z "$(echo ${KUBE_PS1_CONTEXT##prod*})" ]; then
        KUBE_PS1_BG_COLOR=red
        KUBE_PS1_SYMBOL_COLOR=blue
        KUBE_PS1_CTX_COLOR=white
        KUBE_PS1_NS_COLOR=14
    else
        KUBE_PS1_CTX_COLOR=blue
    fi
}
KUBE_PS1_BEFORE_FUNCTION=get_kubecluster_color

What do you think ?