Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 7fe9031

Browse files
erdavilagitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1572e18 commit 7fe9031

File tree

1 file changed

+26
-59
lines changed

1 file changed

+26
-59
lines changed

contrib/completion/git-prompt.sh

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -225,83 +225,49 @@ __git_ps1_show_upstream ()
225225
}
226226

227227
# 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.
230230
__git_ps1_colorize_gitstring ()
231231
{
232232
if [[ -n ${ZSH_VERSION-} ]]; then
233233
local c_red='%F{red}'
234234
local c_green='%F{green}'
235235
local c_lblue='%F{blue}'
236236
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\]'
268244
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'
273245
local bad_color=$c_red
274246
local ok_color=$c_green
275-
local branch_color="$c_clear"
276247
local flags_color="$c_lblue"
277-
local branchstring="$c${b##refs/heads/}"
278248

249+
local branch_color=""
279250
if [ $detached = no ]; then
280251
branch_color="$ok_color"
281252
else
282253
branch_color="$bad_color"
283254
fi
255+
c="$branch_color$c"
256+
b="$b$c_clear"
284257

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
292258
if [ "$w" = "*" ]; then
293-
gitstring="$gitstring\[$bad_color\]$w"
259+
w="$bad_color$w"
294260
fi
295261
if [ -n "$i" ]; then
296-
gitstring="$gitstring\[$ok_color\]$i"
262+
i="$ok_color$i"
297263
fi
298264
if [ -n "$s" ]; then
299-
gitstring="$gitstring\[$flags_color\]$s"
265+
s="$flags_color$s"
300266
fi
301267
if [ -n "$u" ]; then
302-
gitstring="$gitstring\[$bad_color\]$u"
268+
u="$bad_color$u"
303269
fi
304-
gitstring="$gitstring\[$c_clear\]$r$p"
270+
r="$c_clear$r"
305271
}
306272

307273
# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
@@ -443,19 +409,20 @@ __git_ps1 ()
443409
fi
444410

445411
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+
446418
local f="$w$i$s$u"
419+
local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
420+
447421
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
454422
gitstring=$(printf -- "$printf_format" "$gitstring")
455423
PS1="$ps1pc_start$gitstring$ps1pc_end"
456424
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"
459426
fi
460427
fi
461428
}

0 commit comments

Comments
 (0)