prompt: colorize ZSH prompt

Add colors suitable for use in the ZSH prompt. Having learnt that the ZSH equivalent of PROMPT_COMMAND is precmd (), you can now use GIT_PS1_SHOWCOLORHINTS with ZSH. Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed May 17, 2013 at 14:25 UTC 9678696c4ad0d269f933de001daa78c54a2b8868
1 file changed +39 -1
contrib/completion/git-prompt.sh
+39 -1
@@ -20,7 +20,8 @@
20 # <post>, which are strings you would put in $PS1 before
21 # and after the status string generated by the git-prompt
22 # machinery. e.g.
23 -# PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
23 +# Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
24 +# ZSH: precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
25 # will show username, at-sign, host, colon, cwd, then
26 # various status string, followed by dollar and SP, as
27 # your prompt.
@@ -227,6 +228,43 @@ __git_ps1_show_upstream ()
228 # places.
229 __git_ps1_colorize_gitstring ()
230 {
231 + if [[ -n ${ZSH_VERSION-} ]]; then
232 + local c_red='%F{red}'
233 + local c_green='%F{green}'
234 + local c_lblue='%F{blue}'
235 + local c_clear='%f'
236 + local bad_color=$c_red
237 + local ok_color=$c_green
238 + local branch_color="$c_clear"
239 + local flags_color="$c_lblue"
240 + local branchstring="$c${b##refs/heads/}"
241 +
242 + if [ $detached = no ]; then
243 + branch_color="$ok_color"
244 + else
245 + branch_color="$bad_color"
246 + fi
247 +
248 + gitstring="$branch_color$branchstring$c_clear"
249 +
250 + if [ -n "$w$i$s$u$r$p" ]; then
251 + gitstring="$gitstring$z"
252 + fi
253 + if [ "$w" = "*" ]; then
254 + gitstring="$gitstring$bad_color$w"
255 + fi
256 + if [ -n "$i" ]; then
257 + gitstring="$gitstring$ok_color$i"
258 + fi
259 + if [ -n "$s" ]; then
260 + gitstring="$gitstring$flags_color$s"
261 + fi
262 + if [ -n "$u" ]; then
263 + gitstring="$gitstring$bad_color$u"
264 + fi
265 + gitstring="$gitstring$c_clear$r$p"
266 + return
267 + fi
268 local c_red='\e[31m'
269 local c_green='\e[32m'
270 local c_lblue='\e[1;34m'