git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH/RFC 0/3] enable color prompt in non-pcmode
@ 2013-06-16 17:32 Eduardo R. D'Avila
  2013-06-16 17:32 ` [PATCH/RFC 1/3] t9903: add tests for git-prompt pcmode Eduardo R. D'Avila
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Eduardo R. D'Avila @ 2013-06-16 17:32 UTC (permalink / raw)
  To: git
  Cc: felipe.contreras, t.gummerer, artagnon, zoltan.klinger, hegge,
	martinerikwerner, s.oosthoek, gitster, jonathan, szeder,
	Eduardo R. D'Avila

The use of colors in a prompt is only possible in
pcmode (using the variable PROMPT_COMMAND).

Make color prompt work in non-pcmode (using the
variable PS1) for both Bash and ZSH.

This requires editing __git_ps1() and __git_ps1_colorize_gitstring(),
which have duplicate code to handle the prompt
gitstring and color codes.

Prior to enable colors in non-pcmode, refactor
__git_ps1() and __git_ps1_colorize_gitstring().

Eduardo R. D'Avila (3):
  t9903: add tests for git-prompt pcmode
  git-prompt.sh: refactor colored prompt code
  git-prompt.sh: enable color prompt in non-pcmode

 contrib/completion/git-prompt.sh |  91 +++++--------
 t/t9903-bash-prompt.sh           | 269 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 301 insertions(+), 59 deletions(-)

-- 
1.8.3.1.440.g82707f8

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH/RFC 1/3] t9903: add tests for git-prompt pcmode
  2013-06-16 17:32 [PATCH/RFC 0/3] enable color prompt in non-pcmode Eduardo R. D'Avila
@ 2013-06-16 17:32 ` Eduardo R. D'Avila
  2013-06-17  4:24   ` Junio C Hamano
  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
  2 siblings, 1 reply; 9+ messages in thread
From: Eduardo R. D'Avila @ 2013-06-16 17:32 UTC (permalink / raw)
  To: git
  Cc: felipe.contreras, t.gummerer, artagnon, zoltan.klinger, hegge,
	martinerikwerner, s.oosthoek, gitster, jonathan, szeder,
	Eduardo R. D'Avila

git-prompt.sh lacks tests for PROMPT_COMMAND mode.

Add tests for:
* pcmode prompt without colors
* pcmode prompt with colors for bash
* pcmode prompt with colors for zsh

Having these tests enables an upcoming refactor in
a safe way.

Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com>
---
250	0	t/t9903-bash-prompt.sh
 t/t9903-bash-prompt.sh | 250 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 250 insertions(+)

diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 15521cc..ebca9de 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -535,4 +535,254 @@ test_expect_success 'prompt - format string starting with dash' '
 	test_cmp expected "$actual"
 '
 
+test_expect_success 'prompt - pc mode' '
+	printf "BEFORE: (master):AFTER" > expected &&
+	printf "" > expected_output &&
+	(
+		__git_ps1 "BEFORE:" ":AFTER" > "$actual" &&
+		test_cmp expected_output "$actual" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - branch name' '
+	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" > expected &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" > "$actual"
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - detached head' '
+	printf "BEFORE: (\\\[\\\e[31m\\\](%s...)\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" $(git log -1 --format="%h" b1^) > expected &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
+	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[31m\\\]*\\\[\\\e[0m\\\]):AFTER" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '
+	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[32m\\\]+\\\[\\\e[0m\\\]):AFTER" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
+	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[31m\\\]*\\\[\\\e[32m\\\]+\\\[\\\e[0m\\\]):AFTER" > expected &&
+	echo "dirty index" > file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	echo "dirty worktree" > file &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '
+	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[32m\\\]#\\\[\\\e[0m\\\]):AFTER" > expected &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		cd otherrepo &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - inside .git directory' '
+	printf "BEFORE: (\\\[\\\e[32m\\\]GIT_DIR!\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		cd .git &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - stash status indicator' '
+	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[1;34m\\\]$\\\[\\\e[0m\\\]):AFTER" > expected &&
+	echo 2 >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	(
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '
+	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[31m\\\]%%\\\[\\\e[0m\\\]):AFTER" > expected &&
+	(
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - branch name' '
+	printf "BEFORE: (%%F{green}master%%f%%f):AFTER" > expected &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" > "$actual"
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+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 &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty worktree' '
+	printf "BEFORE: (%%F{green}master%%f %%F{red}*%%f):AFTER" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index' '
+	printf "BEFORE: (%%F{green}master%%f %%F{green}+%%f):AFTER" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index and worktree' '
+	printf "BEFORE: (%%F{green}master%%f %%F{red}*%%F{green}+%%f):AFTER" > expected &&
+	echo "dirty index" > file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	echo "dirty worktree" > file &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - before root commit' '
+	printf "BEFORE: (%%F{green}master%%f %%F{green}#%%f):AFTER" > expected &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		cd otherrepo &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - inside .git directory' '
+	printf "BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER" > expected &&
+	echo "dirty" > file &&
+	test_when_finished "git reset --hard" &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		cd .git &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - stash status indicator' '
+	printf "BEFORE: (%%F{green}master%%f %%F{blue}$%%f):AFTER" > expected &&
+	echo 2 >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - untracked files status indicator' '
+	printf "BEFORE: (%%F{green}master%%f %%F{red}%%%%%%f):AFTER" > expected &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
 test_done
-- 
1.8.3.1.440.g82707f8

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH/RFC 2/3] git-prompt.sh: refactor colored prompt code
  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-16 17:32 ` 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
  2 siblings, 0 replies; 9+ messages in thread
