Skip to content

Instantly share code, notes, and snippets.

@canhnht
Last active June 13, 2018 22:31
Show Gist options
  • Save canhnht/ec05b7f40be0dceb8e2bb4662083d280 to your computer and use it in GitHub Desktop.
Save canhnht/ec05b7f40be0dceb8e2bb4662083d280 to your computer and use it in GitHub Desktop.
Set color bash prompt according to active virtualenv, git branch and return status of last command.
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
undblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
badgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
# The various escape codes that we can use to color our prompt.
RED="\[\033[0;31m\]"
YELLOW="\[\033[1;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[1;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
# Detect whether the current directory is a git repository.
function is_git_repository {
git branch > /dev/null 2>&1
}
# Determine the branch/state information for this git repository.
function set_git_branch {
# Capture the output of the "git status" command.
git_status="$(git status 2> /dev/null)"
# Set color based on clean/staged/dirty.
if [[ ${git_status} =~ "working directory clean" ]]; then
state="${bldgrn}"
elif [[ ${git_status} =~ "Changes to be committed" ]]; then
state="${bldylw}"
else
state="${LIGHT_RED}"
fi
# Set arrow icon based on status against remote.
remote_pattern="Your branch is (.*) (of|by|with)"
if [[ ${git_status} =~ ${remote_pattern} ]]; then
# echo "ofof = ${BASH_REMATCH[1]}"
if [[ ${BASH_REMATCH[1]} == ahead* ]]; then
remote="↑"
elif [[ ${BASH_REMATCH[1]} == behind* ]]; then
remote="↓"
else
remote="--"
fi
else
remote=""
fi
diverge_pattern="Your branch and (.*) have diverged"
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="↕"
fi
# Get the name of the branch.
branch_pattern="^(# )?On branch ([^${IFS}]*)"
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[2]}
fi
branch="$(git rev-parse --abbrev-ref HEAD)" # replace all 4 lines with this.
# Set the final branch string.
# echo "bran ${branch}"
BRANCH="${state}(${branch})${remote}${COLOR_NONE} "
}
# Return the prompt symbol to use, colorized based on the return value of the
# previous command.
function set_prompt_symbol () {
if test $1 -eq 0 ; then
PROMPT_SYMBOL="${GREEN}<\$>${COLOR_NONE}"
else
PROMPT_SYMBOL="${LIGHT_RED}<\$>${COLOR_NONE}"
fi
}
# Determine active Python virtualenv details.
function set_virtualenv () {
if test -z "$VIRTUAL_ENV" ; then
PYTHON_VIRTUALENV=""
else
PYTHON_VIRTUALENV="${BLUE}[`basename \"$VIRTUAL_ENV\"`]${COLOR_NONE} "
fi
}
# Set the full bash prompt.
function set_bash_prompt () {
# Set the PROMPT_SYMBOL variable. We do this first so we don't lose the
# return value of the last command.
set_prompt_symbol $?
# Set the PYTHON_VIRTUALENV variable.
set_virtualenv
# Set the BRANCH variable.
if is_git_repository ; then
# echo "is git repo"
set_git_branch
else
BRANCH=''
fi
SHOW_FACE="\`if [ \$? = 0 ]; then echo \[\e[33m\]^_^\[\e[0m\]; else echo \[\e[31m\]O_O\[\e[0m\]; fi\`"
PS1="\n${PYTHON_VIRTUALENV}${GREEN}${txtred}\u@\h:${bldcyn}[\w]:${bldgrn}${BRANCH}\n${SHOW_FACE}..${PROMPT_SYMBOL} "
}
PROMPT_COMMAND=set_bash_prompt
@axxs
Copy link

axxs commented Jun 13, 2018

^_^ works for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment