@@ -225,83 +225,49 @@ __git_ps1_show_upstream ()
225
225
}
226
226
227
227
# 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 .
230
230
__git_ps1_colorize_gitstring ()
231
231
{
232
232
if [[ -n ${ZSH_VERSION-} ]]; then
233
233
local c_red=' %F{red}'
234
234
local c_green=' %F{green}'
235
235
local c_lblue=' %F{blue}'
236
236
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\]'
268
244
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'
273
245
local bad_color=$c_red
274
246
local ok_color=$c_green
275
- local branch_color=" $c_clear "
276
247
local flags_color=" $c_lblue "
277
- local branchstring=" $c ${b## refs/ heads/ } "
278
248
249
+ local branch_color=" "
279
250
if [ $detached = no ]; then
280
251
branch_color=" $ok_color "
281
252
else
282
253
branch_color=" $bad_color "
283
254
fi
255
+ c=" $branch_color$c "
256
+ b=" $b$c_clear "
284
257
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
292
258
if [ " $w " = " *" ]; then
293
- gitstring =" $gitstring \[ $ bad_color\] $w "
259
+ w =" $bad_color$w "
294
260
fi
295
261
if [ -n " $i " ]; then
296
- gitstring =" $gitstring \[ $ ok_color\] $i "
262
+ i =" $ok_color$i "
297
263
fi
298
264
if [ -n " $s " ]; then
299
- gitstring =" $gitstring \[ $ flags_color\] $s "
265
+ s =" $flags_color$s "
300
266
fi
301
267
if [ -n " $u " ]; then
302
- gitstring =" $gitstring \[ $ bad_color\] $u "
268
+ u =" $bad_color$u "
303
269
fi
304
- gitstring =" $gitstring \[ $ c_clear\] $r$p "
270
+ r =" $c_clear$r "
305
271
}
306
272
307
273
# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
@@ -443,19 +409,20 @@ __git_ps1 ()
443
409
fi
444
410
445
411
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
+
446
418
local f=" $w$i$s$u "
419
+ local gitstring=" $c ${b## refs/ heads/ }${f: +$z$f } $r$p "
420
+
447
421
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
454
422
gitstring=$( printf -- " $printf_format " " $gitstring " )
455
423
PS1=" $ps1pc_start$gitstring$ps1pc_end "
456
424
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 "
459
426
fi
460
427
fi
461
428
}
0 commit comments