Colored prompt for zsh
cover Colored prompt for zsh
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
Signature · 1 line
1.8.3.rc1.52.g4537cf1
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$
--- contrib/completion/git-prompt.sh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
contrib/completion/git-prompt.sh +2 -5
@@ -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 }
Signature · 1 line
1.8.3.rc1.52.g4537cf1
Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string
Quoted 2 lines · Ramkumar Ramachandra
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.
Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string
Quoted 2 lines · Junio C Hamano
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.
Quoted 2 lines
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.
Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string
Quoted 8 lines · Ramkumar Ramachandra
Quoted 2 lines · Junio C Hamano
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:
Quoted 7 lines
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
Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string
Quoted 3 lines · SZEDER Gábor
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.
Quoted 1 line
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?
Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string
Quoted 6 lines · Ramkumar Ramachandra
Quoted 3 lines · SZEDER Gábor
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 ;)
Quoted 5 lines
Quoted 1 line
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.
Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string
Quoted 8 lines · SZEDER Gábor
Quoted 3 lines · Ramkumar Ramachandra
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_HIDESTATESEPARATORor 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
Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string
Quoted 6 lines · SZEDER Gábor
Quoted 5 lines · SZEDER Gábor
Quoted 3 lines · Ramkumar Ramachandra
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?
Quoted 7 lines
Quoted 3 lines
The same way we make other things configurable in the prompt: specify the
GIT_PS1_HIDESTATESEPARATORor something similar variable to strip that space from the prompt.Or perhaps better yet: use a variable, e.g.
GIT_PS1_STATESEPARATORor 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?
Re: [PATCH 1/2] git-prompt.sh: strip unnecessary space in prompt string
Quoted 3 lines · Junio C Hamano
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.
PATCH 2/2 git-prompt.zsh: introduce thin ZSH wrapper
In reply to Ramkumar RamachandraTo 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().
--- 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
contrib/completion/git-prompt.sh +40 -33
@@ -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
contrib/completion/git-prompt.zsh new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
1.8.3.rc1.52.g4537cf1
Re: [PATCH 2/2] git-prompt.zsh: introduce thin ZSH wrapper
Quoted 4 lines · Ramkumar Ramachandra
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 aPROMPT_COMMAND, instruct the user to execute__git_ps1insideprecmd().
I think this is two logical changes into one.
Quoted 14 lines
contrib/completion/git-prompt.zsh new file +11
-0@@ -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" } +#
contrib/completion/git-prompt.zsh line 21
Quoted 10 lines
contrib/completion/git-prompt.zsh new file +10
-0+# 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.
Quoted 4 lines
contrib/completion/git-prompt.zsh new file +4
-0+autoload colors +colors + +__git_ps1_colorize_gitstring ()
contrib/completion/git-prompt.zsh line 36
Quoted 10 lines
contrib/completion/git-prompt.zsh new file +10
-0+{ + 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.
Quoted 23 lines
contrib/completion/git-prompt.zsh new file +22
-1+ 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" +} --
Signature · 1 line
Felipe Contreras
PATCH v2 1/1 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.
---
You like this more? I don't mind going either way.
contrib/completion/git-prompt.sh | 67 ++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 20 deletions(-)
contrib/completion/git-prompt.sh +47 -20
@@ -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
Signature · 1 line
1.8.3.rc1.46.gfc3cab0
Re: [PATCH v2] git-prompt.sh: colorize ZSH prompt
Quoted 7 lines · Ramkumar Ramachandra
Add colors suitable for use in the ZSH prompt. Having learnt that the ZSH equivalent of
PROMPT_COMMANDis precmd (), you can now useGIT_PS1_SHOWCOLORHINTSwith ZSH.Signed-off-by Ramkumar Ramachandra <[email protected]>---
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.
Signature · 1 line
Felipe Contreras
Re: [PATCH v2] git-prompt.sh: colorize ZSH prompt
Quoted 3 lines · Felipe Contreras
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)
Re: [PATCH v2] git-prompt.sh: colorize ZSH prompt
Quoted 9 lines · Ramkumar Ramachandra
Quoted 3 lines · Felipe Contreras
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 <[email protected]> Date: Sat May 11 21:55:13 2013 +0530
prompt: split code into __git_ps1_colorize_gitstring()
Will be useful for reorganizations.
contrib/completion/git-prompt.sh +40 -36
@@ -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 <[email protected]> Date: Sat May 11 18:03:39 2013 -0500
prompt: add colorization for zsh
contrib/completion/git-prompt.sh +35 -0
@@ -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)
Signature · 1 line
Felipe Contreras