Hi all,
I've been trying to let my prompt (PS1) show what git branch I'm in, and if that branch is dirty or not (i.e. coloured in red or green). Have been able to do this using a ruby function that is read by PS1, but this is very slow because the ruby interpreter has to be run every time my terminal wants to show a prompt.
A screenshot of what I want (and achieved using the ruby version) is at
http://twitpic.com/1gubtf . The accompanying code is at
http://github.com/jandot/git-promptI've been trying to recode this into bash, but can't get it to work.
Here's my non-working code:
* .bash_profile contains:
Code:
parse_git_branch() {
source ./git-ps.sh 2>/dev/null
}
export PS1="[\w \$(parse_git_branch)]> "
* git-ps.sh is:
Code:
#!/bin/bash
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
RED="\033[1;31m"
BLUE="\033[1;34m"
RST="\033[0m"
CURRENT_BRANCH=`git branch 2>/dev/null | grep '*' | head -n1`
CURRENT_BRANCH=${CURRENT_BRANCH:2}
if [ $CURRENT_BRANCH != "" ] ; then
STATUS_LINES=`git status`
colour=$GREEN
if [[ "$STATUS_LINES" =~ "# Changed but not updated" ]] ; then
colour=$RED
elif [[ "$STATUS_LINES" =~ "# Changes to be committed" ]] ; then
colour=$YELLOW
elif [[ "$STATUS_LINES" =~ "# Untracked files" ]] ; then
colour=$BLUE
fi
git_part=" "$colour"("${CURRENT_BRANCH}")"$RST
else
git_part=''
fi
echo $git_part
Using this code, my prompt looks like this:
Code:
~/Projects/git-prompt \033[1;31m(bash)\033[0m>
Clearly there is an issue with escaping the colour codes somewhere, but I haven't been able to find what it is...
Any help would be greatly appreciated.
Thanks,
jan.