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
@@ -213,10 +215,12 @@ __git_ps1_show_upstream ()
213
215
# when called from PS1 using command substitution
214
216
# in this mode it prints text to add to bash PS1 prompt (includes branch name)
215
217
#
216
- # __git_ps1 requires 2 arguments when called from PROMPT_COMMAND (pc)
218
+ # __git_ps1 requires 2 or 3 arguments when called from PROMPT_COMMAND (pc)
217
219
# in that case it _sets_ PS1. The arguments are parts of a PS1 string.
218
- # when both arguments are given, the first is prepended and the second appended
220
+ # when two arguments are given, the first is prepended and the second appended
219
221
# to the state string when assigned to PS1.
222
+ # The optional third parameter will be used as printf format string to further
223
+ # customize the output of the git-status string.
220
224
# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
221
225
__git_ps1 ()
222
226
{
@@ -227,9 +231,10 @@ __git_ps1 ()
227
231
local printf_format=' (%s)'
228
232
229
233
case " $# " in
230
- 2) pcmode=yes
234
+ 2|3 ) pcmode=yes
231
235
ps1pc_start=" $1 "
232
236
ps1pc_end=" $2 "
237
+ printf_format=" ${3:- $printf_format } "
233
238
;;
234
239
0|1) printf_format=" ${1:- $printf_format } "
235
240
;;
@@ -330,6 +335,7 @@ __git_ps1 ()
330
335
331
336
local f=" $w$i$s$u "
332
337
if [ $pcmode = yes ]; then
338
+ local gitstring=
333
339
if [ -n " ${GIT_PS1_SHOWCOLORHINTS-} " ]; then
334
340
local c_red=' \e[31m'
335
341
local c_green=' \e[32m'
@@ -347,29 +353,31 @@ __git_ps1 ()
347
353
branch_color=" $bad_color "
348
354
fi
349
355
350
- # Setting PS1 directly with \[ and \] around colors
356
+ # Setting gitstring directly with \[ and \] around colors
351
357
# is necessary to prevent wrapping issues!
352
- PS1= " $ps1pc_start ( \[$branch_color \]$branchstring \[$c_clear \]"
358
+ gitstring= " \[$branch_color \]$branchstring \[$c_clear \]"
353
359
354
360
if [ -n " $w$i$s$u$r$p " ]; then
355
- PS1 =" $PS1 "
361
+ gitstring =" $gitstring "
356
362
fi
357
363
if [ " $w " = " *" ]; then
358
- PS1 =" $PS1 \[$bad_color \]$w "
364
+ gitstring =" $gitstring \[$bad_color \]$w "
359
365
fi
360
366
if [ -n " $i " ]; then
361
- PS1 =" $PS1 \[$ok_color \]$i "
367
+ gitstring =" $gitstring \[$ok_color \]$i "
362
368
fi
363
369
if [ -n " $s " ]; then
364
- PS1 =" $PS1 \[$flags_color \]$s "
370
+ gitstring =" $gitstring \[$flags_color \]$s "
365
371
fi
366
372
if [ -n " $u " ]; then
367
- PS1 =" $PS1 \[$bad_color \]$u "
373
+ gitstring =" $gitstring \[$bad_color \]$u "
368
374
fi
369
- PS1 =" $PS1 \[$c_clear \]$r$p ) $ps1pc_end "
375
+ gitstring =" $gitstring \[$c_clear \]$r$p "
370
376
else
371
- PS1 =" $ps1pc_start ( $ c${b## refs/ heads/ }${f: + $f } $r$p ) $ps1pc_end "
377
+ gitstring =" $c ${b## refs/ heads/ }${f: + $f } $r$p "
372
378
fi
379
+ gitstring=$( printf -- " $printf_format " " $gitstring " )
380
+ PS1=" $ps1pc_start$gitstring$ps1pc_end "
373
381
else
374
382
# NO color option unless in PROMPT_COMMAND mode
375
383
printf -- " $printf_format " " $c ${b## refs/ heads/ }${f: + $f } $r$p "
0 commit comments