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