Skip to content

Commit 3b73c7d

Browse files
committed
Merge branch 'so/prompt-command'
Finishing touches... * so/prompt-command: make __git_ps1 accept a third parameter in pcmode
2 parents 1b800f8 + 126b596 commit 3b73c7d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

contrib/completion/git-prompt.sh

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# will show username, at-sign, host, colon, cwd, then
2525
# various status string, followed by dollar and SP, as
2626
# your prompt.
27+
# Optionally, you can supply a third argument with a printf
28+
# format string to finetune the output of the branch status
2729
#
2830
# The argument to __git_ps1 will be displayed only if you are currently
2931
# in a git repository. The %s token will be the name of the current
@@ -222,10 +224,12 @@ __git_ps1_show_upstream ()
222224
# when called from PS1 using command substitution
223225
# in this mode it prints text to add to bash PS1 prompt (includes branch name)
224226
#
225-
# __git_ps1 requires 2 arguments when called from PROMPT_COMMAND (pc)
227+
# __git_ps1 requires 2 or 3 arguments when called from PROMPT_COMMAND (pc)
226228
# in that case it _sets_ PS1. The arguments are parts of a PS1 string.
227-
# when both arguments are given, the first is prepended and the second appended
229+
# when two arguments are given, the first is prepended and the second appended
228230
# to the state string when assigned to PS1.
231+
# The optional third parameter will be used as printf format string to further
232+
# customize the output of the git-status string.
229233
# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
230234
__git_ps1 ()
231235
{
@@ -236,9 +240,10 @@ __git_ps1 ()
236240
local printf_format=' (%s)'
237241

238242
case "$#" in
239-
2) pcmode=yes
243+
2|3) pcmode=yes
240244
ps1pc_start="$1"
241245
ps1pc_end="$2"
246+
printf_format="${3:-$printf_format}"
242247
;;
243248
0|1) printf_format="${1:-$printf_format}"
244249
;;
@@ -339,6 +344,7 @@ __git_ps1 ()
339344

340345
local f="$w$i$s$u"
341346
if [ $pcmode = yes ]; then
347+
local gitstring=
342348
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
343349
local c_red='\e[31m'
344350
local c_green='\e[32m'
@@ -356,29 +362,31 @@ __git_ps1 ()
356362
branch_color="$bad_color"
357363
fi
358364

359-
# Setting PS1 directly with \[ and \] around colors
365+
# Setting gitstring directly with \[ and \] around colors
360366
# is necessary to prevent wrapping issues!
361-
PS1="$ps1pc_start (\[$branch_color\]$branchstring\[$c_clear\]"
367+
gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
362368

363369
if [ -n "$w$i$s$u$r$p" ]; then
364-
PS1="$PS1 "
370+
gitstring="$gitstring "
365371
fi
366372
if [ "$w" = "*" ]; then
367-
PS1="$PS1\[$bad_color\]$w"
373+
gitstring="$gitstring\[$bad_color\]$w"
368374
fi
369375
if [ -n "$i" ]; then
370-
PS1="$PS1\[$ok_color\]$i"
376+
gitstring="$gitstring\[$ok_color\]$i"
371377
fi
372378
if [ -n "$s" ]; then
373-
PS1="$PS1\[$flags_color\]$s"
379+
gitstring="$gitstring\[$flags_color\]$s"
374380
fi
375381
if [ -n "$u" ]; then
376-
PS1="$PS1\[$bad_color\]$u"
382+
gitstring="$gitstring\[$bad_color\]$u"
377383
fi
378-
PS1="$PS1\[$c_clear\]$r$p)$ps1pc_end"
384+
gitstring="$gitstring\[$c_clear\]$r$p"
379385
else
380-
PS1="$ps1pc_start ($c${b##refs/heads/}${f:+ $f}$r$p)$ps1pc_end"
386+
gitstring="$c${b##refs/heads/}${f:+ $f}$r$p"
381387
fi
388+
gitstring=$(printf -- "$printf_format" "$gitstring")
389+
PS1="$ps1pc_start$gitstring$ps1pc_end"
382390
else
383391
# NO color option unless in PROMPT_COMMAND mode
384392
printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"

0 commit comments

Comments
 (0)