Skip to content

Instantly share code, notes, and snippets.

@towolf
Last active October 8, 2020 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save towolf/e5ee79bffcdd91aeb745c86855cc4daa to your computer and use it in GitHub Desktop.
Save towolf/e5ee79bffcdd91aeb745c86855cc4daa to your computer and use it in GitHub Desktop.
KUBECONTEXT in PS1 bash prompt
__kctx () {
local ctx
ctx="$(kubectl config current-context)"
local col
local el
local b='\001\033[0m\033[38;5;88m\033[2m\002'
local c='\001\033[0m\033[38;5;124m\033[1m\002'
local u='\001\033[0m\033[38;5;88m\033[2m\002'
if [[ $ctx == *"prod"* ]]; then
col='\001\033[0m\033[1m\033[38;5;197m\002'
else
col='\001\033[38;5;88m\002'
fi
IFS='_' read -ra el <<< "$ctx"
if [[ "${#el[@]}" = 4 ]]; then
printf -v string $'%s%s%s_' "$b" "${el[0]}" "$u" "$c" "${el[1]}" "$u" "$b" "${el[2]}" "$u" "${col}" "${el[3]}" "$u"
echo -ne "\001\033[38;5;88m\e[2m\002«${string::-1}»"
else
printf $'%s«%s»' "${col}" "${ctx}"
fi
}
PS1='\[\033[38;5;33m\e[1m\]\u@\h\[\033[00m\]:\[\033[38;5;245m\e[1m\]\w $(__kctx) \[\033[00m\]\$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment