make __git_ps1 accept a third parameter in pcmode
The optional third parameter when __git_ps1 is used in PROMPT_COMMAND mode as format string for printf to further customize the way the git status string is embedded in the user's PS1 prompt. Signed-off-by: Simon Oosthoek <s.oosthoek@xs4all.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Simon Oosthoek committed
Dec 26, 2012 at 20:15 UTC
126b59692bb4d1e2283e8528fe0a8103995c23c4
1 file changed
+20
-12
contrib/completion/git-prompt.sh
+20
-12
@@ -24,6 +24,8 @@
24
# will show username, at-sign, host, colon, cwd, then
25
# various status string, followed by dollar and SP, as
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
29
#
30
# The argument to __git_ps1 will be displayed only if you are currently
31
# in a git repository. The %s token will be the name of the current
@@ -213,10 +215,12 @@ __git_ps1_show_upstream ()
215
# when called from PS1 using command substitution
216
# in this mode it prints text to add to bash PS1 prompt (includes branch name)
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)
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
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.
224
# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
225
__git_ps1 ()
226
{
@@ -227,9 +231,10 @@ __git_ps1 ()
231
local printf_format=' (%s)'
232
233
case "$#" in
230
- 2) pcmode=yes
234
+ 2|3) pcmode=yes
235
ps1pc_start="$1"
236
ps1pc_end="$2"
237
+ printf_format="${3:-$printf_format}"
238
;;
239
0|1) printf_format="${1:-$printf_format}"
240
;;
@@ -330,6 +335,7 @@ __git_ps1 ()
335
336
local f="$w$i$s$u"
337
if [ $pcmode = yes ]; then
338
+ local gitstring=
339
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
340
local c_red='\e[31m'
341
local c_green='\e[32m'
@@ -347,29 +353,31 @@ __git_ps1 ()
353
branch_color="$bad_color"
354
fi
355
350
- # Setting PS1 directly with \[ and \] around colors
356
+ # Setting gitstring directly with \[ and \] around colors
357
# is necessary to prevent wrapping issues!
352
- PS1="$ps1pc_start (\[$branch_color\]$branchstring\[$c_clear\]"
358
+ gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
359
360
if [ -n "$w$i$s$u$r$p" ]; then
355
- PS1="$PS1 "
361
+ gitstring="$gitstring "
362
fi
363
if [ "$w" = "*" ]; then
358
- PS1="$PS1\[$bad_color\]$w"
364
+ gitstring="$gitstring\[$bad_color\]$w"
365
fi
366
if [ -n "$i" ]; then
361
- PS1="$PS1\[$ok_color\]$i"
367
+ gitstring="$gitstring\[$ok_color\]$i"
368
fi
369
if [ -n "$s" ]; then
364
- PS1="$PS1\[$flags_color\]$s"
370
+ gitstring="$gitstring\[$flags_color\]$s"
371
fi
372
if [ -n "$u" ]; then
367
- PS1="$PS1\[$bad_color\]$u"
373
+ gitstring="$gitstring\[$bad_color\]$u"
374
fi
369
- PS1="$PS1\[$c_clear\]$r$p)$ps1pc_end"
375
+ gitstring="$gitstring\[$c_clear\]$r$p"
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"
378
fi
379
+ gitstring=$(printf -- "$printf_format" "$gitstring")
380
+ PS1="$ps1pc_start$gitstring$ps1pc_end"
381
else
382
# NO color option unless in PROMPT_COMMAND mode
383
printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"