threads / patch / 33797

patch, 2 partsColored prompt for zsh

Subject: [PATCH 0/2] Colored prompt for zsh

## tl;dr

16 messages between May 11, 2013 and May 13, 2013. Diffs are folded; open one to read it.

replies: 15people: 4as markdown or json

Ramkumar Ramachandra· May 11, 2013, 16:25 UTC · lore
Hi,

I was using ZSH's vcs_info until now, and just discovered contrib/completion/git-prompt.sh. Other differences aside, it's simple enough that I can hack on it.

The first patch strips a small whitespace (we can't waste valuable real estate on unnecessary whitespace), and the second patch should get you a beautiful colored prompt in ZSH.

Enjoy!
Ramkumar Ramachandra (2):
  git-prompt.sh: strip unnecessary space in prompt string
  git-prompt.zsh: introduce thin ZSH wrapper
 contrib/completion/git-prompt.sh  | 80 ++++++++++++++++++++-------------------
 contrib/completion/git-prompt.zsh | 59 +++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 38 deletions(-)
 create mode 100644 contrib/completion/git-prompt.zsh
-- 
1.8.3.rc1.52.g4537cf1
Ramkumar Ramachandra· May 11, 2013, 16:25 UTC · re: Ramkumar Ramachandra · lore

[PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

Nobody has branch names that end with + or *. Then why put a space after the branch name and before [*|+][=|<|>] in the prompt string?

Before this, your prompt might have looked like:
    artagnon|master *=:~/src/git$
Now, it will look like:
    artagnon|master*=:~/src/git$
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 contrib/completion/git-prompt.sh | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
Show changes to contrib/completion/git-prompt.sh +2 −5
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index eaf5c36..08c9b22 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -383,9 +383,6 @@ __git_ps1 ()
 				# 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
@@ -400,13 +397,13 @@ __git_ps1 ()
 				fi
 				gitstring="$gitstring\[$c_clear\]$r$p"
 			else
-				gitstring="$c${b##refs/heads/}${f:+ $f}$r$p"
+				gitstring="$c${b##refs/heads/}${f:+$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:+ $f}$r$p"
+			printf -- "$printf_format" "$c${b##refs/heads/}${f:+$f}$r$p"
 		fi
 	fi
 }
-- 
1.8.3.rc1.52.g4537cf1
Junio C Hamano· May 13, 2013, 04:57 UTC · re: Ramkumar Ramachandra · lore

Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

Ramkumar Ramachandra <artagnon@gmail.com> writes:
> Nobody has branch names that end with + or *.  Then why put a space
> after the branch name and before [*|+][=|<|>] in the prompt string?
I do not think the space is for disambiguation.

It is like asking why typeset a space after a sentence a tad wider than a space after each word: grouping related things together, and separating groups of different kind apart, to make it more readable.

Ramkumar Ramachandra· May 13, 2013, 07:40 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

Junio C Hamano wrote:
> It is like asking why typeset a space after a sentence a tad wider
> than a space after each word:
Not at all.  The prompt has nothing to do with sentences.
> grouping related things together, and
> separating groups of different kind apart, to make it more readable.

You could argue that the colors are not very well thought-out (branch-name has the same color as +), but I really don't see the problem otherwise. How is this more readable:

    artagnon|master *=:~/src/git$
than:
    artagnon|master*=:~/src/git$
(where master is green, * is red, and = is white)?

Looking at what we've done so far, I would argue that we're not thinking about the usability of the prompt enough: it currently screams I AM DOING REBASE-i! (and similarly for other operations in progress). I intend to fix these things in future patches.

Thanks.
SZEDER Gábor· May 13, 2013, 09:17 UTC · re: Ramkumar Ramachandra · lore

Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

On Mon, May 13, 2013 at 01:10:26PM +0530, Ramkumar Ramachandra wrote:
Show 15 quoted lines
> Junio C Hamano wrote:
> > grouping related things together, and
> > separating groups of different kind apart, to make it more readable.
> 
> You could argue that the colors are not very well thought-out
> (branch-name has the same color as +), but I really don't see the
> problem otherwise.  How is this more readable:
> 
>     artagnon|master *=:~/src/git$
> 
> than:
> 
>     artagnon|master*=:~/src/git$
> 
> (where master is green, * is red, and = is white)?
IMHO that's just ugly ;)

Don't forget that others might use a different prompt format and using colors in the prompt is optional. With the default prompt format and without colors I find that that space definitely helps readability.

Gábor
Ramkumar Ramachandra· May 13, 2013, 10:13 UTC · re: SZEDER Gábor · lore

Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

SZEDER Gábor wrote:
> Don't forget that others might use a different prompt format and using
> colors in the prompt is optional.  With the default prompt format and
> without colors I find that that space definitely helps readability.
I'm finding it hard to believe that anyone would want to use a colorless prompt.
> IMHO that's just ugly ;)

If we can agree that it's just a matter of taste, we should both be able to have what we want. Any suggestions on how to make this configurable?

SZEDER Gábor· May 13, 2013, 11:05 UTC · re: Ramkumar Ramachandra · lore

Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

On Mon, May 13, 2013 at 03:43:43PM +0530, Ramkumar Ramachandra wrote:
Show 6 quoted lines
> SZEDER Gábor wrote:
> > Don't forget that others might use a different prompt format and using
> > colors in the prompt is optional.  With the default prompt format and
> > without colors I find that that space definitely helps readability.
> 
> I'm finding it hard to believe that anyone would want to use a colorless prompt.

Well, back then I was shell-shocked to find that there are people who want colors in their prompt ;)

Show 5 quoted lines
> > IMHO that's just ugly ;)
> 
> If we can agree that it's just a matter of taste, we should both be
> able to have what we want.  Any suggestions on how to make this
> configurable?

The same way we make other things configurable in the prompt: specify the GIT_PS1_HIDESTATESEPARATOR or something similar variable to strip that space from the prompt.

SZEDER Gábor· May 13, 2013, 11:11 UTC · re: SZEDER Gábor · lore

Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

On Mon, May 13, 2013 at 01:05:51PM +0200, SZEDER Gábor wrote:
Show 8 quoted lines
> On Mon, May 13, 2013 at 03:43:43PM +0530, Ramkumar Ramachandra wrote:
> > If we can agree that it's just a matter of taste, we should both be
> > able to have what we want.  Any suggestions on how to make this
> > configurable?
> 
> The same way we make other things configurable in the prompt: specify
> the GIT_PS1_HIDESTATESEPARATOR or something similar variable to strip
> that space from the prompt.

Or perhaps better yet: use a variable, e.g. GIT_PS1_STATESEPARATOR or whatever, to specify what should separate the branch name from the repo state, defaulting to a space to keep the current behavior.

Gábor
Junio C Hamano· May 13, 2013, 13:58 UTC · re: SZEDER Gábor · lore

Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

SZEDER Gábor <szeder@ira.uka.de> writes:
Show 13 quoted lines
> On Mon, May 13, 2013 at 01:05:51PM +0200, SZEDER Gábor wrote:
>> On Mon, May 13, 2013 at 03:43:43PM +0530, Ramkumar Ramachandra wrote:
>> > If we can agree that it's just a matter of taste, we should both be
>> > able to have what we want.  Any suggestions on how to make this
>> > configurable?
>> 
>> The same way we make other things configurable in the prompt: specify
>> the GIT_PS1_HIDESTATESEPARATOR or something similar variable to strip
>> that space from the prompt.
>
> Or perhaps better yet: use a variable, e.g. GIT_PS1_STATESEPARATOR or
> whatever, to specify what should separate the branch name from the
> repo state, defaulting to a space to keep the current behavior.
Why just a single knob for that SP?  

If you restructure the code to formulate "gitstring" to call a shell function to do so, people can override it and come up with whatever format they want to use, no?

Ramkumar Ramachandra· May 13, 2013, 14:21 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string

