Skip to content

Instantly share code, notes, and snippets.

@kentquirk
Last active August 29, 2015 14:21
Show Gist options
  • Save kentquirk/ba7aa06f6b673623e32f to your computer and use it in GitHub Desktop.
Save kentquirk/ba7aa06f6b673623e32f to your computer and use it in GitHub Desktop.
Bash prompt for displaying git/svn revision info
#################################################################
# Here's a bunch of stuff to do prompts that are sensitive to git and svn
WHT='\e[22;37m'
TEAL='\e[22;36m'
BLK='\e[22;30m'
RED='\e[22;31m'
GRN='\e[22;32m'
YEL='\e[22;33m'
BLU='\e[22;34m'
BRED='\e[1;31m'
BGRN='\e[1;32m'
BYEL='\e[1;33m'
BBLU='\e[1;34m'
BWHT='\e[1;37m'
BBLK='\e[1;30m'
BGBLK='\e[40m'
BGBLU='\e[44m'
BGYEL='\e[43m'
BGWHT='\e[47m'
BGRED='\e[41m'
KILLCOLOR='\e[0m'
TIME='\@'
USER='\u'
HOST='\h'
WORKINGDIR='\w'
CLR_FRAME=$BLK
CLR_DIR=$WHT
CLR_GIT=$BYEL
CLR_TIME=$BWHT
CLR_BG=$BGRED
CLR_VENV=$YEL
parse_git_branch() {
#git branch | grep \* | sed 's/* //'
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
parse_svn_revision() {
svn info 2>/dev/null | grep "Revision: "| cut -d ' ' -f 2
}
get_revision() {
svn info >/dev/null 2>&1 && parse_svn_revision || parse_git_branch
}
get_venv() {
if [ ! -z $VIRTUAL_ENV ]; then
echo " ($(basename $VIRTUAL_ENV))"
fi
}
# Only do a color prompt if we actually want color
case "$TERM" in
xterm-256color)
PS1="$CLR_BG $CLR_FRAME[$CLR_TIME$TIME$CLR_FRAME][$CLR_DIR$WORKINGDIR$CLR_VENV\$(get_venv)$CLR_FRAME][$CLR_GIT\$(get_revision)$CLR_FRAME] $KILLCOLOR\n\$ "
;;
*)
PS1="[ $TIME ][ $WORKINGDIR \$(get_venv) ][ \$(get_revision) ] \n\$ "
;;
esac
#################################################################
@kentquirk
Copy link
Author

Copy that to your .bash_profile, and you'll get a prompt that looks like this:

  [09:44 AM][~/go/src/github.com/kentquirk/atool2/datastore][addmongo]
$

But with color highlighting. The first part is the time, the second part is your current directory (with an optional virtualenv name if you use python's virtualenv), and the last part is either the current branch name for a git repository, or the current branch number in an svn repository.

You can change colors by messing with the CLR_* variables.

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