Set PROMPT_DIRTRIM dynamically.
[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 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.
31
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.
35 #
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.
40 #
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 
45 # __ps1_ret().
46 #
47
48 # Pick a colour based on the terminal capabilities.
49 # OK: dark green.
50 # Failed: dark red.
51 # Git: royal blue.
52 # P4: yellow.
53 # SVN: magenta.
54 case $(tput colors) in
55   256)
56     PROMPT_BACKGROUND_COLOUR="0;48;5;26"
57     PROMPT_OK_COLOUR="1;38;5;34"
58     PROMPT_FAILED_COLOUR="1;38;5;160"
59     ROOT_OK_COLOUR="0"
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"
64   ;;
65
66   88)
67     PROMPT_BACKGROUND_COLOUR="0;48;5;18"
68     PROMPT_OK_COLOUR="1;38;5;24"
69     PROMPT_FAILED_COLOUR="1;38;5;48"
70     ROOT_OK_COLOUR="0"
71     ROOT_FAILED_COLOUR="0"
72     GIT_COLOUR="0;38;5;23"
73     SVN_COLOUR="0;38;5;49"
74     P4_COLOUR="0;38;5;56"
75   ;;
76
77   *)
78     PROMPT_BACKGROUND_COLOUR="0;44"
79     PROMPT_OK_COLOUR="1;32"
80     PROMPT_FAILED_COLOUR="1;31"
81     ROOT_OK_COLOUR="0"
82     ROOT_FAILED_COLOUR="0"
83     GIT_COLOUR="0;36"
84     SVN_COLOUR="0;35"
85     P4_COLOUR="0;33"
86   ;;
87 esac
88
89 function __ps1() {
90   # Default __ps1_user to 1.
91   [ -z "$__ps1_user" ] && __ps1_user=1
92
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\] '
94   return 0
95 }
96
97 # iTerm doesn't like it if you set bold and colour at the same time.
98 function __ps1_colour_escape() {
99   local ret=$1; shift
100   local bold="${1%%;*}"
101   local colour="${1#*;}"
102   local bgcolour="${2#*;}"
103
104   echo -en "${bold}m\033[$colour"
105   [ "$__ps1_background" = 1 ] && echo -en "m\033[$bgcolour"
106   return $ret
107 }
108
109 function __ps1_background() {
110   local ret=$1; shift
111   echo -n "0"
112   [ "$__ps1_background" = 1 ] && echo -en "m\033[${PROMPT_BACKGROUND_COLOUR}"
113   return $ret
114 }
115
116 function __ps1_col() {
117   local ret=$1; shift
118   if [ $ret -gt 0 ]; then
119     __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
120   else
121     __ps1_colour_escape $ret "$PROMPT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
122   fi
123   return $ret
124 }
125
126 function __ps1_root() {
127   local ret=$1; shift
128   if [ $ret -gt 0 ]; then
129     __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
130   else
131     __ps1_colour_escape $ret "$ROOT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
132   fi
133   return $ret
134 }
135
136 function __ps1_ret() {
137   [ "$__ps1_user" = "1" ] || return $1
138   [ $1 -gt 0 ] && echo -n " ($1)"
139   return $1
140 }
141
142 function __ps1_user() {
143   local ret=$1; shift
144   [ "$__ps1_user" = "1" ] || return $ret
145   echo -n ${1+"$@"}
146   return $ret
147 }
148
149 function __ps1_git() {
150   [ "$__ps1_git" = "1" ] || return $1
151   if [ "$__ps1_user" = "1" ]; then
152     __git_ps1 ' %s'
153   else
154     __git_ps1 '%s'
155   fi
156   return $1
157 }
158
159 function __ps1_p4() {
160   [ "$__ps1_p4" = "1" ] || return $1
161   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" ]; then
162     __p4_ps1 ' %s'
163   else
164     __p4_ps1 '%s'
165   fi
166   return $1
167 }
168
169 function __ps1_svn() {
170   [ "$__ps1_svn" = "1" ] || return $1
171   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" -o "$__ps1_p4" = "1" ]; then
172     __svn_ps1 ' %s'
173   else
174     __svn_ps1 '%s'
175   fi
176   return $1
177 }
178
179 function __ps1_bg() {
180   [ "$__ps1_bg" = "1" ] || return $1
181   local job
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
185   if [ -z "$2" ]; then
186     [ "$__ps1_user" = "1" ] && echo -n " "
187     echo -n "["
188     [ $running = $total ] || echo -n "$running/"
189     echo -n "$total]"
190   else
191     echo $2
192   fi
193   return $1
194 }
195
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 ":"
199   return $1
200 }
201
202 function __ps1_short() {
203   local dirtrim=${PROMPT_DIRTRIM//[^0-9]/}
204
205   if [ "${dirtrim:0:1}" = "0" ]; then
206     echo "$PWD"
207     return $1
208   fi
209
210   dirtrim=${dirtrim##0}
211   if [ -z "$dirtrim" ]; then
212     local prompt="$USER$HOSTNAME$PWD"
213     local width=$(((COLUMNS*2)/3))
214     if [ ${#prompt} -le ${width:-53} ]; then
215       echo "$PWD"
216       return $1
217     else
218       dirtrim=1
219     fi
220   fi
221
222   local dirname=${PWD##*/}
223   local basename=${PWD%/$dirname}
224   local reversed=
225   local component
226   for component in ${basename//\// }; do
227     reversed="$component $reversed"
228   done
229   local n=1
230   local short=
231   for component in $reversed; do
232     if [ $n -ge $dirtrim ]; then
233       short="/${component:0:1}$short"
234     else
235       short="/$component$short"
236     fi
237     n=$((n+1))
238   done
239
240   echo "$short/$dirname"
241   return $1
242 }
243
244 function prompt() {
245   local blurb="Usage: prompt hide|show <what>"
246
247   if [ $# -lt 2 ]; then
248     echo >&2 "$blurb"
249     return 1
250   fi
251
252   action="$1"
253   if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
254     echo >&2 "$blurb"
255     return 1
256   fi
257   if [ "$action" = "hide" ]; then
258     action=0
259   else
260     action=1
261   fi
262
263   what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
264   eval __ps1_$what=$action
265 }
266
267 __ps1