|
3 | 3 | # Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]> |
4 | 4 | # Distributed under the GNU General Public License, version 2.0. |
5 | 5 | # |
6 | | -# This script allows you to see the current branch in your prompt. |
| 6 | +# This script allows you to see repository status in your prompt. |
7 | 7 | # |
8 | 8 | # To enable: |
9 | 9 | # |
|
13 | 13 | # 3a) Change your PS1 to call __git_ps1 as |
14 | 14 | # command-substitution: |
15 | 15 | # Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' |
16 | | -# ZSH: PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ ' |
| 16 | +# ZSH: setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ ' |
17 | 17 | # the optional argument will be used as format string. |
18 | | -# 3b) Alternatively, for a slighly faster prompt, if you are |
19 | | -# using bash, __git_ps1 can be used for PROMPT_COMMAND |
| 18 | +# 3b) Alternatively, for a slightly faster prompt, __git_ps1 can |
| 19 | +# be used for PROMPT_COMMAND in Bash or for precmd() in Zsh |
20 | 20 | # with two parameters, <pre> and <post>, which are strings |
21 | 21 | # you would put in $PS1 before and after the status string |
22 | 22 | # generated by the git-prompt machinery. e.g. |
23 | 23 | # Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "' |
| 24 | +# will show username, at-sign, host, colon, cwd, then |
| 25 | +# various status string, followed by dollar and SP, as |
| 26 | +# your prompt. |
24 | 27 | # ZSH: precmd () { __git_ps1 "%n" ":%~$ " "|%s" } |
25 | | -# will show username, at-sign, host, colon, cwd, then |
26 | | -# various status string, followed by dollar and SP, as |
27 | | -# your prompt. |
| 28 | +# will show username, pipe, then various status string, |
| 29 | +# followed by colon, cwd, dollar and SP, as your prompt. |
28 | 30 | # Optionally, you can supply a third argument with a printf |
29 | 31 | # format string to finetune the output of the branch status |
30 | 32 | # |
31 | | -# The argument to __git_ps1 will be displayed only if you are currently |
32 | | -# in a git repository. The %s token will be the name of the current |
33 | | -# branch. |
| 33 | +# The repository status will be displayed only if you are currently in a |
| 34 | +# git repository. The %s token is the placeholder for the shown status. |
| 35 | +# |
| 36 | +# The prompt status always includes the current branch name. |
34 | 37 | # |
35 | 38 | # In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty value, |
36 | 39 | # unstaged (*) and staged (+) changes will be shown next to the branch |
|
78 | 81 | # |
79 | 82 | # If you would like a colored hint about the current dirty state, set |
80 | 83 | # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on |
81 | | -# the colored output of "git status -sb". |
| 84 | +# the colored output of "git status -sb" and are available only when |
| 85 | +# using __git_ps1 for PROMPT_COMMAND or precmd. |
82 | 86 |
|
83 | 87 | # stores the divergence from upstream in $p |
84 | 88 | # used by GIT_PS1_SHOWUPSTREAM |
@@ -201,83 +205,49 @@ __git_ps1_show_upstream () |
201 | 205 | } |
202 | 206 |
|
203 | 207 | # Helper function that is meant to be called from __git_ps1. It |
204 | | -# builds up a gitstring injecting color codes into the appropriate |
205 | | -# places. |
| 208 | +# injects color codes into the appropriate gitstring variables used |
| 209 | +# to build a gitstring. |
206 | 210 | __git_ps1_colorize_gitstring () |
207 | 211 | { |
208 | 212 | if [[ -n ${ZSH_VERSION-} ]]; then |
209 | 213 | local c_red='%F{red}' |
210 | 214 | local c_green='%F{green}' |
211 | 215 | local c_lblue='%F{blue}' |
212 | 216 | local c_clear='%f' |
213 | | - local bad_color=$c_red |
214 | | - local ok_color=$c_green |
215 | | - local branch_color="$c_clear" |
216 | | - local flags_color="$c_lblue" |
217 | | - local branchstring="$c${b##refs/heads/}" |
218 | | - |
219 | | - if [ $detached = no ]; then |
220 | | - branch_color="$ok_color" |
221 | | - else |
222 | | - branch_color="$bad_color" |
223 | | - fi |
224 | | - |
225 | | - gitstring="$branch_color$branchstring$c_clear" |
226 | | - |
227 | | - if [ -n "$w$i$s$u$r$p" ]; then |
228 | | - gitstring="$gitstring$z" |
229 | | - fi |
230 | | - if [ "$w" = "*" ]; then |
231 | | - gitstring="$gitstring$bad_color$w" |
232 | | - fi |
233 | | - if [ -n "$i" ]; then |
234 | | - gitstring="$gitstring$ok_color$i" |
235 | | - fi |
236 | | - if [ -n "$s" ]; then |
237 | | - gitstring="$gitstring$flags_color$s" |
238 | | - fi |
239 | | - if [ -n "$u" ]; then |
240 | | - gitstring="$gitstring$bad_color$u" |
241 | | - fi |
242 | | - gitstring="$gitstring$c_clear$r$p" |
243 | | - return |
| 217 | + else |
| 218 | + # Using \[ and \] around colors is necessary to prevent |
| 219 | + # issues with command line editing/browsing/completion! |
| 220 | + local c_red='\[\e[31m\]' |
| 221 | + local c_green='\[\e[32m\]' |
| 222 | + local c_lblue='\[\e[1;34m\]' |
| 223 | + local c_clear='\[\e[0m\]' |
244 | 224 | fi |
245 | | - local c_red='\e[31m' |
246 | | - local c_green='\e[32m' |
247 | | - local c_lblue='\e[1;34m' |
248 | | - local c_clear='\e[0m' |
249 | 225 | local bad_color=$c_red |
250 | 226 | local ok_color=$c_green |
251 | | - local branch_color="$c_clear" |
252 | 227 | local flags_color="$c_lblue" |
253 | | - local branchstring="$c${b##refs/heads/}" |
254 | 228 |
|
| 229 | + local branch_color="" |
255 | 230 | if [ $detached = no ]; then |
256 | 231 | branch_color="$ok_color" |
257 | 232 | else |
258 | 233 | branch_color="$bad_color" |
259 | 234 | fi |
| 235 | + c="$branch_color$c" |
260 | 236 |
|
261 | | - # Setting gitstring directly with \[ and \] around colors |
262 | | - # is necessary to prevent wrapping issues! |
263 | | - gitstring="\[$branch_color\]$branchstring\[$c_clear\]" |
264 | | - |
265 | | - if [ -n "$w$i$s$u$r$p" ]; then |
266 | | - gitstring="$gitstring$z" |
267 | | - fi |
| 237 | + z="$c_clear$z" |
268 | 238 | if [ "$w" = "*" ]; then |
269 | | - gitstring="$gitstring\[$bad_color\]$w" |
| 239 | + w="$bad_color$w" |
270 | 240 | fi |
271 | 241 | if [ -n "$i" ]; then |
272 | | - gitstring="$gitstring\[$ok_color\]$i" |
| 242 | + i="$ok_color$i" |
273 | 243 | fi |
274 | 244 | if [ -n "$s" ]; then |
275 | | - gitstring="$gitstring\[$flags_color\]$s" |
| 245 | + s="$flags_color$s" |
276 | 246 | fi |
277 | 247 | if [ -n "$u" ]; then |
278 | | - gitstring="$gitstring\[$bad_color\]$u" |
| 248 | + u="$bad_color$u" |
279 | 249 | fi |
280 | | - gitstring="$gitstring\[$c_clear\]$r$p" |
| 250 | + r="$c_clear$r" |
281 | 251 | } |
282 | 252 |
|
283 | 253 | # __git_ps1 accepts 0 or 1 arguments (i.e., format string) |
@@ -453,22 +423,23 @@ __git_ps1 () |
453 | 423 | fi |
454 | 424 |
|
455 | 425 | local z="${GIT_PS1_STATESEPARATOR-" "}" |
| 426 | + |
| 427 | + # NO color option unless in PROMPT_COMMAND mode |
| 428 | + if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then |
| 429 | + __git_ps1_colorize_gitstring |
| 430 | + fi |
| 431 | + |
456 | 432 | local f="$w$i$s$u" |
| 433 | + local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p" |
| 434 | + |
457 | 435 | if [ $pcmode = yes ]; then |
458 | | - local gitstring= |
459 | | - if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then |
460 | | - __git_ps1_colorize_gitstring |
461 | | - else |
462 | | - gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p" |
463 | | - fi |
464 | 436 | if [[ -n ${ZSH_VERSION-} ]]; then |
465 | 437 | gitstring=$(printf -- "$printf_format" "$gitstring") |
466 | 438 | else |
467 | 439 | printf -v gitstring -- "$printf_format" "$gitstring" |
468 | 440 | fi |
469 | 441 | PS1="$ps1pc_start$gitstring$ps1pc_end" |
470 | 442 | else |
471 | | - # NO color option unless in PROMPT_COMMAND mode |
472 | | - printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p" |
| 443 | + printf -- "$printf_format" "$gitstring" |
473 | 444 | fi |
474 | 445 | } |
0 commit comments