From: Eduardo R. D'Avila @ 2013-06-16 17:32 UTC (permalink / raw)
  To: git
  Cc: felipe.contreras, t.gummerer, artagnon, zoltan.klinger, hegge,
	martinerikwerner, s.oosthoek, gitster, jonathan, szeder,
	Eduardo R. D'Avila

Remove duplication of logic to build gitstring.

__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 ebca9de..22484c1 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -547,7 +547,7 @@ test_expect_success 'prompt - pc mode' '
 '
 
 test_expect_success 'prompt - bash color pc mode - branch name' '
-	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" > expected &&
+	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\]):AFTER" > expected &&
 	(
 		GIT_PS1_SHOWCOLORHINTS=y &&
 		__git_ps1 "BEFORE:" ":AFTER" > "$actual"
@@ -557,7 +557,7 @@ test_expect_success 'prompt - bash color pc mode - branch name' '
 '
 
 test_expect_success 'prompt - bash color pc mode - detached head' '
-	printf "BEFORE: (\\\[\\\e[31m\\\](%s...)\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" $(git log -1 --format="%h" b1^) > expected &&
+	printf "BEFORE: (\\\[\\\e[31m\\\](%s...)\\\[\\\e[0m\\\]):AFTER" $(git log -1 --format="%h" b1^) > expected &&
 	git checkout b1^ &&
 	test_when_finished "git checkout master" &&
 	(
@@ -623,7 +623,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: (\\\[\\\e[32m\\\]GIT_DIR!\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" > expected &&
+	printf "BEFORE: (\\\[\\\e[32m\\\]GIT_DIR!\\\[\\\e[0m\\\]):AFTER" > expected &&
 	echo "dirty" > file &&
 	test_when_finished "git reset --hard" &&
 	(
@@ -662,7 +662,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 &&
@@ -673,7 +673,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" &&
 	(
@@ -744,7 +744,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

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH/RFC 3/3] git-prompt.sh: enable color prompt in non-pcmode
  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-16 17:32 ` [PATCH/RFC 2/3] git-prompt.sh: refactor colored prompt code Eduardo R. D'Avila
@ 2013-06-16 17:32 ` Eduardo R. D'Avila
  2 siblings, 0 replies; 9+ messages in thread
From: Eduardo R. D'Avila @ 2013-06-16 17:32 UTC (permalink / raw)
  To: git
  Cc: felipe.contreras, t.gummerer, artagnon, zoltan.klinger, hegge,
	martinerikwerner, s.oosthoek, gitster, jonathan, szeder,
	Eduardo R. D'Avila

The use of colors in a prompt is only possible in
pcmode (using the variable PROMPT_COMMAND).

Enable color prompt in non-pcmode (using the variable
PS1) for both Bash and ZSH.

Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com>
---
15	9	contrib/completion/git-prompt.sh
19	0	t/t9903-bash-prompt.sh
 contrib/completion/git-prompt.sh | 24 +++++++++++++++---------
 t/t9903-bash-prompt.sh           | 19 +++++++++++++++++++
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 70515cc..c5c75e7 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -13,7 +13,7 @@
 #    3a) Change your PS1 to call __git_ps1 as
 #        command-substitution:
 #        Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
-#        ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
+#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
 #        the optional argument will be used as format string.
 #    3b) Alternatively, if you are using bash, __git_ps1 can be
 #        used for PROMPT_COMMAND with two parameters, <pre> and
@@ -235,12 +235,18 @@ __git_ps1_colorize_gitstring ()
 		local c_lblue='%F{blue}'
 		local c_clear='%f'
 	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\]'
+		local c_red='\e[31m'
+		local c_green='\e[32m'
+		local c_lblue='\e[1;34m'
+		local c_clear='\e[0m'
+		if [ $pcmode = yes ]; then
+			# Using \[ and \] around colors
+			# is necessary to prevent wrapping issues!
+			c_red="\[$c_red\]"
+			c_green="\[$c_green\]"
+			c_lblue="\[$c_lblue\]"
+			c_clear="\[$c_clear\]"
+		fi
 	fi
 	local bad_color=$c_red
 	local ok_color=$c_green
@@ -411,7 +417,7 @@ __git_ps1 ()
 		local z="${GIT_PS1_STATESEPARATOR-" "}"
 
 		# NO color option unless in PROMPT_COMMAND mode
-		if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
+		if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
 			__git_ps1_colorize_gitstring
 		fi
 
@@ -422,7 +428,7 @@ __git_ps1 ()
 			gitstring=$(printf -- "$printf_format" "$gitstring")
 			PS1="$ps1pc_start$gitstring$ps1pc_end"
 		else
-			printf -- "$printf_format" "$gitstring"
+			printf -- "${printf_format//%s/%b}" "$gitstring"
 		fi
 	fi
 }
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 22484c1..63abc72 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -785,4 +785,23 @@ test_expect_success 'prompt - zsh color pc mode - untracked files status indicat
 	test_cmp expected "$actual"
 '
 
+test_expect_success 'prompt - bash color ps1 mode - untracked files status indicator' '
+	printf " (\e[32mmaster\e[0m)" > expected &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color ps1 mode - untracked files status indicator' '
+	printf " (%%F{green}master%%f)" > expected &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		ZSH_VERSION=5.0.0 &&
+		__git_ps1 > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
 test_done
-- 
1.8.3.1.440.g82707f8

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC 1/3] t9903: add tests for git-prompt pcmode
  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
                       ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Junio C Hamano @ 2013-06-17  4:24 UTC (permalink / raw)
  To: Eduardo R. D'Avila
  Cc: git, felipe.contreras, t.gummerer, artagnon, zoltan.klinger,
	hegge, martinerikwerner, s.oosthoek, jonathan, szeder

"Eduardo R. D'Avila" <erdavila@gmail.com> writes:

> git-prompt.sh lacks tests for PROMPT_COMMAND mode.
>
> Add tests for:
> * pcmode prompt without colors
> * pcmode prompt with colors for bash
> * pcmode prompt with colors for zsh
>
> Having these tests enables an upcoming refactor in
> a safe way.
>
> Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com>
> ---
> 250	0	t/t9903-bash-prompt.sh
>  t/t9903-bash-prompt.sh | 250 +++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 250 insertions(+)
>
> diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
> index 15521cc..ebca9de 100755
> --- a/t/t9903-bash-prompt.sh
> +++ b/t/t9903-bash-prompt.sh
> @@ -535,4 +535,254 @@ test_expect_success 'prompt - format string starting with dash' '
>  	test_cmp expected "$actual"
>  '
>  
> +test_expect_success 'prompt - pc mode' '
> +	printf "BEFORE: (master):AFTER" > expected &&

Style; redirected filename immediately follows the redirection
operator, i.e.

	command >expected

> +	printf "" > expected_output &&
> +	(
> +		__git_ps1 "BEFORE:" ":AFTER" > "$actual" &&
> +		test_cmp expected_output "$actual" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - branch name' '
> +	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" > expected &&

With these escape codes that are hardcoded everywhere, the tests
look pretty much unreadable.  Could they be improved to something
like this (two ${reset} and some other characters may want to be
there):

	printf "BEFORE: (${C_green}master${C_reset}):AFTER"

by adding variable definitions early in this test file?

[the rest of the original left unsnipped for reference; my comments
end here]

> +	(
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" > "$actual"
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - detached head' '
> +	printf "BEFORE: (\\\[\\\e[31m\\\](%s...)\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" $(git log -1 --format="%h" b1^) > expected &&
> +	git checkout b1^ &&
> +	test_when_finished "git checkout master" &&
> +	(
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
> +	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[31m\\\]*\\\[\\\e[0m\\\]):AFTER" > expected &&
> +	echo "dirty" > file &&
> +	test_when_finished "git reset --hard" &&
> +	(
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '
> +	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[32m\\\]+\\\[\\\e[0m\\\]):AFTER" > expected &&
> +	echo "dirty" > file &&
> +	test_when_finished "git reset --hard" &&
> +	git add -u &&
> +	(
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
> +	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[31m\\\]*\\\[\\\e[32m\\\]+\\\[\\\e[0m\\\]):AFTER" > expected &&
> +	echo "dirty index" > file &&
> +	test_when_finished "git reset --hard" &&
> +	git add -u &&
> +	echo "dirty worktree" > file &&
> +	(
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '
> +	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[32m\\\]#\\\[\\\e[0m\\\]):AFTER" > expected &&
> +	(
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		cd otherrepo &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - inside .git directory' '
> +	printf "BEFORE: (\\\[\\\e[32m\\\]GIT_DIR!\\\[\\\e[0m\\\]\\\[\\\e[0m\\\]):AFTER" > expected &&
> +	echo "dirty" > file &&
> +	test_when_finished "git reset --hard" &&
> +	(
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		cd .git &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - stash status indicator' '
> +	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[1;34m\\\]$\\\[\\\e[0m\\\]):AFTER" > expected &&
> +	echo 2 >file &&
> +	git stash &&
> +	test_when_finished "git stash drop" &&
> +	(
> +		GIT_PS1_SHOWSTASHSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '
> +	printf "BEFORE: (\\\[\\\e[32m\\\]master\\\[\\\e[0m\\\] \\\[\\\e[31m\\\]%%\\\[\\\e[0m\\\]):AFTER" > expected &&
> +	(
> +		GIT_PS1_SHOWUNTRACKEDFILES=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - zsh color pc mode - branch name' '
> +	printf "BEFORE: (%%F{green}master%%f%%f):AFTER" > expected &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" > "$actual"
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +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 &&
> +	git checkout b1^ &&
> +	test_when_finished "git checkout master" &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty worktree' '
> +	printf "BEFORE: (%%F{green}master%%f %%F{red}*%%f):AFTER" > expected &&
> +	echo "dirty" > file &&
> +	test_when_finished "git reset --hard" &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index' '
> +	printf "BEFORE: (%%F{green}master%%f %%F{green}+%%f):AFTER" > expected &&
> +	echo "dirty" > file &&
> +	test_when_finished "git reset --hard" &&
> +	git add -u &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index and worktree' '
> +	printf "BEFORE: (%%F{green}master%%f %%F{red}*%%F{green}+%%f):AFTER" > expected &&
> +	echo "dirty index" > file &&
> +	test_when_finished "git reset --hard" &&
> +	git add -u &&
> +	echo "dirty worktree" > file &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - zsh color pc mode - dirty status indicator - before root commit' '
> +	printf "BEFORE: (%%F{green}master%%f %%F{green}#%%f):AFTER" > expected &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		cd otherrepo &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - zsh color pc mode - inside .git directory' '
> +	printf "BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER" > expected &&
> +	echo "dirty" > file &&
> +	test_when_finished "git reset --hard" &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWDIRTYSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		cd .git &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - zsh color pc mode - stash status indicator' '
> +	printf "BEFORE: (%%F{green}master%%f %%F{blue}$%%f):AFTER" > expected &&
> +	echo 2 >file &&
> +	git stash &&
> +	test_when_finished "git stash drop" &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWSTASHSTATE=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - zsh color pc mode - untracked files status indicator' '
> +	printf "BEFORE: (%%F{green}master%%f %%F{red}%%%%%%f):AFTER" > expected &&
> +	(
> +		ZSH_VERSION=5.0.0 &&
> +		GIT_PS1_SHOWUNTRACKEDFILES=y &&
> +		GIT_PS1_SHOWCOLORHINTS=y &&
> +		__git_ps1 "BEFORE:" ":AFTER" &&
> +		printf "%s" "$PS1" > "$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
>  test_done

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/3] t9903: add tests for git-prompt pcmode
  2013-06-17  4:24   ` Junio C Hamano
@ 2013-06-18  2:15     ` Eduardo R. D'Avila
  2013-06-18  2:16     ` [PATCH v2 2/3] git-prompt.sh: refactor colored prompt code Eduardo R. D'Avila
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Eduardo R. D'Avila @ 2013-06-18  2:15 UTC (permalink / raw)
  To: git
  Cc: felipe.contreras, t.gummerer, artagnon, zoltan.klinger, hegge,
	martinerikwerner, s.oosthoek, gitster, jonathan, szeder,
	Eduardo R. D'Avila

git-prompt.sh lacks tests for PROMPT_COMMAND mode.

Add tests for:
* pcmode prompt without colors
* pcmode prompt with colors for bash
* pcmode prompt with colors for zsh

Having these tests enables an upcoming refactor in
a safe way.

Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com>
---
254	0	t/t9903-bash-prompt.sh
 t/t9903-bash-prompt.sh | 254 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 254 insertions(+)

diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 15521cc..6a88778 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -10,6 +10,10 @@ test_description='test git-specific bash prompt functions'
 . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
 
 actual="$TRASH_DIRECTORY/actual"
+c_red='\\[\\e[31m\\]'
+c_green='\\[\\e[32m\\]'
+c_lblue='\\[\\e[1;34m\\]'
+c_clear='\\[\\e[0m\\]'
 
 test_expect_success 'setup for prompt tests' '
 	mkdir -p subdir/subsubdir &&
@@ -535,4 +539,254 @@ test_expect_success 'prompt - format string starting with dash' '
 	test_cmp expected "$actual"
 '
 
+test_expect_success 'prompt - pc mode' '
+	printf "BEFORE: (master):AFTER" >expected &&
+	printf "" >expected_output &&
+	(
+		__git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
+		test_cmp expected_output "$actual" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - branch name' '
+	printf "BEFORE: (${c_green}master${c_clear}${c_clear}):AFTER" >expected &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" >"$actual"
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+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 &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
+	printf "BEFORE: (${c_green}master${c_clear} ${c_red}*${c_clear}):AFTER" >expected &&
+	echo "dirty" >file &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '
+	printf "BEFORE: (${c_green}master${c_clear} ${c_green}+${c_clear}):AFTER" >expected &&
+	echo "dirty" >file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
+	printf "BEFORE: (${c_green}master${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER" >expected &&
+	echo "dirty index" >file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	echo "dirty worktree" >file &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '
+	printf "BEFORE: (${c_green}master${c_clear} ${c_green}#${c_clear}):AFTER" >expected &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		cd otherrepo &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - inside .git directory' '
+	printf "BEFORE: (${c_green}GIT_DIR!${c_clear}${c_clear}):AFTER" >expected &&
+	echo "dirty" >file &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		cd .git &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - stash status indicator' '
+	printf "BEFORE: (${c_green}master${c_clear} ${c_lblue}\$${c_clear}):AFTER" >expected &&
+	echo 2 >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	(
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '
+	printf "BEFORE: (${c_green}master${c_clear} ${c_red}%%${c_clear}):AFTER" >expected &&
+	(
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - branch name' '
+	printf "BEFORE: (%%F{green}master%%f%%f):AFTER" >expected &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" >"$actual"
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+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 &&
+	git checkout b1^ &&
+	test_when_finished "git checkout master" &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty worktree' '
+	printf "BEFORE: (%%F{green}master%%f %%F{red}*%%f):AFTER" >expected &&
+	echo "dirty" >file &&
+	test_when_finished "git reset --hard" &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index' '
+	printf "BEFORE: (%%F{green}master%%f %%F{green}+%%f):AFTER" >expected &&
+	echo "dirty" >file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index and worktree' '
+	printf "BEFORE: (%%F{green}master%%f %%F{red}*%%F{green}+%%f):AFTER" >expected &&
+	echo "dirty index" >file &&
+	test_when_finished "git reset --hard" &&
+	git add -u &&
+	echo "dirty worktree" >file &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - before root commit' '
+	printf "BEFORE: (%%F{green}master%%f %%F{green}#%%f):AFTER" >expected &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		cd otherrepo &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - inside .git directory' '
+	printf "BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER" >expected &&
+	echo "dirty" >file &&
+	test_when_finished "git reset --hard" &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		cd .git &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - stash status indicator' '
+	printf "BEFORE: (%%F{green}master%%f %%F{blue}$%%f):AFTER" >expected &&
+	echo 2 >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWSTASHSTATE=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode - untracked files status indicator' '
+	printf "BEFORE: (%%F{green}master%%f %%F{red}%%%%%%f):AFTER" >expected &&
+	(
+		ZSH_VERSION=5.0.0 &&
+		GIT_PS1_SHOWUNTRACKEDFILES=y &&
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 "BEFORE:" ":AFTER" &&
+		printf "%s" "$PS1" >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
 test_done
-- 
1.8.3.1.440.g82707f8

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 2/3] git-prompt.sh: refactor colored prompt code
  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
  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
  3 siblings, 0 replies; 9+ messages in thread
From: Eduardo R. D'Avila @ 2013-06-18  2:16 UTC (permalink / raw)
  To: git
  Cc: felipe.contreras, t.gummerer, artagnon, zoltan.klinger, hegge,
	martinerikwerner, s.oosthoek, gitster, jonathan, szeder,
	Eduardo R. D'Avila

__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

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 3/3] git-prompt.sh: enable color prompt in non-pcmode
  2013-06-17  4:24   ` Junio C Hamano
  2013-06-18  2:15     ` [PATCH v2 " Eduardo R. D'Avila
  2013-06-18  2:16     ` [PATCH v2 2/3] git-prompt.sh: refactor colored prompt code Eduardo R. D'Avila
@ 2013-06-18  2:17     ` Eduardo R. D'Avila
  2013-06-18  2:19     ` [PATCH v2 " Eduardo R. D'Avila
  3 siblings, 0 replies; 9+ messages in thread
From: Eduardo R. D'Avila @ 2013-06-18  2:17 UTC (permalink / raw)
  To: git
  Cc: felipe.contreras, t.gummerer, artagnon, zoltan.klinger, hegge,
	martinerikwerner, s.oosthoek, gitster, jonathan, szeder,
	Eduardo R. D'Avila

The use of colors in a prompt is only possible in
pcmode (using the variable PROMPT_COMMAND).

Enable color prompt in non-pcmode (using the variable
PS1) for both Bash and ZSH.

Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com>
---
15	9	contrib/completion/git-prompt.sh
19	0	t/t9903-bash-prompt.sh
 contrib/completion/git-prompt.sh | 24 +++++++++++++++---------
 t/t9903-bash-prompt.sh           | 19 +++++++++++++++++++
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 70515cc..c5c75e7 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -13,7 +13,7 @@
 #    3a) Change your PS1 to call __git_ps1 as
 #        command-substitution:
 #        Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
-#        ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
+#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
 #        the optional argument will be used as format string.
 #    3b) Alternatively, if you are using bash, __git_ps1 can be
 #        used for PROMPT_COMMAND with two parameters, <pre> and
@@ -235,12 +235,18 @@ __git_ps1_colorize_gitstring ()
 		local c_lblue='%F{blue}'
 		local c_clear='%f'
 	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\]'
+		local c_red='\e[31m'
+		local c_green='\e[32m'
+		local c_lblue='\e[1;34m'
+		local c_clear='\e[0m'
+		if [ $pcmode = yes ]; then
+			# Using \[ and \] around colors
+			# is necessary to prevent wrapping issues!
+			c_red="\[$c_red\]"
+			c_green="\[$c_green\]"
+			c_lblue="\[$c_lblue\]"
+			c_clear="\[$c_clear\]"
+		fi
 	fi
 	local bad_color=$c_red
 	local ok_color=$c_green
@@ -411,7 +417,7 @@ __git_ps1 ()
 		local z="${GIT_PS1_STATESEPARATOR-" "}"
 
 		# NO color option unless in PROMPT_COMMAND mode
-		if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
+		if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
 			__git_ps1_colorize_gitstring
 		fi
 
@@ -422,7 +428,7 @@ __git_ps1 ()
 			gitstring=$(printf -- "$printf_format" "$gitstring")
 			PS1="$ps1pc_start$gitstring$ps1pc_end"
 		else
-			printf -- "$printf_format" "$gitstring"
+			printf -- "${printf_format//%s/%b}" "$gitstring"
 		fi
 	fi
 }
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 1101adf..7dccc1c 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -789,4 +789,23 @@ test_expect_success 'prompt - zsh color pc mode - untracked files status indicat
 	test_cmp expected "$actual"
 '
 
+test_expect_success 'prompt - bash color ps1 mode - untracked files status indicator' '
+	printf " (\e[32mmaster\e[0m)" >expected &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color ps1 mode - untracked files status indicator' '
+	printf " (%%F{green}master%%f)" >expected &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		ZSH_VERSION=5.0.0 &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
 test_done
-- 
1.8.3.1.440.g82707f8

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 3/3] git-prompt.sh: enable color prompt in non-pcmode
  2013-06-17  4:24   ` Junio C Hamano
                       ` (2 preceding siblings ...)
  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     ` Eduardo R. D'Avila
  3 siblings, 0 replies; 9+ messages in thread
From: Eduardo R. D'Avila @ 2013-06-18  2:19 UTC (permalink / raw)
  To: git
  Cc: felipe.contreras, t.gummerer, artagnon, zoltan.klinger, hegge,
	martinerikwerner, s.oosthoek, gitster, jonathan, szeder,
	Eduardo R. D'Avila

The use of colors in a prompt is only possible in
pcmode (using the variable PROMPT_COMMAND).

Enable color prompt in non-pcmode (using the variable
PS1) for both Bash and ZSH.

Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com>
---
15	9	contrib/completion/git-prompt.sh
19	0	t/t9903-bash-prompt.sh
 contrib/completion/git-prompt.sh | 24 +++++++++++++++---------
 t/t9903-bash-prompt.sh           | 19 +++++++++++++++++++
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 70515cc..c5c75e7 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -13,7 +13,7 @@
 #    3a) Change your PS1 to call __git_ps1 as
 #        command-substitution:
 #        Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
-#        ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
+#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
 #        the optional argument will be used as format string.
 #    3b) Alternatively, if you are using bash, __git_ps1 can be
 #        used for PROMPT_COMMAND with two parameters, <pre> and
@@ -235,12 +235,18 @@ __git_ps1_colorize_gitstring ()
 		local c_lblue='%F{blue}'
 		local c_clear='%f'
 	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\]'
+		local c_red='\e[31m'
+		local c_green='\e[32m'
+		local c_lblue='\e[1;34m'
+		local c_clear='\e[0m'
+		if [ $pcmode = yes ]; then
+			# Using \[ and \] around colors
+			# is necessary to prevent wrapping issues!
+			c_red="\[$c_red\]"
+			c_green="\[$c_green\]"
+			c_lblue="\[$c_lblue\]"
+			c_clear="\[$c_clear\]"
+		fi
 	fi
 	local bad_color=$c_red
 	local ok_color=$c_green
@@ -411,7 +417,7 @@ __git_ps1 ()
 		local z="${GIT_PS1_STATESEPARATOR-" "}"
 
 		# NO color option unless in PROMPT_COMMAND mode
-		if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
+		if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
 			__git_ps1_colorize_gitstring
 		fi
 
@@ -422,7 +428,7 @@ __git_ps1 ()
 			gitstring=$(printf -- "$printf_format" "$gitstring")
 			PS1="$ps1pc_start$gitstring$ps1pc_end"
 		else
-			printf -- "$printf_format" "$gitstring"
+			printf -- "${printf_format//%s/%b}" "$gitstring"
 		fi
 	fi
 }
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 1101adf..7dccc1c 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -789,4 +789,23 @@ test_expect_success 'prompt - zsh color pc mode - untracked files status indicat
 	test_cmp expected "$actual"
 '
 
+test_expect_success 'prompt - bash color ps1 mode - untracked files status indicator' '
+	printf " (\e[32mmaster\e[0m)" >expected &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color ps1 mode - untracked files status indicator' '
+	printf " (%%F{green}master%%f)" >expected &&
+	(
+		GIT_PS1_SHOWCOLORHINTS=y &&
+		ZSH_VERSION=5.0.0 &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
 test_done
-- 
1.8.3.1.440.g82707f8

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-06-18  2:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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     ` [PATCH v2 2/3] git-prompt.sh: refactor colored prompt code Eduardo R. D'Avila
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

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).