X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=.profile.d%2Fps1.bashrc;h=528b49d67791a7b8aea6e4dc7bf75c0b21bd73ec;hb=884267902e73908af24a211c7ae9205702d09a6e;hp=b65e42d8447e3c3b91a00d4ddec329ed548ae59a;hpb=0614eba6741fe0b5f1ef54ad7478fe003ca0e613;p=profile.git diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index b65e42d..528b49d 100644 --- a/.profile.d/ps1.bashrc +++ b/.profile.d/ps1.bashrc @@ -1,5 +1,5 @@ #!bash Coloured prompts. -# To use, add a call to __ps1 in your .bash_profile file. +# profile-required: TERM.bashrc # # The prompt comprises multiple parts, some of which may be hidden by unsetting # shell variables or using the ``prompt'' function. @@ -8,7 +8,7 @@ # green if the last command exited 0 or in red otherwise. # This part will be shown only if __ps1_user is 1. By default it is 1. # The success and failure colours can be changed by modifying -# $PROMPT_OK_COLOUR and $PROMPT_FAILURE_COLOUR respectively. +# $PROMPT_OK_COLOUR and $PROMPT_FAILED_COLOUR respectively. # # The second part of the prompt is taken from git-completion.bashrc. # It is shown only if __ps1_git is 1. By default it is 0. @@ -24,6 +24,8 @@ # non-zero. # # The final part of the prompt is the (full) working directory and $ string. +# If the shell is running as root the # string's colour can be changed by +# modifying $ROOT_OK_COLOUR and $ROOT_FAILED_COLOUR. # # Colouring is performed by the __ps1_col() and __ps1_ret() functions. # We redirect stderr to /dev/null when calling these functions to prevent @@ -47,6 +49,8 @@ case $(tput colors) in 256) PROMPT_OK_COLOUR="1;38;5;34" PROMPT_FAILED_COLOUR="1;38;5;160" + ROOT_OK_COLOUR="0" + ROOT_FAILED_COLOUR="0" GIT_COLOUR="0;38;5;33" SVN_COLOUR="0;38;5;127" P4_COLOUR="0;38;5;142" @@ -55,6 +59,8 @@ case $(tput colors) in 88) PROMPT_OK_COLOUR="1;38;5;24" PROMPT_FAILED_COLOUR="1;38;5;48" + ROOT_OK_COLOUR="0" + ROOT_FAILED_COLOUR="0" GIT_COLOUR="0;38;5;23" SVN_COLOUR="0;38;5;49" P4_COLOUR="0;38;5;56" @@ -63,6 +69,8 @@ case $(tput colors) in *) PROMPT_OK_COLOUR="1;32" PROMPT_FAILED_COLOUR="1;31" + ROOT_OK_COLOUR="0" + ROOT_FAILED_COLOUR="0" GIT_COLOUR="0;36" SVN_COLOUR="0;35" P4_COLOUR="0;33" @@ -73,7 +81,7 @@ function __ps1() { # Default __ps1_user to 1. [ -z "$__ps1_user" ] && __ps1_user=1 - PS1='$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[0m\]$(__ps1_ret $? 2>/dev/null):\w\$ ' + PS1='\[\033[0m\]$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[0m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_colon $?)\w\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] ' return 0 } @@ -97,6 +105,16 @@ function __ps1_col() { return $ret } +function __ps1_root() { + local ret=$1; shift + if [ $ret -gt 0 ]; then + __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR" + else + __ps1_colour_escape $ret "$ROOT_OK_COLOUR" + fi + return $ret +} + function __ps1_ret() { [ "$__ps1_user" = "1" ] || return $1 [ $1 -gt 0 ] && echo -n " ($1)" @@ -140,6 +158,12 @@ function __ps1_svn() { return $1 } +function __ps1_colon() { + local all="$__ps1_user$__ps1_git$__ps1_p4$__ps1_svn" + [ "${all/1/}" = "$all" ] || echo -n ":" + return $1 +} + function prompt() { local blurb="Usage: prompt hide|show " @@ -162,3 +186,5 @@ function prompt() { what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')" eval __ps1_$what=$action } + +__ps1