Add kube context to prompt

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-09-16 14:16:38 +02:00
parent 6059afdaa9
commit ea39a9ea85
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -11,94 +11,109 @@
# Also borrowing from http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/ # Also borrowing from http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/
function box_color() { function box_color() {
[ -f ~/.box_color ] && cat ~/.box_color || ~/bin/color_from_hostname.py [ -f ~/.box_color ] && cat ~/.box_color || ~/bin/color_from_hostname.py
} }
function box_name() { function box_name() {
[ -f ~/.box-name ] && cat ~/.box-name || echo ${SHORT_HOST:-$HOST} [ -f ~/.box-name ] && cat ~/.box-name || echo ${SHORT_HOST:-$HOST}
} }
function git_describe() { function git_describe() {
git describe --tags 2>/dev/null || git rev-parse --short HEAD 2>/dev/null || printf '\ue701' git describe --tags 2>/dev/null || git rev-parse --short HEAD 2>/dev/null || printf '\ue701'
} }
function awsenv_prompt() { function awsenv_prompt() {
local pr=$(awsenv prompt 2>/dev/null) local pr=$(awsenv prompt 2>/dev/null)
[ -z "${pr}" ] || echo "${bracket_open} ${pr} ${bracket_close}" [ -z "${pr}" ] || echo "${bracket_open} ${pr} ${bracket_close}"
} }
function config-git_prompt() { function config-git_prompt() {
if [ -f ~/bin/config-git-status.sh ]; then if [ -f ~/bin/config-git-status.sh ]; then
if ! (~/bin/config-git-status.sh); then if ! (~/bin/config-git-status.sh); then
echo "${bracket_open} %{$fg[red]%} ${bracket_close}" echo "${bracket_open} %{$fg[red]%} ${bracket_close}"
fi fi
fi fi
} }
function shortened_branch() { function shortened_branch() {
local branch=$(git_current_branch) local branch=$(git_current_branch)
[ $(echo -n "${branch}" | wc -c) -gt 15 ] && branch="${branch:0:15}%{$fg[red]%}$(printf '\uf141')%{$reset_color%}" [ $(echo -n "${branch}" | wc -c) -gt 15 ] && branch="${branch:0:15}%{$fg[red]%}$(printf '\uf141')%{$reset_color%}"
echo "${branch}" echo "${branch}"
} }
function build_git_prompt() { function build_git_prompt() {
# Allow hiding the right side of the prompt # Allow hiding the right side of the prompt
(test "${NO_RIGHT}" = "true") && return (test "${NO_RIGHT}" = "true") && return
# When there is no git, don't show a git prompt # When there is no git, don't show a git prompt
git branch >/dev/null 2>/dev/null || return git branch >/dev/null 2>/dev/null || return
# Fetch status of the current repo # Fetch status of the current repo
local INDEX=$(command git status --porcelain -b 2>/dev/null) local INDEX=$(command git status --porcelain -b 2>/dev/null)
local REMOTE=$(command git remote -v | grep fetch) local REMOTE=$(command git remote -v | grep fetch)
echo -n "${bracket_open} %{$fg[blue]%}" echo -n "${bracket_open} %{$fg[blue]%}"
# Mark specific remotes # Mark specific remotes
case "${REMOTE}" in case "${REMOTE}" in
*bitbucket.org*) *bitbucket.org*)
printf '\ue703 ' printf '\ue703 '
;; ;;
*github.com*) *github.com*)
printf '\ue709 ' printf '\ue709 '
;; ;;
*gitlab.com*) *gitlab.com*)
printf '\uf296 ' printf '\uf296 '
;; ;;
*) *)
printf '\uf1d3 ' printf '\uf1d3 '
;; ;;
esac esac
# Show current branch and commit / tag # Show current branch and commit / tag
echo -n "%{$reset_color%}" echo -n "%{$reset_color%}"
echo -n "$(shortened_branch) " echo -n "$(shortened_branch) "
echo -n "($(git_describe)) " echo -n "($(git_describe)) "
# Print repository status information # Print repository status information
[ $(echo "$INDEX" | wc -l) -gt 1 ] && echo -n "%{$fg[red]%}$(printf '\uf0f6')%{$FG[236]%} " [ $(echo "$INDEX" | wc -l) -gt 1 ] && echo -n "%{$fg[red]%}$(printf '\uf0f6')%{$FG[236]%} "
(git rev-parse --verify refs/stash >/dev/null 2>&1) && echo -n "%{$fg[blue]%}$(printf '\uf64c')%{$FG[236]%} " (git rev-parse --verify refs/stash >/dev/null 2>&1) && echo -n "%{$fg[blue]%}$(printf '\uf64c')%{$FG[236]%} "
# Show difference to remote # Show difference to remote
if (echo "$INDEX" | grep '^## .*ahead.*behind' &>/dev/null); then if (echo "$INDEX" | grep '^## .*ahead.*behind' &>/dev/null); then
echo -n "%{$fg[red]%}$(printf '\uf047')" echo -n "%{$fg[red]%}$(printf '\uf047')"
elif (echo "$INDEX" | grep '^## .*ahead' &>/dev/null); then elif (echo "$INDEX" | grep '^## .*ahead' &>/dev/null); then
echo -n "%{$fg[green]%}$(printf '\uf061')" echo -n "%{$fg[green]%}$(printf '\uf061')"
elif (echo "$INDEX" | grep '^## .*behind' &>/dev/null); then elif (echo "$INDEX" | grep '^## .*behind' &>/dev/null); then
echo -n "%{$fg[yellow]%}$(printf '\uf060')" echo -n "%{$fg[yellow]%}$(printf '\uf060')"
else else
echo -n "%{$fg[green]%}=" echo -n "%{$fg[green]%}="
fi fi
echo -n "%{$reset_color%} ${bracket_close}" echo -n "%{$reset_color%} ${bracket_close}"
} }
function prompt_gomod() { function prompt_gomod() {
local mod_default="auto" local mod_default="auto"
local mod=${GO111MODULE:-${mod_default}} local mod=${GO111MODULE:-${mod_default}}
[[ ${mod} == ${mod_default} ]] || echo "${bracket_open} %{$fg[yellow]%}${mod} ${bracket_close}" [[ ${mod} == ${mod_default} ]] || echo "${bracket_open} %{$fg[yellow]%}${mod} ${bracket_close}"
}
function prompt_kubectx() {
command -v kubectl >/dev/null || return 0
local ctx="$(kubectl config current-context 2>/dev/null || echo "")"
local color="yellow"
[[ -n $ctx ]] || return 0
case "${ctx}" in
minikube) color="green" ;;
prod) color="red" ;;
esac
echo "${bracket_open} %{$fg[${color}]%}${ctx} ${bracket_close}"
} }
local current_dir='$(short_path)' local current_dir='$(short_path)'
@ -114,9 +129,10 @@ local prompt_part_exit="%(?..${bracket_open} %{$fg[red]%}%?%{${reset_color}%} ${
local prompt_part_char='$(prompt_char)' local prompt_part_char='$(prompt_char)'
local prompt_part_configgit='$(config-git_prompt)' local prompt_part_configgit='$(config-git_prompt)'
local prompt_part_gomod='$(prompt_gomod)' local prompt_part_gomod='$(prompt_gomod)'
local prompt_part_kubectx='$(prompt_kubectx)'
PROMPT=" PROMPT="
╭─ ${prompt_part_time}${prompt_part_user}${prompt_part_configgit}${prompt_part_gomod}${prompt_part_exit} ${prompt_part_path} ╭─ ${prompt_part_time}${prompt_part_user}${prompt_part_configgit}${prompt_part_gomod}${prompt_part_kubectx}${prompt_part_exit} ${prompt_part_path}
╰─ " ╰─ "
RPROMPT="${git_info}" RPROMPT="${git_info}"