Junio C Hamano wrote:
> If you restructure the code to formulate "gitstring" to call a shell
> function to do so, people can override it and come up with whatever
> format they want to use, no?

The user would essentially have to implement her own __git_ps1_colorize_string; a function that takes a lot of arguments and stitches them together with color. Much too painful for little gain.

Ramkumar Ramachandra· May 11, 2013, 16:25 UTC · re: Ramkumar Ramachandra · lore

[PATCH 2/2] git-prompt.zsh: introduce thin ZSH wrapper

To facilitate a colored prompt in ZSH, write a thin wrapper around git-prompt.sh, factoring out and overriding the coloring logic. Since ZSH lacks a PROMPT_COMMAND, instruct the user to execute __git_ps1 inside precmd().

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 contrib/completion/git-prompt.sh  | 73 +++++++++++++++++++++------------------
 contrib/completion/git-prompt.zsh | 59 +++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+), 33 deletions(-)
 create mode 100644 contrib/completion/git-prompt.zsh
Show changes to 2 files +99 −33

contrib/completion/git-prompt.sh, contrib/completion/git-prompt.zsh

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 08c9b22..0bc51ad 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -222,6 +222,45 @@ __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.
+__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 [ "$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,39 +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 [ "$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
diff --git a/contrib/completion/git-prompt.zsh b/contrib/completion/git-prompt.zsh
new file mode 100644
index 0000000..dc164dd
--- /dev/null
+++ b/contrib/completion/git-prompt.zsh
@@ -0,0 +1,59 @@
+# git prompt support for zsh: wrapper around git-prompt.sh
+#
+# To enable:
+#
+#    1) Copy this file and git-prompt.sh to ~/.zsh/prompt
+#    2) Add the following lines to your .zshrc:
+#
+#          source ~/.zsh/prompt/git-prompt.zsh
+#          GIT_PS1_SHOWCOLORHINTS=true
+#          precmd () { __git_ps1 "%n|" ":%~$ " "%s" }
+#
+#    3) You can now add the following to ~/.zshrc and expect the
+#       characters to be displayed in color:
+#
+#          GIT_PS1_DESCRIBE_STYLE=branch
+#          GIT_PS1_SHOWUPSTREAM=auto
+#          GIT_PS1_SHOWDIRTYSTATE=true
+#          GIT_PS1_SHOWUNTRACKEDFILES=true
+
+test -z "$script" && script="$(dirname ${funcsourcetrace[1]%:*})"/git-prompt.sh
+ZSH_VERSION='' . "$script"
+
+autoload colors
+colors
+
+__git_ps1_colorize_gitstring ()
+{
+	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"
+}
-- 
1.8.3.rc1.52.g4537cf1
Felipe Contreras· May 11, 2013, 21:26 UTC · re: Ramkumar Ramachandra · lore

Re: [PATCH 2/2] git-prompt.zsh: introduce thin ZSH wrapper

On Sat, May 11, 2013 at 11:25 AM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:

> To facilitate a colored prompt in ZSH, write a thin wrapper around
> git-prompt.sh, factoring out and overriding the coloring logic.  Since
> ZSH lacks a PROMPT_COMMAND, instruct the user to execute __git_ps1
> inside precmd().
I think this is two logical changes into one.
Show 24 quoted lines
> --- /dev/null
> +++ b/contrib/completion/git-prompt.zsh
> @@ -0,0 +1,59 @@
> +# git prompt support for zsh: wrapper around git-prompt.sh
> +#
> +# To enable:
> +#
> +#    1) Copy this file and git-prompt.sh to ~/.zsh/prompt
> +#    2) Add the following lines to your .zshrc:
> +#
> +#          source ~/.zsh/prompt/git-prompt.zsh
> +#          GIT_PS1_SHOWCOLORHINTS=true
> +#          precmd () { __git_ps1 "%n|" ":%~$ " "%s" }
> +#
> +#    3) You can now add the following to ~/.zshrc and expect the
> +#       characters to be displayed in color:
> +#
> +#          GIT_PS1_DESCRIBE_STYLE=branch
> +#          GIT_PS1_SHOWUPSTREAM=auto
> +#          GIT_PS1_SHOWDIRTYSTATE=true
> +#          GIT_PS1_SHOWUNTRACKEDFILES=true
> +
> +test -z "$script" && script="$(dirname ${funcsourcetrace[1]%:*})"/git-prompt.sh
> +ZSH_VERSION='' . "$script"

I've been thinking that this method of loading the script is not the best; we should probably have a list of locations where distributions usually put this file. So if we could avoid the creation of of a new file, that would be great.

Show 14 quoted lines
> +autoload colors
> +colors
> +
> +__git_ps1_colorize_gitstring ()
> +{
> +       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/}"

This is the only real difference with bash colors, no? I think it's overkill to create a new file, and load the old one, only to override part of a function. We should probably start with everything in the same file, and only later if we find it's necessary, split.

Show 23 quoted lines
> +       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"
> +}
> --
-- 
Felipe Contreras
Ramkumar Ramachandra· May 11, 2013, 22:18 UTC · re: Felipe Contreras · lore

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

