Split PS1 components into .ps1 files.
[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 # Subsequent parts are taken from *.ps1 files in the .ps1.d directory.
14 # Each file <plugin>.ps1 should contain:
15 # * A line __ps1_<plugin>=${__ps1_<plugin>:-X} where X is 0 or 1 to activate
16 #   the plugin by default (or not).
17 # * A function __ps1_<plugin>() which returns the text to be displayed.
18 #   The function will be passed the return code of the last command as its
19 #   first argument and should return the same code.
20 #   The function should print nothing unless __ps1_<plugin> is 1.
21 #   The function should call "__ps1_prefix $1 __ps1_<plugin>" to retrieve a
22 #   prefix string.  When the string is non-empty, it should be printed before
23 #   any output from the plugin itself.  Doing so will ensure that the text is
24 #   formatted correctly regardless of whether other parts of the prompt are
25 #   being shown.
26 # * One to three lines __ps1_<plugin>_colour256, __ps1_<plugin>_colour88 and
27 #   __ps1_<plugin>_colour setting the colour for the plugin.
28 # The plugin should not emit ANSI colour sequences itself.  PS1 needs to wrap
29 # all escape sequences with the literal strings \[ and \] but these must be
30 # embedded directly into the prompt and not evaluated programmatically.
31 # Omitting them will cause the terminal to redraw incorrectly under certain
32 # circumstances.  Trying to include them in a function will not work.
33 # Instead the plugin should define at least __ps1_<plugin>_colour.  The
34 # colour will be generated before any text from the plugin is printed.
35 # To handle 88- and 256-colour terminals, the plugin may also declare
36 # __ps1_<plugin>_colour88 and/or __ps1_<plugin>_colour256.
37 #
38 # Example plugin code:
39 #
40 # __ps1_example_colour='0;33'
41 #
42 # function __ps1_example() {
43 #   if [ -n "$__ps1_example" ]; then
44 #     echo -n "$(__ps1_prefix $1 __ps1_example)"
45 #     echo -n "example!"
46 #   fi
47 #   return $1
48 # }
49 #
50 # The next part of the prompt is the exit status of the last command.
51 # This part will be shown only if __ps1_user is set and the exit status is
52 # non-zero.
53 #
54 # The penultimate part of the prompt is the number of background jobs managed
55 # by the shell, in square brackets.  If all background jobs are running,
56 # their number will be shown as [n].  If some are stopped, the number of
57 # running (r) and total (t) jobs will be shown as [r/t].
58 # This part will be shown only if __ps1_bg is 1.  By default it is 0.
59
60 # The final part of the prompt is the (full) working directory and $ string.
61 # If the shell is running as root the # string's colour can be changed by
62 # modifying $ROOT_OK_COLOUR and $ROOT_FAILED_COLOUR.
63 #
64 # Colouring is performed by the __ps1_col() and __ps1_ret() functions.
65 # We redirect stderr to /dev/null when calling these functions to prevent
66 # bash complaining about not knowing them when you su to another user,
67 # retaining PS1 but not the function definitions.
68 #
69 # Note that $? is passed as an argument to - and is returned from - all
70 # functions.  As $? is set following any shell activity it is only guaranteed
71 # to represent the return code of the last command at the beginning of __ps1().
72 # By passing between subsequent functions we ensure that it is available for
73 # __ps1_ret().
74 #
75
76 # Pick a colour based on the terminal capabilities.
77 # OK: dark green.
78 # Failed: dark red.
79 case $(tput colors) in
80   256)
81     __ps1_colours=256
82     PROMPT_BACKGROUND_COLOUR="0;48;5;26"
83     PROMPT_OK_COLOUR="1;38;5;34"
84     PROMPT_FAILED_COLOUR="1;38;5;160"
85     ROOT_OK_COLOUR="0"
86     ROOT_FAILED_COLOUR="0"
87   ;;
88
89   88)
90     __ps1_colours=88
91     PROMPT_BACKGROUND_COLOUR="0;48;5;18"
92     PROMPT_OK_COLOUR="1;38;5;24"
93     PROMPT_FAILED_COLOUR="1;38;5;48"
94     ROOT_OK_COLOUR="0"
95     ROOT_FAILED_COLOUR="0"
96   ;;
97
98   *)
99     __ps1_colours=
100     PROMPT_BACKGROUND_COLOUR="0;44"
101     PROMPT_OK_COLOUR="1;32"
102     PROMPT_FAILED_COLOUR="1;31"
103     ROOT_OK_COLOUR="0"
104     ROOT_FAILED_COLOUR="0"
105   ;;
106 esac
107
108 __ps1_all='$__ps1_user'
109
110 function __ps1() {
111   # Default __ps1_user to 1.
112   [ -z "$__ps1_user" ] && __ps1_user=1
113
114   local pre='\[\033[$(__ps1_background $?)m\]$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[0m\]'
115   local post='\[\033[$(__ps1_background $?)m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_bg $?)$(__ps1_colon $?)$(__ps1_short $?)\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] '
116   local snippets=''
117   local snippet=
118   for snippet in ${PROFILE_HOME:-~}/.ps1.d/*.ps1; do
119     . $snippet
120     local name=${snippet##*/}
121     name=__ps1_${name%.ps1}
122     __ps1_all="$__ps1_all\$$name"
123     snippets="$snippets"'\[$(__ps1_colour_start $? '"$name"')\]$('"$name"' $? $__ps1_all 2>/dev/null)\[$(__ps1_colour_end $? '"$name"')\]'
124   done
125   PS1=$pre$snippets$post
126
127   return 0
128 }
129
130 function __ps1_colour_for() {
131   local colour=
132   local ret=
133   for colour in "${1}_colour${__ps1_colours}" "${1}_colour"; do
134     ret=$(eval echo -n "\$$colour")
135     [ -n "$ret" ] && break
136   done
137   echo -n $ret
138 }
139
140 function __ps1_prefix() {
141   local var=\$${2#\$}
142   local prefix=${__ps1_all%$var*}
143   local all="$(eval echo $prefix)"
144   [ "${all/1/}" = "$all" ] || echo -n " "
145   return $1
146 }
147
148 # iTerm doesn't like it if you set bold and colour at the same time.
149 function __ps1_colour_escape() {
150   local ret=$1; shift
151   local bold="${1%%;*}"
152   local colour="${1#*;}"
153   local bgcolour="${2#*;}"
154
155   echo -en "${bold}m\033[$colour"
156   [ "$__ps1_background" = 1 ] && echo -en "m\033[$bgcolour"
157   return $ret
158 }
159
160 function __ps1_colour_start() {
161   local colour=$(__ps1_colour_for $2)
162   echo -en '\033['
163   __ps1_colour_escape 0 "$colour" "$PROMPT_BACKGROUND_COLOUR"
164   echo -n m
165   return $1
166 }
167
168 function __ps1_colour_end() {
169   [ -n "$(__ps1_colour_for $2)" ] && echo -en '\033[0m'
170   return $1
171 }
172
173 function __ps1_background() {
174   local ret=$1; shift
175   echo -n "0"
176   [ "$__ps1_background" = 1 ] && echo -en "m\033[${PROMPT_BACKGROUND_COLOUR}"
177   return $ret
178 }
179
180 function __ps1_col() {
181   local ret=$1; shift
182   if [ $ret -gt 0 ]; then
183     __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
184   else
185     __ps1_colour_escape $ret "$PROMPT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
186   fi
187   return $ret
188 }
189
190 function __ps1_root() {
191   local ret=$1; shift
192   if [ $ret -gt 0 ]; then
193     __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
194   else
195     __ps1_colour_escape $ret "$ROOT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
196   fi
197   return $ret
198 }
199
200 function __ps1_ret() {
201   [ "$__ps1_user" = "1" ] || return $1
202   [ $1 -gt 0 ] && echo -n " ($1)"
203   return $1
204 }
205
206 function __ps1_user() {
207   local ret=$1; shift
208   [ "$__ps1_user" = "1" ] || return $ret
209   echo -n ${1+"$@"}
210   return $ret
211 }
212
213 function __ps1_bg() {
214   [ "$__ps1_bg" = "1" ] || return $1
215   local job
216   local running=0; for job in $(builtin jobs -pr); do running=$((running+1)); done
217   local total=0; for job in $(builtin jobs -p); do total=$((total+1)); done
218   [ $total = 0 ] && return $1
219   if [ -z "$2" ]; then
220     [ "$__ps1_user" = "1" ] && echo -n " "
221     echo -n "["
222     [ $running = $total ] || echo -n "$running/"
223     echo -n "$total]"
224   else
225     echo $2
226   fi
227   return $1
228 }
229
230 function __ps1_colon() {
231   local all="$__ps1_user$(eval echo $__ps1_all)$(__ps1_bg $1 1)"
232   [ "${all/1/}" = "$all" ] || echo -n ":"
233   return $1
234 }
235
236 function __ps1_short() {
237   local home=${HOME%%/}
238   local pwd=${PWD/#$home/\~}
239   local dirtrim=${PROMPT_DIRTRIM//[^0-9]/}
240
241   if [ "${dirtrim:0:1}" = "0" ]; then
242     echo "$pwd"
243     return $1
244   fi
245
246   dirtrim=${dirtrim##0}
247   if [ -z "$dirtrim" ]; then
248     local prompt="$USER$HOSTNAME$pwd"
249     local width=$(((COLUMNS*2)/3))
250     if [ ${#prompt} -le ${width:-53} ]; then
251       echo "$pwd"
252       return $1
253     else
254       dirtrim=1
255     fi
256   fi
257
258   local dirname=${pwd##*/}
259   local basename=${pwd%/$dirname}
260   local reversed=
261   local component
262   for component in ${basename//\// }; do
263     reversed="$component $reversed"
264   done
265   local n=1
266   local short=
267   for component in $reversed; do
268     [ $n = 1 -a "$PWD" = "$pwd" ] || short="/$short"
269     if [ $n -ge $dirtrim ]; then
270       short="${component:0:1}$short"
271     else
272       short="$component$short"
273     fi
274     n=$((n+1))
275   done
276
277   [ "${short:0:1}" = "~" ] || short="/$short"
278   echo "$short/$dirname"
279   return $1
280 }
281
282 function prompt() {
283   local blurb="Usage: prompt hide|show <what>"
284
285   if [ $# -lt 2 ]; then
286     echo >&2 "$blurb"
287     return 1
288   fi
289
290   action="$1"
291   if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
292     echo >&2 "$blurb"
293     return 1
294   fi
295   if [ "$action" = "hide" ]; then
296     action=0
297   else
298     action=1
299   fi
300
301   what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
302   eval __ps1_$what=$action
303 }
304
305 __ps1