Fix missing slashes in short dir.
[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 home=${HOME%%/}
204   local pwd=${PWD/#$home/\~}
205   local dirtrim=${PROMPT_DIRTRIM//[^0-9]/}
206
207   if [ "${dirtrim:0:1}" = "0" ]; then
208     echo "$pwd"
209     return $1
210   fi
211
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
217       echo "$pwd"
218       return $1
219     else
220       dirtrim=1
221     fi
222   fi
223
224   local dirname=${pwd##*/}
225   local basename=${pwd%/$dirname}
226   local reversed=
227   local component
228   for component in ${basename//\// }; do
229     reversed="$component $reversed"
230   done
231   local n=1
232   local short=
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"
237     else
238       short="$component$short"
239     fi
240     n=$((n+1))
241   done
242
243   [ "${short:0:1}" = "~" ] || short="/$short"
244   echo "$short/$dirname"
245   return $1
246 }
247
248 function prompt() {
249   local blurb="Usage: prompt hide|show <what>"
250
251   if [ $# -lt 2 ]; then
252     echo >&2 "$blurb"
253     return 1
254   fi
255
256   action="$1"
257   if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
258     echo >&2 "$blurb"
259     return 1
260   fi
261   if [ "$action" = "hide" ]; then
262     action=0
263   else
264     action=1
265   fi
266
267   what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
268   eval __ps1_$what=$action
269 }
270
271 __ps1