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

Re: [PATCH v2] git-prompt.sh: colorize ZSH prompt

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
May 11, 2013, 23:06 UTC
Message-ID
<CAMP44s0QKGVzWohsbyN_7EdW1WxHvqDrjpuCykxNFkUKi=XKbA@mail.gmail.com>
In-Reply-To
<CALkWK0mySjW_QGObkXWvhzMa0gbwKDire4Dx9=PV+9OnEgZjsQ@mail.gmail.com>

On Sat, May 11, 2013 at 5:40 PM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:

Show 9 quoted lines
> Felipe Contreras wrote:
>> Not really. If we need to avoid the \[\], it makes sense to have a
>> separate function, but what I meant is that this function should be
>> initially on the same file, and created in a separate patch.
>
> What are you saying?
>
> 1. It makes sense to have a separate function specifically for ZSH,
> but it should be located in the same physical file (why?)
This is what I mean:
commit 4726a5f76ec7d96c34863d6640488e2183cc8f00
Author: Ramkumar Ramachandra <artagnon@gmail.com>
Date:   Sat May 11 21:55:13 2013 +0530
    prompt: split code into __git_ps1_colorize_gitstring()
    Will be useful for reorganizations.
    Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index eaf5c36..1187292 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -222,6 +222,45 @@ __git_ps1_show_upstream ()

 }

+__git_ps1_colorize_gitstring ()
+{
+	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/}"
+
+	if [ $detached = no ]; then
+		branch_color="$ok_color"
+	else
+		branch_color="$bad_color"
+	fi
+
+	# 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 "
+	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"
+}

 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
 # when called from PS1 using command substitution
@@ -363,42 +402,7 @@ __git_ps1 ()
 		if [ $pcmode = yes ]; then
 			local gitstring=
 			if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
-				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/}"
-
-				if [ $detached = no ]; then
-					branch_color="$ok_color"
-				else
-					branch_color="$bad_color"
-				fi
-
-				# 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 "
-				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"
+				__git_ps1_colorize_gitstring
 			else
 				gitstring="$c${b##refs/heads/}${f:+ $f}$r$p"
 			fi

commit 23d89b370ac36401da1d9a98754e222a7a4c93e5
Author: Felipe Contreras <felipe.contreras@gmail.com>
Date:   Sat May 11 18:03:39 2013 -0500

    prompt: add colorization for zsh

    Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 1187292..8946a29 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -224,6 +224,8 @@ __git_ps1_show_upstream ()

 __git_ps1_colorize_gitstring ()
 {
+	if [[ -z ${ZSH_VERSION-} ]]
+	then
 		local c_red='\e[31m'
 		local c_green='\e[32m'
 		local c_lblue='\e[1;34m'
@@ -260,6 +262,39 @@ __git_ps1_colorize_gitstring ()
 			gitstring="$gitstring\[$bad_color\]$u"
 		fi
 		gitstring="$gitstring\[$c_clear\]$r$p"
+	else
+		local c_red='%F{red}'
+		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 [ "$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"
+	fi
 }

 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
-- 
Felipe Contreras
Previous: Ramkumar Ramachandra
Message 16 of 16 in “Colored prompt for zsh”
  1. 0/2 Colored prompt for zshRamkumar Ramachandra, May 11, 2013
  2. 1/2 git-prompt.sh: strip unnecessary space in prompt stringRamkumar Ramachandra, May 11, 2013
  3. Junio C HamanoMay 13, 2013
  4. Ramkumar RamachandraMay 13, 2013
  5. SZEDER GáborMay 13, 2013
  6. Ramkumar RamachandraMay 13, 2013
  7. SZEDER GáborMay 13, 2013
  8. SZEDER GáborMay 13, 2013
  9. Junio C HamanoMay 13, 2013
  10. Ramkumar RamachandraMay 13, 2013
  11. 2/2 git-prompt.zsh: introduce thin ZSH wrapperRamkumar Ramachandra, May 11, 2013
  12. Felipe ContrerasMay 11, 2013
  13. git-prompt.sh: colorize ZSH promptRamkumar Ramachandra, May 11, 2013
  14. Felipe ContrerasMay 11, 2013
  15. Ramkumar RamachandraMay 11, 2013
  16. Felipe ContrerasMay 11, 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.