|
54 | 54 | # find one, or @{upstream} otherwise. Once you have set
|
55 | 55 | # GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
|
56 | 56 | # setting the bash.showUpstream config variable.
|
| 57 | +# |
| 58 | +# If you would like a colored hint about the current dirty state, set |
| 59 | +# GIT_PS1_SHOWCOLORHINTS to a nonempty value. When tracked files are |
| 60 | +# modified, the branch name turns red, when all modifications are staged |
| 61 | +# the branch name turns yellow and when all changes are checked in, the |
| 62 | +# color changes to green. The colors are currently hardcoded in the function. |
57 | 63 |
|
58 | 64 | # __gitdir accepts 0 or 1 arguments (i.e., location)
|
59 | 65 | # returns location of .git repo
|
@@ -207,6 +213,7 @@ __git_ps1_show_upstream ()
|
207 | 213 | # in that case it _sets_ PS1. The arguments are parts of a PS1 string.
|
208 | 214 | # when both arguments are given, the first is prepended and the second appended
|
209 | 215 | # to the state string when assigned to PS1.
|
| 216 | +# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true |
210 | 217 | __git_ps1 ()
|
211 | 218 | {
|
212 | 219 | local pcmode=no
|
@@ -320,9 +327,36 @@ __git_ps1 ()
|
320 | 327 | local f="$w$i$s$u"
|
321 | 328 | if [ $pcmode = yes ]; then
|
322 | 329 | PS1="$ps1pc_start("
|
323 |
| - PS1="$PS1$c${b##refs/heads/}${f:+ $f}$r$p" |
| 330 | + if [ -n "${GIT_PS1_SHOWCOLORHINT-}" ]; then |
| 331 | + local c_red='\e[31m' |
| 332 | + local c_green='\e[32m' |
| 333 | + local c_yellow='\e[33m' |
| 334 | + local c_lblue='\e[1;34m' |
| 335 | + local c_purple='\e[35m' |
| 336 | + local c_cyan='\e[36m' |
| 337 | + local c_clear='\e[0m' |
| 338 | + local branchstring="$c${b##refs/heads/}" |
| 339 | + local branch_color="$c_green" |
| 340 | + local flags_color="$c_cyan" |
| 341 | + |
| 342 | + if [ "$w" = "*" ]; then |
| 343 | + branch_color="$c_red" |
| 344 | + elif [ -n "$i" ]; then |
| 345 | + branch_color="$c_yellow" |
| 346 | + fi |
| 347 | + |
| 348 | + # Setting PS1 directly with \[ and \] around colors |
| 349 | + # is necessary to prevent wrapping issues! |
| 350 | + PS1="$PS1\[$branch_color\]$branchstring\[$c_clear\]" |
| 351 | + if [ -n "$f" ]; then |
| 352 | + PS1="$PS1 \[$flags_color\]$f\[$c_clear\]" |
| 353 | + fi |
| 354 | + else |
| 355 | + PS1="$PS1$c${b##refs/heads/}${f:+ $f}$r$p" |
| 356 | + fi |
324 | 357 | PS1="$PS1)$ps1pc_end"
|
325 | 358 | else
|
| 359 | + # NO color option unless in PROMPT_COMMAND mode |
326 | 360 | printf -- "$printf_format" "$c${b##refs/heads/}${f:+ $f}$r$p"
|
327 | 361 | fi
|
328 | 362 | fi
|
|
0 commit comments