From: "Eduardo R. D'Avila" <erdavila@gmail.com>
To: git@vger.kernel.org
Cc: felipe.contreras@gmail.com, t.gummerer@gmail.com,
artagnon@gmail.com, zoltan.klinger@gmail.com, hegge@resisty.net,
martinerikwerner@gmail.com, s.oosthoek@xs4all.nl,
gitster@pobox.com, jonathan@leto.net, szeder@ira.uka.de,
"Eduardo R. D'Avila" <erdavila@gmail.com>
Subject: [PATCH v2 2/3] git-prompt.sh: refactor colored prompt code
Date: Mon, 17 Jun 2013 23:16:51 -0300 [thread overview]
Message-ID: <1371521811-9819-1-git-send-email-erdavila@gmail.com> (raw)
In-Reply-To: <7vhagxicu9.fsf@alter.siamese.dyndns.org>
__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>
---
26 59 contrib/completion/git-prompt.sh
6 6 t/t9903-bash-prompt.sh
contrib/completion/git-prompt.sh | 85 ++++++++++++----------------------------
t/t9903-bash-prompt.sh | 12 +++---
2 files changed, 32 insertions(+), 65 deletions(-)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 86a4f3f..70515cc 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -225,8 +225,8 @@ __git_ps1_show_upstream ()
}
# Helper function that is meant to be called from __git_ps1. It
-# builds up a gitstring injecting color codes into the appropriate
-# places.
+# injects color codes into the appropriate gitstring variables used
+# to build a gitstring.
__git_ps1_colorize_gitstring ()
{
if [[ -n ${ZSH_VERSION-} ]]; then
@@ -234,74 +234,40 @@ __git_ps1_colorize_gitstring ()
local c_green='%F{green}'
local c_lblue='%F{blue}'
local c_clear='%f'
- local bad_color=$c_red
- local ok_color=$c_green
- local branch_color="$c_clear"
- local flags_color="$c_lblue"
- local branchstring="$c${b##refs/heads/}"
-
- if [ $detached = no ]; then
- 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
+ else
+ # Using \[ and \] around colors
+ # is necessary to prevent wrapping issues!
+ local c_red='\[\e[31m\]'
+ local c_green='\[\e[32m\]'
+ local c_lblue='\[\e[1;34m\]'
+ local c_clear='\[\e[0m\]'
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 ok_color=$c_green
- local branch_color="$c_clear"
local flags_color="$c_lblue"
- local branchstring="$c${b##refs/heads/}"
+ local branch_color=""
if [ $detached = no ]; then
branch_color="$ok_color"
else
branch_color="$bad_color"
fi
+ c="$branch_color$c"
- # 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
+ z="$c_clear$z"
if [ "$w" = "*" ]; then
- gitstring="$gitstring\[$bad_color\]$w"
+ w="$bad_color$w"
fi
if [ -n "$i" ]; then
- gitstring="$gitstring\[$ok_color\]$i"
+ i="$ok_color$i"
fi
if [ -n "$s" ]; then
- gitstring="$gitstring\[$flags_color\]$s"
+ s="$flags_color$s"
fi
if [ -n "$u" ]; then
- gitstring="$gitstring\[$bad_color\]$u"
+ u="$bad_color$u"
fi
- gitstring="$gitstring\[$c_clear\]$r$p"
+ r="$c_clear$r"
}
# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
@@ -443,19 +409,20 @@ __git_ps1 ()
fi
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 gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
+
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")
PS1="$ps1pc_start$gitstring$ps1pc_end"
else
- # NO color option unless in PROMPT_COMMAND mode
- printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"
+ printf -- "$printf_format" "$gitstring"
fi
fi
}
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 6a88778..1101adf 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -551,7 +551,7 @@ test_expect_success 'prompt - pc mode' '
'
test_expect_success 'prompt - bash color pc mode - branch name' '
- printf "BEFORE: (${c_green}master${c_clear}${c_clear}):AFTER" >expected &&
+ printf "BEFORE: (${c_green}master${c_clear}):AFTER" >expected &&
(
GIT_PS1_SHOWCOLORHINTS=y &&
__git_ps1 "BEFORE:" ":AFTER" >"$actual"
@@ -561,7 +561,7 @@ test_expect_success 'prompt - bash color pc mode - branch name' '
'
test_expect_success 'prompt - bash color pc mode - detached head' '
- printf "BEFORE: (${c_red}(%s...)${c_clear}${c_clear}):AFTER" $(git log -1 --format="%h" b1^) >expected &&
+ printf "BEFORE: (${c_red}(%s...)${c_clear}):AFTER" $(git log -1 --format="%h" b1^) >expected &&
git checkout b1^ &&
test_when_finished "git checkout master" &&
(
@@ -627,7 +627,7 @@ test_expect_success 'prompt - bash color pc mode - dirty status indicator - befo
'
test_expect_success 'prompt - bash color pc mode - inside .git directory' '
- printf "BEFORE: (${c_green}GIT_DIR!${c_clear}${c_clear}):AFTER" >expected &&
+ printf "BEFORE: (${c_green}GIT_DIR!${c_clear}):AFTER" >expected &&
echo "dirty" >file &&
test_when_finished "git reset --hard" &&
(
@@ -666,7 +666,7 @@ test_expect_success 'prompt - bash color pc mode - untracked files status indica
'
test_expect_success 'prompt - zsh color pc mode - branch name' '
- printf "BEFORE: (%%F{green}master%%f%%f):AFTER" >expected &&
+ printf "BEFORE: (%%F{green}master%%f):AFTER" >expected &&
(
ZSH_VERSION=5.0.0 &&
GIT_PS1_SHOWCOLORHINTS=y &&
@@ -677,7 +677,7 @@ test_expect_success 'prompt - zsh color pc mode - branch name' '
'
test_expect_success 'prompt - zsh color pc mode - detached head' '
- printf "BEFORE: (%%F{red}(%s...)%%f%%f):AFTER" $(git log -1 --format="%h" b1^) >expected &&
+ printf "BEFORE: (%%F{red}(%s...)%%f):AFTER" $(git log -1 --format="%h" b1^) >expected &&
git checkout b1^ &&
test_when_finished "git checkout master" &&
(
@@ -748,7 +748,7 @@ test_expect_success 'prompt - zsh color pc mode - dirty status indicator - befor
'
test_expect_success 'prompt - zsh color pc mode - inside .git directory' '
- printf "BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER" >expected &&
+ printf "BEFORE: (%%F{green}GIT_DIR!%%f):AFTER" >expected &&
echo "dirty" >file &&
test_when_finished "git reset --hard" &&
(
--
1.8.3.1.440.g82707f8
next prev parent reply other threads:[~2013-06-18 2:17 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-16 17:32 [PATCH/RFC 0/3] enable color prompt in non-pcmode Eduardo R. D'Avila
2013-06-16 17:32 ` [PATCH/RFC 1/3] t9903: add tests for git-prompt pcmode Eduardo R. D'Avila
2013-06-17 4:24 ` Junio C Hamano
2013-06-18 2:15 ` [PATCH v2 " Eduardo R. D'Avila
2013-06-18 2:16 ` Eduardo R. D'Avila [this message]
2013-06-18 2:17 ` [PATCH 3/3] git-prompt.sh: enable color prompt in non-pcmode Eduardo R. D'Avila
2013-06-18 2:19 ` [PATCH v2 " Eduardo R. D'Avila
2013-06-16 17:32 ` [PATCH/RFC 2/3] git-prompt.sh: refactor colored prompt code Eduardo R. D'Avila
2013-06-16 17:32 ` [PATCH/RFC 3/3] git-prompt.sh: enable color prompt in non-pcmode Eduardo R. D'Avila
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1371521811-9819-1-git-send-email-erdavila@gmail.com \
--to=erdavila@gmail.com \
--cc=artagnon@gmail.com \
--cc=felipe.contreras@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=hegge@resisty.net \
--cc=jonathan@leto.net \
--cc=martinerikwerner@gmail.com \
--cc=s.oosthoek@xs4all.nl \
--cc=szeder@ira.uka.de \
--cc=t.gummerer@gmail.com \
--cc=zoltan.klinger@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).