Skip to content

Instantly share code, notes, and snippets.

@virgild
Created February 10, 2011 16:49
Show Gist options
  • Save virgild/820869 to your computer and use it in GitHub Desktop.
Save virgild/820869 to your computer and use it in GitHub Desktop.
#!/bin/bash
function parse_git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "("${ref#refs/heads/}")"
}
# Color codes
RED='\[\033[01;31m\]'
GREEN='\[\033[01;32m\]'
YELLOW='\[\033[01;33m\]'
BLUE='\[\033[01;34m\]'
PURPLE='\[\033[01;35m\]'
CYAN='\[\033[01;36m\]'
WHITE='\[\033[01;37m\]'
NIL='\[\033[00m\]'
RESET="\[\033[0m\]"
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
unkblk='\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
woot='\[\e[43m\]\[\e[3;30m\]'
txtrst='\e[0m' # Text Reset
# Hostname styles
FULL='\H'
SHORT='\h'
# System => color/hostname map:
# UC: username color
# LC: location/cwd color
# HD: hostname display (\h vs \H)
# Defaults:
UC=$BLUE
LC=$GREEN
HD=$FULL
# Multiplatform sed tomfoolery used below (extended regexp)
case $(uname -s) in
# -E flag, and override macports sed=>gsed alias
Darwin ) alias esed='/usr/bin/sed -E' ;;
# -r flag with GNU sed on Linux
Linux ) alias esed='sed -r' ;;
esac
# Prompt function because PROMPT_COMMAND is awesome
function set_prompt() {
# If logged in as another user, not gonna have all this firing anyway.
# So let's just show the host only and be done with it.
host="${woot}${HD}${NIL}"
# Special vim-tab-like shortpath (~/folder/directory/foo => ~/f/d/foo)
_pwd=`pwd | sed "s#$HOME#~#"`
if [[ $_pwd == "~" ]]; then
_dirname=$_pwd
else
_dirname=`dirname "$_pwd" | esed "s/\/(.)[^\/]*/\/\1/g"`
if [[ $_dirname == "/" ]]; then
_dirname=""
fi
_dirname="$_dirname/`basename "$_pwd"`"
fi
path="${LC}${_dirname}${NIL}"
# Git branch / dirtiness
# Dirtiness cribbed from:
# http://henrik.nyh.se/2008/12/git-dirty-prompt#comment-8325834
if git update-index -q --refresh 2>/dev/null; git diff-index --quiet --cached HEAD --ignore-submodules -- 2>/dev/null && git diff-files --quiet --ignore-submodules 2>/dev/null
then dirty=""
else
dirty="${RED}*${NIL}"
fi
_branch=$(git symbolic-ref HEAD 2>/dev/null)
_branch=${_branch#refs/heads/} # apparently faster than sed
branch="" # need this to clear it when we leave a repo
if [[ -n $_branch ]]; then
branch=" ${NIL}[${PURPLE}${_branch}${dirty}${NIL}]"
fi
# Dollar/pound sign
end="${LC}\$${NIL} "
# Feels kind of like cheating...but works so well!
export PS1="${host}:${path}${venv}${branch} ${end}"
#export PS1="${path}${venv} ${end}"
}
export PROMPT_COMMAND=set_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment