Thatbytes

Mostly Interesting

Fun With Bash Prompts

After reading Oliver’s blog about cool bash prompts i thought id give it a go. I wanted a way to also get feedback about my current status of my git branch.

After hacking around for ten minutes i came up with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function _git_prompt() {
  local git_status="`git status -unormal 2>&1`"
         if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
             if [[ "$git_status" =~ nothing\ to\ commit ]]; then
                 local ansi=32
             elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
                 local ansi=34
             else
                 local ansi=33
             fi
             echo -n '\[\e[0;33;'"$ansi"'m\]'"$(__git_ps1)"'\[\e[0m\]'
         fi
     }
 function _prompt_command() {                                                                                                                                                                               
 PS1="`if [ \$? = 0 ]; then echo -n '\[\e[0;33;32m\]^-^';else echo -n '\[\e[0;33;33m\]-_-';fi`\[\033[00m\] \u \[\033[36m\]\w `_git_prompt`\[\033[37m\]$\[\033[00m\]"
 }
 PROMPT_COMMAND=_prompt_command

Basically this will change the colour of the branch name depending of the status of the checkout out. Screen shot below:

Comments