Skip to content

Instantly share code, notes, and snippets.

@Boboss74
Last active January 7, 2020 13:26
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 Boboss74/caf570e61707353630f1714929113126 to your computer and use it in GitHub Desktop.
Save Boboss74/caf570e61707353630f1714929113126 to your computer and use it in GitHub Desktop.
Git colored prompt
# This is a fork of a fork of a fork... of probably https://www.marc-eymard.fr/linux/bash-prompt
# Add the following lines inside your ~/.bashrc
# START : git, coloration
if [ -z "$PS1_ORIG" ]; then
PS1_ORIG=$PS1
OLD_PROMPT_COMMAND=$PROMPT_COMMAND
fi
RED='\001\033[31m\002'
YELLOW='\001\033[33m\002'
GREEN='\001\033[32m\002'
BLUE='\001\033[34m\002'
DARK_BLUE='\001\033[36m\002'
COLOR_NONE='\001\033[00m\002'
function parse_git_branch {
local AHEAD='↑'
local LATE='↓'
local DIVERGE='↕'
local NOT_CLEAN='!'
git rev-parse --git-dir &> /dev/null
if [ $? != 0 ]; then
echo "NOT_A_GIT_REPO"
else
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*)"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status} =~ "working tree clean" ]]; then
state="${RED}${NOT_CLEAN}"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} =~ "ahead of" ]]; then
remote="${YELLOW}${AHEAD}"
else
remote="${YELLOW}${LATE}"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}${DIVERGE}"
fi
branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
echo " (${branch})${remote}${state}"
fi
}
function prompt_func() {
previous_return_value=$?;
local git_result=$(parse_git_branch)
if [ "$git_result" == "NOT_A_GIT_REPO" ]; then
eval ${OLD_PROMPT_COMMAND}
PS1=$PS1_ORIG
else
local GIT_TOPLEVEL=$(git rev-parse --show-toplevel)
local prompt="${BLUE}[${RED}${PWD/#${GIT_TOPLEVEL%/*}/}${GREEN}${git_result}${BLUE}]${COLOR_NONE}"
local SEP="➔"
eval ${OLD_PROMPT_COMMAND} #Set the title value
if test $previous_return_value -eq 0; then
PS1="${prompt}${SEP}${COLOR_NONE} "
else
PS1="${prompt}${RED}${SEP}${COLOR_NONE} "
fi
fi
}
PROMPT_COMMAND=prompt_func
# END : git, coloration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment