1 #!bash Coloured prompts.
2 # profile-required: TERM.bashrc
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_FAILED_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 sixth part of the prompt is the number of background jobs managed
27 # by the shell, in square brackets. If all background jobs are running,
28 # their number will be shown as [n]. If some are stopped, the number of
29 # running (r) and total (t) jobs will be shown as [r/t].
30 # This part will be shown only if __ps1_bg is 1. By default it is 0.
32 # The final part of the prompt is the (full) working directory and $ string.
33 # If the shell is running as root the # string's colour can be changed by
34 # modifying $ROOT_OK_COLOUR and $ROOT_FAILED_COLOUR.
36 # Colouring is performed by the __ps1_col() and __ps1_ret() functions.
37 # We redirect stderr to /dev/null when calling these functions to prevent
38 # bash complaining about not knowing them when you su to another user,
39 # retaining PS1 but not the function definitions.
41 # Note that $? is passed as an argument to - and is returned from - all
42 # functions. As $? is set following any shell activity it is only guaranteed
43 # to represent the return code of the last command at the beginning of __ps1().
44 # By passing between subsequent functions we ensure that it is available for
48 # Pick a colour based on the terminal capabilities.
54 case $(tput colors) in
56 PROMPT_BACKGROUND_COLOUR="0;48;5;26"
57 PROMPT_OK_COLOUR="1;38;5;34"
58 PROMPT_FAILED_COLOUR="1;38;5;160"
60 ROOT_FAILED_COLOUR="0"
61 GIT_COLOUR="0;38;5;33"
62 SVN_COLOUR="0;38;5;127"
63 P4_COLOUR="0;38;5;142"
67 PROMPT_BACKGROUND_COLOUR="0;48;5;18"
68 PROMPT_OK_COLOUR="1;38;5;24"
69 PROMPT_FAILED_COLOUR="1;38;5;48"
71 ROOT_FAILED_COLOUR="0"
72 GIT_COLOUR="0;38;5;23"
73 SVN_COLOUR="0;38;5;49"
78 PROMPT_BACKGROUND_COLOUR="0;44"
79 PROMPT_OK_COLOUR="1;32"
80 PROMPT_FAILED_COLOUR="1;31"
82 ROOT_FAILED_COLOUR="0"
90 # Default __ps1_user to 1.
91 [ -z "$__ps1_user" ] && __ps1_user=1
93 PS1='\[\033[$(__ps1_background $?)m\]$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR $PROMPT_BACKGROUND_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[$(__ps1_background $?)m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_bg $?)$(__ps1_colon $?)$(__ps1_short $?)\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] '
97 # iTerm doesn't like it if you set bold and colour at the same time.
98 function __ps1_colour_escape() {
100 local bold="${1%%;*}"
101 local colour="${1#*;}"
102 local bgcolour="${2#*;}"
104 echo -en "${bold}m\033[$colour"
105 [ "$__ps1_background" = 1 ] && echo -en "m\033[$bgcolour"
109 function __ps1_background() {
112 [ "$__ps1_background" = 1 ] && echo -en "m\033[${PROMPT_BACKGROUND_COLOUR}"
116 function __ps1_col() {
118 if [ $ret -gt 0 ]; then
119 __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
121 __ps1_colour_escape $ret "$PROMPT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
126 function __ps1_root() {
128 if [ $ret -gt 0 ]; then
129 __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
131 __ps1_colour_escape $ret "$ROOT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
136 function __ps1_ret() {
137 [ "$__ps1_user" = "1" ] || return $1
138 [ $1 -gt 0 ] && echo -n " ($1)"
142 function __ps1_user() {
144 [ "$__ps1_user" = "1" ] || return $ret
149 function __ps1_git() {
150 [ "$__ps1_git" = "1" ] || return $1
151 if [ "$__ps1_user" = "1" ]; then
159 function __ps1_p4() {
160 [ "$__ps1_p4" = "1" ] || return $1
161 if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" ]; then
169 function __ps1_svn() {
170 [ "$__ps1_svn" = "1" ] || return $1
171 if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" -o "$__ps1_p4" = "1" ]; then
179 function __ps1_bg() {
180 [ "$__ps1_bg" = "1" ] || return $1
182 local running=0; for job in $(builtin jobs -pr); do running=$((running+1)); done
183 local total=0; for job in $(builtin jobs -p); do total=$((total+1)); done
184 [ $total = 0 ] && return $1
186 [ "$__ps1_user" = "1" ] && echo -n " "
188 [ $running = $total ] || echo -n "$running/"
196 function __ps1_colon() {
197 local all="$__ps1_user$__ps1_git$__ps1_p4$__ps1_svn$(__ps1_bg $1 1)"
198 [ "${all/1/}" = "$all" ] || echo -n ":"
202 function __ps1_short() {
203 local home=${HOME%%/}
204 local pwd=${PWD/#$home/\~}
205 local dirtrim=${PROMPT_DIRTRIM//[^0-9]/}
207 if [ "${dirtrim:0:1}" = "0" ]; then
212 dirtrim=${dirtrim##0}
213 if [ -z "$dirtrim" ]; then
214 local prompt="$USER$HOSTNAME$pwd"
215 local width=$(((COLUMNS*2)/3))
216 if [ ${#prompt} -le ${width:-53} ]; then
224 local dirname=${pwd##*/}
225 local basename=${pwd%/$dirname}
228 for component in ${basename//\// }; do
229 reversed="$component $reversed"
233 for component in $reversed; do
234 [ $n = 1 -a "$PWD" = "$pwd" ] || short="/$short"
235 if [ $n -ge $dirtrim ]; then
236 short="${component:0:1}$short"
238 short="$component$short"
243 echo "$short$dirname"
248 local blurb="Usage: prompt hide|show <what>"
250 if [ $# -lt 2 ]; then
256 if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
260 if [ "$action" = "hide" ]; then
266 what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
267 eval __ps1_$what=$action