Colours for PS2 and PS4.
[profile.git] / .profile.d / ps1.bashrc
1 #!bash Coloured prompts.
2 # profile-required: TERM.bashrc
3 #
4 # The prompt comprises multiple parts, some of which may be hidden by unsetting 
5 # shell variables or using the ``prompt'' function.
6 #
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_FAILED_COLOUR respectively.
12 #
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.
15 #
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.
18 #
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.
21 #
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 
24 # non-zero.
25
26 # The final part of the prompt is the (full) working directory and $ string.
27 # If the shell is running as root the # string's colour can be changed by
28 # modifying $ROOT_OK_COLOUR and $ROOT_FAILED_COLOUR.
29 #
30 # Colouring is performed by the __ps1_col() and __ps1_ret() functions.
31 # We redirect stderr to /dev/null when calling these functions to prevent 
32 # bash complaining about not knowing them when you su to another user, 
33 # retaining PS1 but not the function definitions.
34 #
35 # Note that $? is passed as an argument to - and is returned from - all 
36 # functions.  As $? is set following any shell activity it is only guaranteed 
37 # to represent the return code of the last command at the beginning of __ps1().
38 # By passing between subsequent functions we ensure that it is available for 
39 # __ps1_ret().
40 #
41
42 # Pick a colour based on the terminal capabilities.
43 # OK: dark green.
44 # Failed: dark red.
45 # Git: royal blue.
46 # P4: yellow.
47 # SVN: magenta.
48 case $(tput colors) in
49   256)
50     PROMPT_OK_COLOUR="1;38;5;34"
51     PROMPT_FAILED_COLOUR="1;38;5;160"
52     ROOT_OK_COLOUR="0"
53     ROOT_FAILED_COLOUR="0"
54     GIT_COLOUR="0;38;5;33"
55     SVN_COLOUR="0;38;5;127"
56     P4_COLOUR="0;38;5;142"
57   ;;
58
59   88)
60     PROMPT_OK_COLOUR="1;38;5;24"
61     PROMPT_FAILED_COLOUR="1;38;5;48"
62     ROOT_OK_COLOUR="0"
63     ROOT_FAILED_COLOUR="0"
64     GIT_COLOUR="0;38;5;23"
65     SVN_COLOUR="0;38;5;49"
66     P4_COLOUR="0;38;5;56"
67   ;;
68
69   *)
70     PROMPT_OK_COLOUR="1;32"
71     PROMPT_FAILED_COLOUR="1;31"
72     ROOT_OK_COLOUR="0"
73     ROOT_FAILED_COLOUR="0"
74     GIT_COLOUR="0;36"
75     SVN_COLOUR="0;35"
76     P4_COLOUR="0;33"
77   ;;
78 esac
79
80 function __ps1() {
81   # Default __ps1_user to 1.
82   [ -z "$__ps1_user" ] && __ps1_user=1
83
84   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\] '
85   return 0
86 }
87
88 # iTerm doesn't like it if you set bold and colour at the same time.
89 function __ps1_colour_escape() {
90   local ret=$1; shift
91   local bold="${1%%;*}"
92   local colour="${1#*;}"
93
94   echo -en "${bold}m\033[$colour"
95   return $ret
96 }
97
98 function __ps1_col() {
99   local ret=$1; shift
100   if [ $ret -gt 0 ]; then
101     __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR"
102   else
103     __ps1_colour_escape $ret "$PROMPT_OK_COLOUR"
104   fi
105   return $ret
106 }
107
108 function __ps1_root() {
109   local ret=$1; shift
110   if [ $ret -gt 0 ]; then
111     __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR"
112   else
113     __ps1_colour_escape $ret "$ROOT_OK_COLOUR"
114   fi
115   return $ret
116 }
117
118 function __ps1_ret() {
119   [ "$__ps1_user" = "1" ] || return $1
120   [ $1 -gt 0 ] && echo -n " ($1)"
121   return $1
122 }
123
124 function __ps1_user() {
125   local ret=$1; shift
126   [ "$__ps1_user" = "1" ] || return $ret
127   echo -n ${1+"$@"}
128   return $ret
129 }
130
131 function __ps1_git() {
132   [ "$__ps1_git" = "1" ] || return $1
133   if [ "$__ps1_user" = "1" ]; then
134     __git_ps1 ' %s'
135   else
136     __git_ps1 '%s'
137   fi
138   return $1
139 }
140
141 function __ps1_p4() {
142   [ "$__ps1_p4" = "1" ] || return $1
143   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" ]; then
144     __p4_ps1 ' %s'
145   else
146     __p4_ps1 '%s'
147   fi
148   return $1
149 }
150
151 function __ps1_svn() {
152   [ "$__ps1_svn" = "1" ] || return $1
153   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" -o "$__ps1_p4" = "1" ]; then
154     __svn_ps1 ' %s'
155   else
156     __svn_ps1 '%s'
157   fi
158   return $1
159 }
160
161 function __ps1_colon() {
162   local all="$__ps1_user$__ps1_git$__ps1_p4$__ps1_svn"
163   [ "${all/1/}" = "$all" ] || echo -n ":"
164   return $1
165 }
166
167 function prompt() {
168   local blurb="Usage: prompt hide|show <what>"
169
170   if [ $# -lt 2 ]; then
171     echo >&2 "$blurb"
172     return 1
173   fi
174
175   action="$1"
176   if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
177     echo >&2 "$blurb"
178     return 1
179   fi
180   if [ "$action" = "hide" ]; then
181     action=0
182   else
183     action=1
184   fi
185
186   what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
187   eval __ps1_$what=$action
188 }
189
190 __ps1