Prompt tweaks.
[profile.git] / .profile.d / ps1.bashrc
1 #!bash Coloured prompts.
2 #
3 # The prompt comprises multiple parts, some of which may be hidden by unsetting 
4 # shell variables or using the ``prompt'' function.
5 #
6 # The first part of the prompt is user@host where host is highlighted in 
7 # green if the last command exited 0 or in red otherwise.
8 # This part will be shown only if __ps1_user is 1.  By default it is 1.
9 # The success and failure colours can be changed by modifying 
10 # $PROMPT_OK_COLOUR and $PROMPT_FAILED_COLOUR respectively.
11 #
12 # The second part of the prompt is taken from git-completion.bashrc.
13 # It is shown only if __ps1_git is 1.  By default it is 0.
14 #
15 # The third part of the prompt is taken from p4-completion.bashrc.
16 # It is shown only if __ps1_p4 is 1.  By default it is 0.
17 #
18 # The fourth part of the prompt is taken from svn-completion.bashrc.
19 # It is shown only if __ps1_svn is 1.  By default it is 0.
20 #
21 # The fifth part of the prompt is the exit status of the last command.
22 # This part will be shown only if __ps1_user is set and the exit status is 
23 # non-zero.
24
25 # The final part of the prompt is the (full) working directory and $ string.
26 # If the shell is running as root the # string's colour can be changed by
27 # modifying $ROOT_OK_COLOUR and $ROOT_FAILED_COLOUR.
28 #
29 # Colouring is performed by the __ps1_col() and __ps1_ret() functions.
30 # We redirect stderr to /dev/null when calling these functions to prevent 
31 # bash complaining about not knowing them when you su to another user, 
32 # retaining PS1 but not the function definitions.
33 #
34 # Note that $? is passed as an argument to - and is returned from - all 
35 # functions.  As $? is set following any shell activity it is only guaranteed 
36 # to represent the return code of the last command at the beginning of __ps1().
37 # By passing between subsequent functions we ensure that it is available for 
38 # __ps1_ret().
39 #
40
41 # Pick a colour based on the terminal capabilities.
42 # OK: dark green.
43 # Failed: dark red.
44 # Git: royal blue.
45 # P4: yellow.
46 # SVN: magenta.
47 case $(tput colors) in
48   256)
49     PROMPT_OK_COLOUR="1;38;5;34"
50     PROMPT_FAILED_COLOUR="1;38;5;160"
51     ROOT_OK_COLOUR="0"
52     ROOT_FAILED_COLOUR="0"
53     GIT_COLOUR="0;38;5;33"
54     SVN_COLOUR="0;38;5;127"
55     P4_COLOUR="0;38;5;142"
56   ;;
57
58   88)
59     PROMPT_OK_COLOUR="1;38;5;24"
60     PROMPT_FAILED_COLOUR="1;38;5;48"
61     ROOT_OK_COLOUR="0"
62     ROOT_FAILED_COLOUR="0"
63     GIT_COLOUR="0;38;5;23"
64     SVN_COLOUR="0;38;5;49"
65     P4_COLOUR="0;38;5;56"
66   ;;
67
68   *)
69     PROMPT_OK_COLOUR="1;32"
70     PROMPT_FAILED_COLOUR="1;31"
71     ROOT_OK_COLOUR="0"
72     ROOT_FAILED_COLOUR="0"
73     GIT_COLOUR="0;36"
74     SVN_COLOUR="0;35"
75     P4_COLOUR="0;33"
76   ;;
77 esac
78
79 function __ps1() {
80   # Default __ps1_user to 1.
81   [ -z "$__ps1_user" ] && __ps1_user=1
82
83   PS1='$(__ps1_user $? \u@)\[\033[$(__ps1_col $? 2>/dev/null)m\]$(__ps1_user $? \h)\[\033[$(__ps1_colour_escape $? $GIT_COLOUR)m\]$(__ps1_git $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $P4_COLOUR)m\]$(__ps1_p4 $? 2>/dev/null)\[\033[0m\]\[\033[$(__ps1_colour_escape $? $SVN_COLOUR)m\]$(__ps1_svn $? 2>/dev/null)\[\033[0m\]$(__ps1_ret $? 2>/dev/null)$(__ps1_colon $?)\w\[\033[$(__ps1_root $? 2>/dev/null)m\]\$\[\033[0m\] '
84   return 0
85 }
86
87 # iTerm doesn't like it if you set bold and colour at the same time.
88 function __ps1_colour_escape() {
89   local ret=$1; shift
90   local bold="${1%%;*}"
91   local colour="${1#*;}"
92
93   echo -en "${bold}m\033[$colour"
94   return $ret
95 }
96
97 function __ps1_col() {
98   local ret=$1; shift
99   if [ $ret -gt 0 ]; then
100     __ps1_colour_escape $ret "$PROMPT_FAILED_COLOUR"
101   else
102     __ps1_colour_escape $ret "$PROMPT_OK_COLOUR"
103   fi
104   return $ret
105 }
106
107 function __ps1_root() {
108   local ret=$1; shift
109   if [ $ret -gt 0 ]; then
110     __ps1_colour_escape $ret "$ROOT_FAILED_COLOUR"
111   else
112     __ps1_colour_escape $ret "$ROOT_OK_COLOUR"
113   fi
114   return $ret
115 }
116
117 function __ps1_ret() {
118   [ "$__ps1_user" = "1" ] || return $1
119   [ $1 -gt 0 ] && echo -n " ($1)"
120   return $1
121 }
122
123 function __ps1_user() {
124   local ret=$1; shift
125   [ "$__ps1_user" = "1" ] || return $ret
126   echo -n ${1+"$@"}
127   return $ret
128 }
129
130 function __ps1_git() {
131   [ "$__ps1_git" = "1" ] || return $1
132   if [ "$__ps1_user" = "1" ]; then
133     __git_ps1 ' %s'
134   else
135     __git_ps1 '%s'
136   fi
137   return $1
138 }
139
140 function __ps1_p4() {
141   [ "$__ps1_p4" = "1" ] || return $1
142   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" ]; then
143     __p4_ps1 ' %s'
144   else
145     __p4_ps1 '%s'
146   fi
147   return $1
148 }
149
150 function __ps1_svn() {
151   [ "$__ps1_svn" = "1" ] || return $1
152   if [ "$__ps1_user" = "1" -o "$__ps1_git" = "1" -o "$__ps1_p4" = "1" ]; then
153     __svn_ps1 ' %s'
154   else
155     __svn_ps1 '%s'
156   fi
157   return $1
158 }
159
160 function __ps1_colon() {
161   local all="$__ps1_user$__ps1_git$__ps1_p4$__ps1_svn"
162   [ "${all/1/}" = "$all" ] || echo -n ":"
163   return $1
164 }
165
166 function prompt() {
167   local blurb="Usage: prompt hide|show <what>"
168
169   if [ $# -lt 2 ]; then
170     echo >&2 "$blurb"
171     return 1
172   fi
173
174   action="$1"
175   if [ ! "$action" = "hide" -a ! "$action" = "show" ]; then
176     echo >&2 "$blurb"
177     return 1
178   fi
179   if [ "$action" = "hide" ]; then
180     action=0
181   else
182     action=1
183   fi
184
185   what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
186   eval __ps1_$what=$action
187 }
188
189 __ps1