Re: [PATCH v2] git-prompt.sh: colorize ZSH prompt
From: Felipe Contreras <hidden>
Date: 2016-06-15 22:57:13
Subsystem:
the rest · Maintainer:
Linus Torvalds
On Sat, May 11, 2013 at 5:40 PM, Ramkumar Ramachandra [off-list ref] wrote:
Felipe Contreras wrote:quoted
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 [off-list ref]
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 [off-list ref]
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 [off-list ref]
Date: Sat May 11 18:03:39 2013 -0500
prompt: add colorization for zsh
Signed-off-by: Felipe Contreras [off-list ref]
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