1 #!bash Coloured prompts.
2 # To use, add a call to __ps1 in your .bash_profile file.
4 # The prompt comprises multiple parts, some of which may be hidden by unsetting
5 # shell variables or using the ``prompt'' function.
7 # The first part of the prompt is user@host where host is highlighted in
8 # green if the last command exited 0 or in red otherwise.
9 # This part will be shown only if __ps1_user is 1. By default it is 1.
10 # The success and failure colours can be changed by modifying
11 # $PROMPT_OK_COLOUR and $PROMPT_FAILURE_COLOUR respectively.
13 # The second part of the prompt is taken from git-completion.bashrc.
14 # It is shown only if __ps1_git is 1. By default it is 0.
16 # The third part of the prompt is taken from p4-completion.bashrc.
17 # It is shown only if __ps1_p4 is 1. By default it is 0.
19 # The fourth part of the prompt is taken from svn-completion.bashrc.
20 # It is shown only if __ps1_svn is 1. By default it is 0.
22 # The fifth part of the prompt is the exit status of the last command.
23 # This part will be shown only if __ps1_user is set and the exit status is
26 # The final part of the prompt is the (full) working directory and $ string.
28 # Colouring is performed by the __ps1_col() and __ps1_ret() functions.
29 # We redirect stderr to /dev/null when calling these functions to prevent
30 # bash complaining about not knowing them when you su to another user,
31 # retaining PS1 but not the function definitions.
33 # Note that $? is passed as an argument to - and is returned from - all
34 # functions. As $? is set following any shell activity it is only guaranteed
35 # to represent the return code of the last command at the beginning of __ps1().
36 # By passing between subsequent functions we ensure that it is available for
40 # Pick a colour based on the terminal capabilities.
45 case $(tput colors) in
47 export PROMPT_OK_COLOUR="1;38;5;34"
48 export PROMPT_FAILED_COLOUR="1;38;5;160"
49 export GIT_COLOUR="0;38;5;33"
50 export SVN_COLOUR="0;38;5;127"
51 export P4_COLOUR="0;38;5;142"
55 export PROMPT_OK_COLOUR="1;38;5;24"
56 export PROMPT_FAILED_COLOUR="1;38;5;48"
57 export GIT_COLOUR="0;38;5;23"
58 export SVN_COLOUR="0;38;5;49"
59 export P4_COLOUR="0;38;5;56"
63 export PROMPT_OK_COLOUR="1;32"
64 export PROMPT_FAILED_COLOUR="1;31"
65 export GIT_COLOUR="0;36"
66 export SVN_COLOUR="0;35"
67 export P4_COLOUR="0;33"
72 # Default __ps1_user to 1.
73 [ -z "$__ps1_user" ] && __ps1_user=1
75 export PS1='$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[${GIT_COLOUR}m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[${P4_COLOUR}m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[${SVN_COLOUR}m\]$(__ps1_svn $? 2>/dev/null)\[\033[0m\]$(__ps1_ret $? 2>/dev/null):\w\$ '
79 function __ps1_col() {
80 [ $1 -gt 0 ] && echo -n "$PROMPT_FAILED_COLOUR" || echo -n "$PROMPT_OK_COLOUR"
84 function __ps1_ret() {
85 [ "$__ps1_user" = "1" ] || return $1
86 [ $1 -gt 0 ] && echo -n " ($1)"
90 function __ps1_user() {
92 [ "$__ps1_user" = "1" ] || return $ret
97 function __ps1_git() {
98 [ "$__ps1_git" = "1" ] || return $1
99 if [ "$__ps1_user" = "1" ]; then
107 function __ps1_p4() {
108 [ "$__ps1_p4" = "1" ] || return $1
109 if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" ]; then
117 function __ps1_svn() {
118 [ "$__ps1_svn" = "1" ] || return $1
119 if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" -o "$__ps1_p4" = "1" ]; then
128 local blurb="Usage: prompt hide|show <what>"
130 if [ $# -lt 2 ]; then
136 if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
140 if [ "$action" = "hide" ]; then
146 what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
147 eval __ps1_$what=$action