24
24
# will show username, at-sign, host, colon, cwd, then
25
25
# various status string, followed by dollar and SP, as
26
26
# your prompt.
27
+ # Optionally, you can supply a third argument with a printf
28
+ # format string to finetune the output of the branch status
27
29
#
28
30
# The argument to __git_ps1 will be displayed only if you are currently
29
31
# in a git repository. The %s token will be the name of the current
@@ -222,10 +224,12 @@ __git_ps1_show_upstream ()
222
224
# when called from PS1 using command substitution
223
225
# in this mode it prints text to add to bash PS1 prompt (includes branch name)
224
226
#
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)
226
228
# 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
228
230
# 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.
229
233
# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
230
234
__git_ps1 ()
231
235
{
@@ -236,9 +240,10 @@ __git_ps1 ()
236
240
local printf_format=' (%s)'
237
241
238
242
case " $# " in
239
- 2) pcmode=yes
243
+ 2|3 ) pcmode=yes
240
244
ps1pc_start=" $1 "
241
245
ps1pc_end=" $2 "
246
+ printf_format=" ${3:- $printf_format } "
242
247
;;
243
248
0|1) printf_format=" ${1:- $printf_format } "
244
249
;;
@@ -339,6 +344,7 @@ __git_ps1 ()
339
344
340
345
local f=" $w$i$s$u "
341
346
if [ $pcmode = yes ]; then
347
+ local gitstring=
342
348
if [ -n " ${GIT_PS1_SHOWCOLORHINTS-} " ]; then
343
349
local c_red=' \e[31m'
344
350
local c_green=' \e[32m'
@@ -356,29 +362,31 @@ __git_ps1 ()
356
362
branch_color=" $bad_color "
357
363
fi
358
364
359
- # Setting PS1 directly with \[ and \] around colors
365
+ # Setting gitstring directly with \[ and \] around colors
360
366
# is necessary to prevent wrapping issues!
361
- PS1= " $ps1pc_start ( \[$branch_color \]$branchstring \[$c_clear \]"
367
+ gitstring= " \[$branch_color \]$branchstring \[$c_clear \]"
362
368
363
369
if [ -n " $w$i$s$u$r$p " ]; then
364
- PS1 =" $PS1 "
370
+ gitstring =" $gitstring "
365
371
fi
366
372
if [ " $w " = " *" ]; then
367
- PS1 =" $PS1 \[$bad_color \]$w "
373
+ gitstring =" $gitstring \[$bad_color \]$w "
368
374
fi
369
375
if [ -n " $i " ]; then
370
- PS1 =" $PS1 \[$ok_color \]$i "
376
+ gitstring =" $gitstring \[$ok_color \]$i "
371
377
fi
372
378
if [ -n " $s " ]; then
373
- PS1 =" $PS1 \[$flags_color \]$s "
379
+ gitstring =" $gitstring \[$flags_color \]$s "
374
380
fi
375
381
if [ -n " $u " ]; then
376
- PS1 =" $PS1 \[$bad_color \]$u "
382
+ gitstring =" $gitstring \[$bad_color \]$u "
377
383
fi
378
- PS1 =" $PS1 \[$c_clear \]$r$p ) $ps1pc_end "
384
+ gitstring =" $gitstring \[$c_clear \]$r$p "
379
385
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 "
381
387
fi
388
+ gitstring=$( printf -- " $printf_format " " $gitstring " )
389
+ PS1=" $ps1pc_start$gitstring$ps1pc_end "
382
390
else
383
391
# NO color option unless in PROMPT_COMMAND mode
384
392
printf -- " $printf_format " " $c ${b## refs/ heads/ }${f: + $f } $r$p "
0 commit comments