prompt: factor out gitstring coloring logic

So that we can extend it with ZSH-colors in a later patch. 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 18562ad1a007234aded1e78f73d4b2f53a02fd0b
1 file changed +43 -36
contrib/completion/git-prompt.sh
+43 -36
@@ -222,6 +222,48 @@ __git_ps1_show_upstream ()
222
223 }
224
225 +# Helper function that is meant to be called from __git_ps1. It
226 +# builds up a gitstring injecting color codes into the appropriate
227 +# places.
228 +__git_ps1_colorize_gitstring ()
229 +{
230 + local c_red='\e[31m'
231 + local c_green='\e[32m'
232 + local c_lblue='\e[1;34m'
233 + local c_clear='\e[0m'
234 + local bad_color=$c_red
235 + local ok_color=$c_green
236 + local branch_color="$c_clear"
237 + local flags_color="$c_lblue"
238 + local branchstring="$c${b##refs/heads/}"
239 +
240 + if [ $detached = no ]; then
241 + branch_color="$ok_color"
242 + else
243 + branch_color="$bad_color"
244 + fi
245 +
246 + # Setting gitstring directly with \[ and \] around colors
247 + # is necessary to prevent wrapping issues!
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 +}
267
268 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
269 # when called from PS1 using command substitution
@@ -364,42 +406,7 @@ __git_ps1 ()
406 if [ $pcmode = yes ]; then
407 local gitstring=
408 if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
367 - local c_red='\e[31m'
368 - local c_green='\e[32m'
369 - local c_lblue='\e[1;34m'
370 - local c_clear='\e[0m'
371 - local bad_color=$c_red
372 - local ok_color=$c_green
373 - local branch_color="$c_clear"
374 - local flags_color="$c_lblue"
375 - local branchstring="$c${b##refs/heads/}"
376 -
377 - if [ $detached = no ]; then
378 - branch_color="$ok_color"
379 - else
380 - branch_color="$bad_color"
381 - fi
382 -
383 - # Setting gitstring directly with \[ and \] around colors
384 - # is necessary to prevent wrapping issues!
385 - gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
386 -
387 - if [ -n "$w$i$s$u$r$p" ]; then
388 - gitstring="$gitstring$z"
389 - fi
390 - if [ "$w" = "*" ]; then
391 - gitstring="$gitstring\[$bad_color\]$w"
392 - fi
393 - if [ -n "$i" ]; then
394 - gitstring="$gitstring\[$ok_color\]$i"
395 - fi
396 - if [ -n "$s" ]; then
397 - gitstring="$gitstring\[$flags_color\]$s"
398 - fi
399 - if [ -n "$u" ]; then
400 - gitstring="$gitstring\[$bad_color\]$u"
401 - fi
402 - gitstring="$gitstring\[$c_clear\]$r$p"
409 + __git_ps1_colorize_gitstring
410 else
411 gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
412 fi