git-prompt.sh: refactor colored prompt code

__git_ps1_colorize_gitstring() sets color codes and builds the prompt gitstring. It has duplicated code to handle color codes for bash and zsh shells. __git_ps1() also has duplicated logic to build the prompt gitstring. Remove duplication of logic to build gitstring in __git_ps1_colorize_gitstring() and __git_ps1(). Leave in __git_ps1_colorize_gitstring() only logic to set color codes. Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eduardo R. D'Avila committed Jun 26, 2013 at 00:05 UTC 7fe9031920be5c98a7e58eea6be34442390392ec
1 file changed +26 -59
contrib/completion/git-prompt.sh
+26 -59
@@ -225,8 +225,8 @@ __git_ps1_show_upstream ()
225 }
226
227 # Helper function that is meant to be called from __git_ps1. It
228 -# builds up a gitstring injecting color codes into the appropriate
229 -# places.
228 +# injects color codes into the appropriate gitstring variables used
229 +# to build a gitstring.
230 __git_ps1_colorize_gitstring ()
231 {
232 if [[ -n ${ZSH_VERSION-} ]]; then
@@ -234,74 +234,40 @@ __git_ps1_colorize_gitstring ()
234 local c_green='%F{green}'
235 local c_lblue='%F{blue}'
236 local c_clear='%f'
237 - local bad_color=$c_red
238 - local ok_color=$c_green
239 - local branch_color="$c_clear"
240 - local flags_color="$c_lblue"
241 - local branchstring="$c${b##refs/heads/}"
242 -
243 - if [ $detached = no ]; then
244 - branch_color="$ok_color"
245 - else
246 - branch_color="$bad_color"
247 - fi
248 -
249 - gitstring="$branch_color$branchstring$c_clear"
250 -
251 - if [ -n "$w$i$s$u$r$p" ]; then
252 - gitstring="$gitstring$z"
253 - fi
254 - if [ "$w" = "*" ]; then
255 - gitstring="$gitstring$bad_color$w"
256 - fi
257 - if [ -n "$i" ]; then
258 - gitstring="$gitstring$ok_color$i"
259 - fi
260 - if [ -n "$s" ]; then
261 - gitstring="$gitstring$flags_color$s"
262 - fi
263 - if [ -n "$u" ]; then
264 - gitstring="$gitstring$bad_color$u"
265 - fi
266 - gitstring="$gitstring$c_clear$r$p"
267 - return
237 + else
238 + # Using \[ and \] around colors is necessary to prevent
239 + # issues with command line editing/browsing/completion!
240 + local c_red='\[\e[31m\]'
241 + local c_green='\[\e[32m\]'
242 + local c_lblue='\[\e[1;34m\]'
243 + local c_clear='\[\e[0m\]'
244 fi
269 - local c_red='\e[31m'
270 - local c_green='\e[32m'
271 - local c_lblue='\e[1;34m'
272 - local c_clear='\e[0m'
245 local bad_color=$c_red
246 local ok_color=$c_green
275 - local branch_color="$c_clear"
247 local flags_color="$c_lblue"
277 - local branchstring="$c${b##refs/heads/}"
248
249 + local branch_color=""
250 if [ $detached = no ]; then
251 branch_color="$ok_color"
252 else
253 branch_color="$bad_color"
254 fi
255 + c="$branch_color$c"
256 + b="$b$c_clear"
257
285 - # Setting gitstring directly with \[ and \] around colors
286 - # is necessary to prevent wrapping issues!
287 - gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
288 -
289 - if [ -n "$w$i$s$u$r$p" ]; then
290 - gitstring="$gitstring$z"
291 - fi
258 if [ "$w" = "*" ]; then
293 - gitstring="$gitstring\[$bad_color\]$w"
259 + w="$bad_color$w"
260 fi
261 if [ -n "$i" ]; then
296 - gitstring="$gitstring\[$ok_color\]$i"
262 + i="$ok_color$i"
263 fi
264 if [ -n "$s" ]; then
299 - gitstring="$gitstring\[$flags_color\]$s"
265 + s="$flags_color$s"
266 fi
267 if [ -n "$u" ]; then
302 - gitstring="$gitstring\[$bad_color\]$u"
268 + u="$bad_color$u"
269 fi
304 - gitstring="$gitstring\[$c_clear\]$r$p"
270 + r="$c_clear$r"
271 }
272
273 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
@@ -443,19 +409,20 @@ __git_ps1 ()
409 fi
410
411 local z="${GIT_PS1_STATESEPARATOR-" "}"
412 +
413 + # NO color option unless in PROMPT_COMMAND mode
414 + if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
415 + __git_ps1_colorize_gitstring
416 + fi
417 +
418 local f="$w$i$s$u"
419 + local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
420 +
421 if [ $pcmode = yes ]; then
448 - local gitstring=
449 - if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
450 - __git_ps1_colorize_gitstring
451 - else
452 - gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
453 - fi
422 gitstring=$(printf -- "$printf_format" "$gitstring")
423 PS1="$ps1pc_start$gitstring$ps1pc_end"
424 else
457 - # NO color option unless in PROMPT_COMMAND mode
458 - printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"
425 + printf -- "$printf_format" "$gitstring"
426 fi
427 fi
428 }