dot/bashrc: Show Git branch name in the prompt, if machinery available.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 8 Jan 2015 14:12:46 +0000 (14:12 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 8 Jan 2015 14:12:46 +0000 (14:12 +0000)
dot/bashrc

index 7fa8e3a..fe4b375 100644 (file)
@@ -35,12 +35,13 @@ if [ -t 0 ]; then
        "") bold="\[$(tput md)\]" unbold="\[$(tput me)\]" ;;
        *) bold="\[$(tput bold)\]" unbold="\[$(tput sgr0)\]" ;;
       esac
+      gitcolour="\[$(tput setaf 6)\]"
       rccolour="\[$(tput setaf 1)\]"
       uncolour="\[$(tput op)\]"
       nl="\[\r\]"
       ;;
     *)
-      bold='' unbold='' nl='' rccolour='' uncolour='';;
+      bold='' unbold='' nl='' gitcolour='' rccolour='' uncolour='';;
   esac
 
   ## Choose the right delimiters.  Highlight root prompts specially;
@@ -69,12 +70,17 @@ if [ -t 0 ]; then
   fi
 
   ## Build the prompt string.
+  if type __git_ps1 >/dev/null 2>&1; then
+    git="$unbold$gitcolour\$(rc=\$?;__git_ps1;exit \$rc)$uncolour$bold"
+  else
+    git=""
+  fi
   rc="$unbold$rccolour\$(rc=\$?;case \$rc in 0);;"
   rc="$rc*)echo -n \" rc=\$rc\";;esac;exit \$rc)$uncolour$bold"
-  PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$rc$marker$right$unbold"
+  PS1="$nl$bold$left$sec_l$u\\h$sec_r \\w$git$rc$marker$right$unbold"
   PS2="$PS1 $bold>$unbold "
   unset nl bold unbold left right sec_l sec_r marker
-  unset rccolour uncolour rc
+  unset gitcolour rccolour uncolour git rc
 fi
 
 ###--------------------------------------------------------------------------