Add colors suitable for use in the ZSH prompt. Having learnt that the ZSH equivalent of PROMPT_COMMAND is precmd (), you can now use GIT_PS1_SHOWCOLORHINTS with ZSH.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 You like this more?  I don't mind going either way.
 contrib/completion/git-prompt.sh | 67 ++++++++++++++++++++++++++++------------
 1 file changed, 47 insertions(+), 20 deletions(-)
Show changes to contrib/completion/git-prompt.sh +47 −20
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 08c9b22..26e5bc2 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -20,7 +20,8 @@
 #        <post>, which are strings you would put in $PS1 before
 #        and after the status string generated by the git-prompt
 #        machinery.  e.g.
-#           PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
+#        Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
+#        ZSH: precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
 #        will show username, at-sign, host, colon, cwd, then
 #        various status string, followed by dollar and SP, as
 #        your prompt.
@@ -363,10 +364,18 @@ __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'
+				if [[ -n ${ZSH_VERSION-} ]]; then
+					local c_red='%F{red}'
+					local c_green='%F{green}'
+					local c_lblue='%F{blue}'
+					local c_clear='%f'
+				else
+					local c_red='\e[31m'
+					local c_green='\e[32m'
+					local c_lblue='\e[1;34m'
+					local c_clear='\e[0m'
+				fi
+
 				local bad_color=$c_red
 				local ok_color=$c_green
 				local branch_color="$c_clear"
@@ -379,23 +388,41 @@ __git_ps1 ()
 					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 ${ZSH_VERSION-} ]]; then
+					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"
+					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"
+				else
+					# Setting gitstring directly with \[ and \] around colors
+					# is necessary to prevent wrapping issues!
+					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
-				gitstring="$gitstring\[$c_clear\]$r$p"
 			else
 				gitstring="$c${b##refs/heads/}${f:+$f}$r$p"
 			fi
-- 
1.8.3.rc1.46.gfc3cab0
Felipe Contreras· May 11, 2013, 22:29 UTC · re: Ramkumar Ramachandra · lore

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

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

Show 7 quoted lines
> Add colors suitable for use in the ZSH prompt.  Having learnt that the
> ZSH equivalent of PROMPT_COMMAND is precmd (), you can now use
> GIT_PS1_SHOWCOLORHINTS with ZSH.
>
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
> ---
>  You like this more?  I don't mind going either way.

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.

-- 
Felipe Contreras
Ramkumar Ramachandra· May 11, 2013, 22:40 UTC · re: Felipe Contreras · lore

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

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?)
2. You like the original patch, but would prefer two different parts (a nit)
Felipe Contreras· May 11, 2013, 23:06 UTC · re: Ramkumar Ramachandra · lore

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

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>
Show changes to contrib/completion/git-prompt.sh +75 −36
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

← back to recent threads