Re: [PATCH v2] git-prompt.sh: colorize ZSH prompt
On Sat, May 11, 2013 at 5:40 PM, Ramkumar Ramachandra wrote: > 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 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 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 Date: Sat May 11 18:03:39 2013 -0500 prompt: add colorization for zsh Signed-off-by: Felipe Contreras 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 @
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) -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v2] git-prompt.sh: colorize ZSH prompt
On Sat, May 11, 2013 at 5:18 PM, Ramkumar Ramachandra wrote: > 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 > --- > 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 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
[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 --- You like this more? I don't mind going either way. contrib/completion/git-prompt.sh | 67 1 file changed, 47 insertions(+), 20 deletions(-) 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 @@ #, 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" +