From ea39a9ea8536fc7fe874bf07f2de5f8069d7ed83 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 16 Sep 2022 14:16:38 +0200 Subject: [PATCH] Add kube context to prompt Signed-off-by: Knut Ahlers --- .zsh/oh-my-custom/luzifer.zsh-theme | 132 ++++++++++++++++------------ 1 file changed, 74 insertions(+), 58 deletions(-) diff --git a/.zsh/oh-my-custom/luzifer.zsh-theme b/.zsh/oh-my-custom/luzifer.zsh-theme index 6771bcd..307beac 100644 --- a/.zsh/oh-my-custom/luzifer.zsh-theme +++ b/.zsh/oh-my-custom/luzifer.zsh-theme @@ -11,94 +11,109 @@ # Also borrowing from http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/ 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() { - [ -f ~/.box-name ] && cat ~/.box-name || echo ${SHORT_HOST:-$HOST} + [ -f ~/.box-name ] && cat ~/.box-name || echo ${SHORT_HOST:-$HOST} } 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() { - local pr=$(awsenv prompt 2>/dev/null) - [ -z "${pr}" ] || echo "${bracket_open} ${pr} ${bracket_close}" + local pr=$(awsenv prompt 2>/dev/null) + [ -z "${pr}" ] || echo "${bracket_open} ${pr} ${bracket_close}" } function config-git_prompt() { - if [ -f ~/bin/config-git-status.sh ]; then - if ! (~/bin/config-git-status.sh); then - echo "${bracket_open} %{$fg[red]%} ${bracket_close}" - fi - fi + if [ -f ~/bin/config-git-status.sh ]; then + if ! (~/bin/config-git-status.sh); then + echo "${bracket_open} %{$fg[red]%} ${bracket_close}" + fi + fi } function shortened_branch() { - local branch=$(git_current_branch) - [ $(echo -n "${branch}" | wc -c) -gt 15 ] && branch="${branch:0:15}%{$fg[red]%}$(printf '\uf141')%{$reset_color%}" - echo "${branch}" + local branch=$(git_current_branch) + [ $(echo -n "${branch}" | wc -c) -gt 15 ] && branch="${branch:0:15}%{$fg[red]%}$(printf '\uf141')%{$reset_color%}" + echo "${branch}" } function build_git_prompt() { - # Allow hiding the right side of the prompt - (test "${NO_RIGHT}" = "true") && return + # Allow hiding the right side of the prompt + (test "${NO_RIGHT}" = "true") && return - # When there is no git, don't show a git prompt - git branch >/dev/null 2>/dev/null || return + # When there is no git, don't show a git prompt + git branch >/dev/null 2>/dev/null || return - # Fetch status of the current repo - local INDEX=$(command git status --porcelain -b 2>/dev/null) - local REMOTE=$(command git remote -v | grep fetch) + # Fetch status of the current repo + local INDEX=$(command git status --porcelain -b 2>/dev/null) + local REMOTE=$(command git remote -v | grep fetch) - echo -n "${bracket_open} %{$fg[blue]%}" + echo -n "${bracket_open} %{$fg[blue]%}" - # Mark specific remotes - case "${REMOTE}" in - *bitbucket.org*) - printf '\ue703 ' - ;; - *github.com*) - printf '\ue709 ' - ;; - *gitlab.com*) - printf '\uf296 ' - ;; - *) - printf '\uf1d3 ' - ;; - esac + # Mark specific remotes + case "${REMOTE}" in + *bitbucket.org*) + printf '\ue703 ' + ;; + *github.com*) + printf '\ue709 ' + ;; + *gitlab.com*) + printf '\uf296 ' + ;; + *) + printf '\uf1d3 ' + ;; + esac - # Show current branch and commit / tag - echo -n "%{$reset_color%}" - echo -n "$(shortened_branch) " - echo -n "($(git_describe)) " + # Show current branch and commit / tag + echo -n "%{$reset_color%}" + echo -n "$(shortened_branch) " + echo -n "($(git_describe)) " - # Print repository status information - [ $(echo "$INDEX" | wc -l) -gt 1 ] && echo -n "%{$fg[red]%}$(printf '\uf0f6')%{$FG[236]%} " + # Print repository status information + [ $(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 - if (echo "$INDEX" | grep '^## .*ahead.*behind' &>/dev/null); then - echo -n "%{$fg[red]%}$(printf '\uf047')" - elif (echo "$INDEX" | grep '^## .*ahead' &>/dev/null); then - echo -n "%{$fg[green]%}$(printf '\uf061')" - elif (echo "$INDEX" | grep '^## .*behind' &>/dev/null); then - echo -n "%{$fg[yellow]%}$(printf '\uf060')" - else - echo -n "%{$fg[green]%}=" - fi + # Show difference to remote + if (echo "$INDEX" | grep '^## .*ahead.*behind' &>/dev/null); then + echo -n "%{$fg[red]%}$(printf '\uf047')" + elif (echo "$INDEX" | grep '^## .*ahead' &>/dev/null); then + echo -n "%{$fg[green]%}$(printf '\uf061')" + elif (echo "$INDEX" | grep '^## .*behind' &>/dev/null); then + echo -n "%{$fg[yellow]%}$(printf '\uf060')" + else + echo -n "%{$fg[green]%}=" + fi - echo -n "%{$reset_color%} ${bracket_close}" + echo -n "%{$reset_color%} ${bracket_close}" } function prompt_gomod() { - local mod_default="auto" - local mod=${GO111MODULE:-${mod_default}} + local mod_default="auto" + 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)' @@ -114,9 +129,10 @@ local prompt_part_exit="%(?..${bracket_open} %{$fg[red]%}%?%{${reset_color}%} ${ local prompt_part_char='$(prompt_char)' local prompt_part_configgit='$(config-git_prompt)' local prompt_part_gomod='$(prompt_gomod)' +local prompt_part_kubectx='$(prompt_kubectx)' 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}"