git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH/RFC 3/3] git-prompt.sh: enable color prompt in non-pcmode

From
Eduardo R. D'Avila <erdavila@gmail.com>
Date
Jun 16, 2013, 17:32 UTC
Message-ID
<c875c17e4745d57bd26b680e82aa221659faf3cd.1371402672.git.erdavila@gmail.com>
In-Reply-To
<cover.1371402672.git.erdavila@gmail.com>

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
Previous: Eduardo R. D'Avila
Message 9 of 9 in “enable color prompt in non-pcmode”
  1. 0/3 enable color prompt in non-pcmodeEduardo R. D'Avila, Jun 16, 2013
  2. 1/3 t9903: add tests for git-prompt pcmodeEduardo R. D'Avila, Jun 16, 2013
  3. Junio C HamanoJun 17, 2013
  4. 1/3 t9903: add tests for git-prompt pcmodeEduardo R. D'Avila, Jun 18, 2013
  5. 2/3 git-prompt.sh: refactor colored prompt codeEduardo R. D'Avila, Jun 18, 2013
  6. 3/3 git-prompt.sh: enable color prompt in non-pcmodeEduardo R. D'Avila, Jun 18, 2013
  7. 3/3 git-prompt.sh: enable color prompt in non-pcmodeEduardo R. D'Avila, Jun 18, 2013
  8. 2/3 git-prompt.sh: refactor colored prompt codeEduardo R. D'Avila, Jun 16, 2013
  9. 3/3 git-prompt.sh: enable color prompt in non-pcmodeEduardo R. D'Avila, Jun 16, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.