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 # 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
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.
38 # Example plugin code:
40 # __ps1_example_colour='0;33'
42 # function __ps1_example() {
43 # if [ -n "$__ps1_example" ]; then
44 # echo -n "$(__ps1_prefix $1 __ps1_example)"
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
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.
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.
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.
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
76 # Pick a colour based on the terminal capabilities.
79 case $(tput colors) in
82 PROMPT_BACKGROUND_COLOUR="0;48;5;26"
83 PROMPT_OK_COLOUR="1;38;5;34"
84 PROMPT_FAILED_COLOUR="1;38;5;160"
86 ROOT_FAILED_COLOUR="0"
91 PROMPT_BACKGROUND_COLOUR="0;48;5;18"
92 PROMPT_OK_COLOUR="1;38;5;24"
93 PROMPT_FAILED_COLOUR="1;38;5;48"
95 ROOT_FAILED_COLOUR="0"
100 PROMPT_BACKGROUND_COLOUR="0;44"
101 PROMPT_OK_COLOUR="1;32"
102 PROMPT_FAILED_COLOUR="1;31"
104 ROOT_FAILED_COLOUR="0"
108 __ps1_all='$__ps1_user'
111 # Default __ps1_user to 1.
112 [ -z "$__ps1_user" ] && __ps1_user=1
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\] '
118 for snippet in ${PROFILE_HOME:-~}/.ps1.d/*.ps1; do
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"')\]'
125 PS1=$pre$snippets$post
130 function __ps1_colour_for() {
133 for colour in "${1}_colour${__ps1_colours}" "${1}_colour"; do
134 ret=$(eval echo -n "\$$colour")
135 [ -n "$ret" ] && break
140 function __ps1_prefix() {
142 local prefix=${__ps1_all%$var*}
143 local all="$(eval echo $prefix)"
144 [ "${all/1/}" = "$all" ] || echo -n " "
148 # iTerm doesn't like it if you set bold and colour at the same time.
149 function __ps1_colour_escape() {
151 local bold="${1%%;*}"
152 local colour="${1#*;}"
153 local bgcolour="${2#*;}"
155 echo -en "${bold}m\033[$colour"
156 [ "$__ps1_background" = 1 ] && echo -en "m\033[$bgcolour"
160 function __ps1_colour_start() {
161 local colour=$(__ps1_colour_for $2)
163 __ps1_colour_escape 0 "$colour" "$PROMPT_BACKGROUND_COLOUR"
168 function __ps1_colour_end() {
169 [ -n "$(__ps1_colour_for $2)" ] && echo -en '\033[0m'
173 function __ps1_background() {
176 [ "$__ps1_background" = 1 ] && echo -en "m\033[${PROMPT_BACKGROUND_COLOUR}"
180 function __ps1_col() {
182 if [ $ret -gt 0 ]; then
183 __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
185 __ps1_colour_escape $ret "$PROMPT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
190 function __ps1_root() {
192 if [ $ret -gt 0 ]; then
193 __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
195 __ps1_colour_escape $ret "$ROOT_OK_COLOUR" "$PROMPT_BACKGROUND_COLOUR"
200 function __ps1_ret() {
201 [ "$__ps1_user" = "1" ] || return $1
202 [ $1 -gt 0 ] && echo -n " ($1)"
206 function __ps1_user() {
208 [ "$__ps1_user" = "1" ] || return $ret
213 function __ps1_bg() {
214 [ "$__ps1_bg" = "1" ] || return $1
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
220 [ "$__ps1_user" = "1" ] && echo -n " "
222 [ $running = $total ] || echo -n "$running/"
230 function __ps1_colon() {
231 local all="$__ps1_user$(eval echo $__ps1_all)$(__ps1_bg $1 1)"
232 [ "${all/1/}" = "$all" ] || echo -n ":"
236 function __ps1_short() {
237 local home=${HOME%%/}
238 local pwd=${PWD/#$home/\~}
239 local dirtrim=${PROMPT_DIRTRIM//[^0-9]/}
241 if [ "${dirtrim:0:1}" = "0" ]; then
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
258 local dirname=${pwd##*/}
259 local basename=${pwd%/$dirname}
262 for component in ${basename//\// }; do
263 reversed="$component $reversed"
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"
272 short="$component$short"
277 [ "${short:0:1}" = "~" ] || short="/$short"
278 echo "$short/$dirname"
283 local blurb="Usage: prompt hide|show <what>"
285 if [ $# -lt 2 ]; then
291 if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
295 if [ "$action" = "hide" ]; then
301 what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
302 eval __ps1_$what=$action