git-prompt.sh: refactor colored prompt code

__git_ps1_colorize_gitstring() sets color codes and
builds the prompt gitstring. It has duplicated code
to handle color codes for bash and zsh shells.
__git_ps1() also has duplicated logic to build the
prompt gitstring.

Remove duplication of logic to build gitstring in
__git_ps1_colorize_gitstring() and __git_ps1().

Leave in __git_ps1_colorize_gitstring() only logic
to set color codes.

Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Eduardo R. D'Avila 2013-06-26 00:05:14 -03:00 коммит произвёл Junio C Hamano
Родитель 1572e18e60
Коммит 7fe9031920
1 изменённых файлов: 26 добавлений и 59 удалений

Просмотреть файл

@ -225,8 +225,8 @@ __git_ps1_show_upstream ()
} }
# Helper function that is meant to be called from __git_ps1. It # Helper function that is meant to be called from __git_ps1. It
# builds up a gitstring injecting color codes into the appropriate # injects color codes into the appropriate gitstring variables used
# places. # to build a gitstring.
__git_ps1_colorize_gitstring () __git_ps1_colorize_gitstring ()
{ {
if [[ -n ${ZSH_VERSION-} ]]; then if [[ -n ${ZSH_VERSION-} ]]; then
@ -234,74 +234,40 @@ __git_ps1_colorize_gitstring ()
local c_green='%F{green}' local c_green='%F{green}'
local c_lblue='%F{blue}' local c_lblue='%F{blue}'
local c_clear='%f' local c_clear='%f'
local bad_color=$c_red else
local ok_color=$c_green # Using \[ and \] around colors is necessary to prevent
local branch_color="$c_clear" # issues with command line editing/browsing/completion!
local flags_color="$c_lblue" local c_red='\[\e[31m\]'
local branchstring="$c${b##refs/heads/}" local c_green='\[\e[32m\]'
local c_lblue='\[\e[1;34m\]'
if [ $detached = no ]; then local c_clear='\[\e[0m\]'
branch_color="$ok_color"
else
branch_color="$bad_color"
fi
gitstring="$branch_color$branchstring$c_clear"
if [ -n "$w$i$s$u$r$p" ]; then
gitstring="$gitstring$z"
fi
if [ "$w" = "*" ]; then
gitstring="$gitstring$bad_color$w"
fi
if [ -n "$i" ]; then
gitstring="$gitstring$ok_color$i"
fi
if [ -n "$s" ]; then
gitstring="$gitstring$flags_color$s"
fi
if [ -n "$u" ]; then
gitstring="$gitstring$bad_color$u"
fi
gitstring="$gitstring$c_clear$r$p"
return
fi fi
local c_red='\e[31m'
local c_green='\e[32m'
local c_lblue='\e[1;34m'
local c_clear='\e[0m'
local bad_color=$c_red local bad_color=$c_red
local ok_color=$c_green local ok_color=$c_green
local branch_color="$c_clear"
local flags_color="$c_lblue" local flags_color="$c_lblue"
local branchstring="$c${b##refs/heads/}"
local branch_color=""
if [ $detached = no ]; then if [ $detached = no ]; then
branch_color="$ok_color" branch_color="$ok_color"
else else
branch_color="$bad_color" branch_color="$bad_color"
fi fi
c="$branch_color$c"
b="$b$c_clear"
# Setting gitstring directly with \[ and \] around colors
# is necessary to prevent wrapping issues!
gitstring="\[$branch_color\]$branchstring\[$c_clear\]"
if [ -n "$w$i$s$u$r$p" ]; then
gitstring="$gitstring$z"
fi
if [ "$w" = "*" ]; then if [ "$w" = "*" ]; then
gitstring="$gitstring\[$bad_color\]$w" w="$bad_color$w"
fi fi
if [ -n "$i" ]; then if [ -n "$i" ]; then
gitstring="$gitstring\[$ok_color\]$i" i="$ok_color$i"
fi fi
if [ -n "$s" ]; then if [ -n "$s" ]; then
gitstring="$gitstring\[$flags_color\]$s" s="$flags_color$s"
fi fi
if [ -n "$u" ]; then if [ -n "$u" ]; then
gitstring="$gitstring\[$bad_color\]$u" u="$bad_color$u"
fi fi
gitstring="$gitstring\[$c_clear\]$r$p" r="$c_clear$r"
} }
# __git_ps1 accepts 0 or 1 arguments (i.e., format string) # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
@ -443,19 +409,20 @@ __git_ps1 ()
fi fi
local z="${GIT_PS1_STATESEPARATOR-" "}" local z="${GIT_PS1_STATESEPARATOR-" "}"
# NO color option unless in PROMPT_COMMAND mode
if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
__git_ps1_colorize_gitstring
fi
local f="$w$i$s$u" local f="$w$i$s$u"
local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
if [ $pcmode = yes ]; then if [ $pcmode = yes ]; then
local gitstring=
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
__git_ps1_colorize_gitstring
else
gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
fi
gitstring=$(printf -- "$printf_format" "$gitstring") gitstring=$(printf -- "$printf_format" "$gitstring")
PS1="$ps1pc_start$gitstring$ps1pc_end" PS1="$ps1pc_start$gitstring$ps1pc_end"
else else
# NO color option unless in PROMPT_COMMAND mode printf -- "$printf_format" "$gitstring"
printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"
fi fi
fi fi
} }