Skip to content

Instantly share code, notes, and snippets.

@chriskirkland chriskirkland/.bashrc
Last active May 12, 2017

Embed
What would you like to do?
Bash prompt with kubernetes & git decorations
# Based on http://www.terminally-incoherent.com/blog/2013/01/14/whats-in-your-bash-prompt/
# Requires Bash >= 4.2
# Colors
Color_Off='\033[0m'
Red='\033[0;31m'
Green='\033[0;32m'
Purple='\033[0;35m'
LightBlue='\e[94m'
Dim='\e[2m'
# Symbols
CheckSym="\u2714"
ExSym="\u2718"
HelmSym="\u2388"
NullSym="\u2205"
# setup command prompt
function __prompt_command()
{
# capture the exit status of the last command
local EXIT="$?"
PS1='> '
local PROMPT=""
# previous command exit code indicator
if [ $EXIT -eq 0 ]; then PROMPT+="${Green}${CheckSym}${Color_Off} "; else PROMPT+="${Red}${ExSym}${Color_Off} "; fi
local kubernetes_context=$(kubectl config current-context 2>&1)
if [[ "$kubernetes_context" =~ "error: current-context is not set" ]]; then
PROMPT+="${Dim}${HelmSym} (${NullSym})${Color_Off} "
else
PROMPT+="${LightBlue}${HelmSym} (${kubernetes_context})${Color_Off} "
fi
# check if inside git repo
local git_status=$(git status -unormal 2>&1)
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
# parse the porcelain output of git status
if [[ "$git_status" =~ nothing\ to\ commit ]]; then
local Color_On=$Green
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
local Color_On=$Purple
else
local Color_On=$Red
fi
local remote=$(git config --get remote.origin.url)
local repo=$(git config --get remote.origin.url | cut -d'/' -f2 | cut -d'.' -f1) # default; ssh git source
if echo $remote | grep -q https; then # https git source
repo=$(git config --get remote.origin.url | cut -d'/' -f5 | cut -d'.' -f1)
fi
if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
local branch=${BASH_REMATCH[1]}
else
# Detached HEAD. (branch=HEAD is a faster alternative.)
local branch=$(git describe --all --contains --abbrev=4 HEAD 2> /dev/null || echo HEAD)
fi
# add the result to prompt
PROMPT+="${repo}:(${Color_On}${branch}${Color_Off}) "
else
# basic information (path)
PROMPT+=$(dirs -0)
fi
echo -e "$PROMPT"
}
PROMPT_COMMAND=__prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.