All of lore.kernel.org
 help / color / mirror / Atom feed
* Add colors to the prompt for status indicators
@ 2010-10-31  4:14 Sebastien Douche
  2010-10-31  6:26 ` Kevin Ballard
  2010-11-14 20:50 ` Andrew Sayers
  0 siblings, 2 replies; 9+ messages in thread
From: Sebastien Douche @ 2010-10-31  4:14 UTC (permalink / raw)
  To: git

Hi,
the prompt (git-completion.bash) bundled with git is the most
"advanced" I found on the web. But I would add colors for "status".
Example show: * (unstaged) in blue, + (staged in red), $ (stashed) in
green, % (untracked) in bold blue and < > <> (upstream indicator) in
red.

How make this?

Cheers

-- 
Sebastien Douche <sdouche@gmail.com>
Twitter: http://bit.ly/afkrK (agile, lean, python, git, open source)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Add colors to the prompt for status indicators
  2010-10-31  4:14 Add colors to the prompt for status indicators Sebastien Douche
@ 2010-10-31  6:26 ` Kevin Ballard
  2010-11-14 20:50 ` Andrew Sayers
  1 sibling, 0 replies; 9+ messages in thread
From: Kevin Ballard @ 2010-10-31  6:26 UTC (permalink / raw)
  To: Sebastien Douche; +Cc: git

You could write a shell function that takes a string as an argument and inserts the appropriate color codes based on string substitution, and then just pass the results of __git_ps1 to that function.

Something like the following might work:

function __my_git_colorize_ps1 () {
    echo "$1" | while read -N 1 char; do
        case "$char" in
            \*) echo -n $'\e[34m*\e[m';;
            +) echo -n $'\e[31m+\e[m';;
            $) echo -n $'\e[32m$\e[m';;
            %) echo -n $'\e[1;34m%\e[m';;
            \<|\>) echo -n $'\e[31m'"$char"$'\e[m';;
            *) echo -n "$char";;
        esac
    done
}

-Kevin Ballard

On Oct 30, 2010, at 9:14 PM, Sebastien Douche wrote:

> Hi,
> the prompt (git-completion.bash) bundled with git is the most
> "advanced" I found on the web. But I would add colors for "status".
> Example show: * (unstaged) in blue, + (staged in red), $ (stashed) in
> green, % (untracked) in bold blue and < > <> (upstream indicator) in
> red.
> 
> How make this?
> 
> Cheers
> 
> -- 
> Sebastien Douche <sdouche@gmail.com>
> Twitter: http://bit.ly/afkrK (agile, lean, python, git, open source)
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Add colors to the prompt for status indicators
  2010-10-31  4:14 Add colors to the prompt for status indicators Sebastien Douche
  2010-10-31  6:26 ` Kevin Ballard
@ 2010-11-14 20:50 ` Andrew Sayers
  2010-11-15 22:52   ` Kevin Ballard
  1 sibling, 1 reply; 9+ messages in thread
From: Andrew Sayers @ 2010-11-14 20:50 UTC (permalink / raw)
  To: Sebastien Douche; +Cc: git

Sorry for the delayed response to this one - I'm afraid it's the usual
work excuse :)

Non-printing characters need to be surrounded by \[ and \] for bash to
calculate line lengths correctly.  So far as I can tell, this has to be
in PS1 itself - bash doesn't recognise it if it's included in a script.

To see the problem, do this:

OLD_PS1="$PS1"
PS1="\033[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0m>"

Then hold down any key - you should see some weird line-wrapping
behaviour a little way before the edge of your terminal.  To get your
old terminal back:

PS1="$OLD_PS1"

To see what happens when the "\[" characters are embedded in a script:

foo() {
 echo -e "\[\033[;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0m\]"
}
PS1='$(foo)>'

The "\[" and "\]" are represented literally in your terminal, and the
line-wrapping still occurs.

It's possible to work around this for a whole script:

foo() {
 echo -e "\033[;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0m"
}
PS1='\[$(foo)'\]>'

As a fan of colourful prompts, I'd be very happy if you found a way
around this for parts of a script.  But as a fan of fast prompts, I'd
prefer not to call __git_ps1 more than once :)

	- Andrew

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Add colors to the prompt for status indicators
  2010-11-14 20:50 ` Andrew Sayers
@ 2010-11-15 22:52   ` Kevin Ballard
  2010-11-15 23:14     ` Kevin Ballard
  0 siblings, 1 reply; 9+ messages in thread
From: Kevin Ballard @ 2010-11-15 22:52 UTC (permalink / raw)
  To: Andrew Sayers; +Cc: Sebastien Douche, git

On Nov 14, 2010, at 12:50 PM, Andrew Sayers wrote:

> It's possible to work around this for a whole script:
> 
> foo() {
> echo -e "\033[;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0m"
> }
> PS1='\[$(foo)'\]>'

If $(foo) were to emit some actual printable characters, won't this make
bash ignore them when calculating line lengths?

> As a fan of colourful prompts, I'd be very happy if you found a way
> around this for parts of a script.  But as a fan of fast prompts, I'd
> prefer not to call __git_ps1 more than once :)

I don't think there is any way around this, besides patching bash to be
intelligent and determine which PS1 characters are printable itself without
relying on \[ and \].

-Kevin Ballard

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Add colors to the prompt for status indicators
  2010-11-15 22:52   ` Kevin Ballard
@ 2010-11-15 23:14     ` Kevin Ballard
  2010-11-16  8:11       ` Sebastien Douche
  0 siblings, 1 reply; 9+ messages in thread
From: Kevin Ballard @ 2010-11-15 23:14 UTC (permalink / raw)
  To: Andrew Sayers; +Cc: Sebastien Douche, git list

On Nov 15, 2010, at 2:52 PM, Kevin Ballard wrote:

>> As a fan of colourful prompts, I'd be very happy if you found a way
>> around this for parts of a script.  But as a fan of fast prompts, I'd
>> prefer not to call __git_ps1 more than once :)
> 
> I don't think there is any way around this, besides patching bash to be
> intelligent and determine which PS1 characters are printable itself without
> relying on \[ and \].

I take it back. We can use PROMPT_COMMAND for this, to set up variables
containing what you want.

Something like the following should work:

function __populate_git_ps1_vars () {
    # this relies on a bashism, so make sure you're actually using bash.
    # specifically it relies on <<<"word".
    local status
    local prompt="$(__git_ps1 "%s")"
    # empty out the vars
    __git_ps1_branch=""
    __git_ps1_staged=""
    __git_ps1_unstaged=""
    __git_ps1_stash=""
    __git_ps1_untracked=""
    __git_ps1_upstream=""
    __git_ps1_left=""
    __git_ps1_right=""
    if test -z "$prompt"; then
        # return now
        return
    fi
    __git_ps1_left=" ("
    __git_ps1_right=")"
    __git_ps1_branch="$(git rev-parse --symbolic-full-name --abbrev-ref=loose HEAD)"
    status="${prompt#$__git_ps1_branch}"
    while read -N 1 char; do
        case "$char" in
            \*) __git_ps1_unstaged="$char" ;;
            +) __git_ps1_staged="$char" ;;
            $) __git_ps1_stash="$char" ;;
            %) __git_ps1_untracked="$char" ;;
            \<|\>) __git_ps1_upstream="${__git_ps1_upstream}$char" ;;
        esac
    done <<<"$status"
}

PROMPT_COMMAND=__populate_git_ps1_vars

PS1='\w${__git_ps1_left}${__git_ps1_branch}\[\e[31m\]${__git_ps1_staged}\[\e[34m\]${__git_ps1_unstaged}\[\e[32m\]${__git_ps1_stash}\[\e[1;34m\]${__git_ps1_untracked}\[\e[31m\]${__git_ps1_upstream}\[\e[m\]${__git_ps1_right}> '

-Kevin Ballard

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Add colors to the prompt for status indicators
  2010-11-15 23:14     ` Kevin Ballard
@ 2010-11-16  8:11       ` Sebastien Douche
  2010-11-16  9:07         ` Kevin Ballard
  0 siblings, 1 reply; 9+ messages in thread
From: Sebastien Douche @ 2010-11-16  8:11 UTC (permalink / raw)
  To: Kevin Ballard; +Cc: Andrew Sayers, git list

On Tue, Nov 16, 2010 at 00:14, Kevin Ballard <kevin@sb.org> wrote:
> I take it back. We can use PROMPT_COMMAND for this, to set up variables
> containing what you want.
>
> Something like the following should work:

Hi Kevin,
good job! I looked the git prompt and I'm a bit disappointed with the
combination, ps1 can show many items:

#
%
+
*
$
REBASE-i|
REBASE-m|
MERGING|
BISECTING|
< > <> (or u+count u-count u+countu-count)

Each item is "optional", so you can have REBASE-i| with * and +, or
only *. It seems to complicated to wrap all items (I think of the
verbose mode for upstream branches). Maybe coding a lua (or Python)
prompt with a config file.


-- 
Sebastien Douche <sdouche@gmail.com>
Twitter: @sdouche (agile, lean, python, git, open source)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Add colors to the prompt for status indicators
  2010-11-16  8:11       ` Sebastien Douche
@ 2010-11-16  9:07         ` Kevin Ballard
  2010-11-16 10:43           ` Michael J Gruber
  0 siblings, 1 reply; 9+ messages in thread
From: Kevin Ballard @ 2010-11-16  9:07 UTC (permalink / raw)
  To: Sebastien Douche; +Cc: Andrew Sayers, git list

On Nov 16, 2010, at 12:11 AM, Sebastien Douche wrote:

> On Tue, Nov 16, 2010 at 00:14, Kevin Ballard <kevin@sb.org> wrote:
>> I take it back. We can use PROMPT_COMMAND for this, to set up variables
>> containing what you want.
>> 
>> Something like the following should work:
> 
> Hi Kevin,
> good job! I looked the git prompt and I'm a bit disappointed with the
> combination, ps1 can show many items:
> 
> #
> %
> +
> *
> $
> REBASE-i|
> REBASE-m|
> MERGING|
> BISECTING|
> < > <> (or u+count u-count u+countu-count)
> 
> Each item is "optional", so you can have REBASE-i| with * and +, or
> only *. It seems to complicated to wrap all items (I think of the
> verbose mode for upstream branches). Maybe coding a lua (or Python)
> prompt with a config file.

In the end you need to stuff everything that needs a distinct color into its
own shell variable. You could just extend the shell function that's doing that
right now, or you could write a script in another language that emits a
shell-quoted set of variables suitable for evaling by the shell.

-Kevin Ballard

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Add colors to the prompt for status indicators
  2010-11-16  9:07         ` Kevin Ballard
@ 2010-11-16 10:43           ` Michael J Gruber
  2010-11-16 10:49             ` Kevin Ballard
  0 siblings, 1 reply; 9+ messages in thread
From: Michael J Gruber @ 2010-11-16 10:43 UTC (permalink / raw)
  To: Kevin Ballard; +Cc: Sebastien Douche, Andrew Sayers, git list

Kevin Ballard venit, vidit, dixit 16.11.2010 10:07:
> On Nov 16, 2010, at 12:11 AM, Sebastien Douche wrote:
> 
>> On Tue, Nov 16, 2010 at 00:14, Kevin Ballard <kevin@sb.org> wrote:
>>> I take it back. We can use PROMPT_COMMAND for this, to set up variables
>>> containing what you want.
>>>
>>> Something like the following should work:
>>
>> Hi Kevin,
>> good job! I looked the git prompt and I'm a bit disappointed with the
>> combination, ps1 can show many items:
>>
>> #
>> %
>> +
>> *
>> $
>> REBASE-i|
>> REBASE-m|
>> MERGING|
>> BISECTING|
>> < > <> (or u+count u-count u+countu-count)
>>
>> Each item is "optional", so you can have REBASE-i| with * and +, or
>> only *. It seems to complicated to wrap all items (I think of the
>> verbose mode for upstream branches). Maybe coding a lua (or Python)
>> prompt with a config file.
> 
> In the end you need to stuff everything that needs a distinct color into its
> own shell variable. You could just extend the shell function that's doing that
> right now, or you could write a script in another language that emits a
> shell-quoted set of variables suitable for evaling by the shell.

Also, you should get away from parsing individual characters in
__git_ps1. Characters like + and - are valid in branch names. Coloring
them within the name is not quite intended.

I guess the only way is to produce colors within __git_ps1 (based on an
option). Also, colors would allow to use the same status letters as "git
status -s".

Michael

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Add colors to the prompt for status indicators
  2010-11-16 10:43           ` Michael J Gruber
@ 2010-11-16 10:49             ` Kevin Ballard
  0 siblings, 0 replies; 9+ messages in thread
From: Kevin Ballard @ 2010-11-16 10:49 UTC (permalink / raw)
  To: Michael J Gruber; +Cc: Sebastien Douche, Andrew Sayers, git list

On Nov 16, 2010, at 2:43 AM, Michael J Gruber wrote:

> Kevin Ballard venit, vidit, dixit 16.11.2010 10:07:
>> On Nov 16, 2010, at 12:11 AM, Sebastien Douche wrote:
>> 
>>> On Tue, Nov 16, 2010 at 00:14, Kevin Ballard <kevin@sb.org> wrote:
>>>> I take it back. We can use PROMPT_COMMAND for this, to set up variables
>>>> containing what you want.
>>>> 
>>>> Something like the following should work:
>>> 
>>> Hi Kevin,
>>> good job! I looked the git prompt and I'm a bit disappointed with the
>>> combination, ps1 can show many items:
>>> 
>>> #
>>> %
>>> +
>>> *
>>> $
>>> REBASE-i|
>>> REBASE-m|
>>> MERGING|
>>> BISECTING|
>>> < > <> (or u+count u-count u+countu-count)
>>> 
>>> Each item is "optional", so you can have REBASE-i| with * and +, or
>>> only *. It seems to complicated to wrap all items (I think of the
>>> verbose mode for upstream branches). Maybe coding a lua (or Python)
>>> prompt with a config file.
>> 
>> In the end you need to stuff everything that needs a distinct color into its
>> own shell variable. You could just extend the shell function that's doing that
>> right now, or you could write a script in another language that emits a
>> shell-quoted set of variables suitable for evaling by the shell.
> 
> Also, you should get away from parsing individual characters in
> __git_ps1. Characters like + and - are valid in branch names. Coloring
> them within the name is not quite intended.

That's actually handled with the PROMPT_COMMAND version. It uses

  git rev-parse --symbolic-full-name --abbrev-ref=loose HEAD

to get a copy of the branch name, and then removes that from the __git_ps1 output.
Everything that's left is part of the status.

> I guess the only way is to produce colors within __git_ps1 (based on an
> option). Also, colors would allow to use the same status letters as "git
> status -s".

That's how my original version worked (though it had the flaw you mentioned above).
The PROMPT_COMMAND version was motivated by the fact that the color escapes need to
be embedded directly in PS1 so \[ and \] can be used to instruct bash to treat them
as non-printable characters. If the color codes are emitted by __git_ps1 directly, then
bash will incorrectly treat them as printable characters and wrap at the wrong spot.

What would be nice is a mode for __git_ps1 that emits a string suitable for eval by
bash that gives every status indicator its own variable. This could be enabled by
using something like $(__git_ps1 --shell-quote " (%s)"). In this case, __git_ps1 would
break the format string into two parts, by splitting around the %s, and put those in
the left/right guard variables.

-Kevin Ballard

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-11-16 10:49 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-31  4:14 Add colors to the prompt for status indicators Sebastien Douche
2010-10-31  6:26 ` Kevin Ballard
2010-11-14 20:50 ` Andrew Sayers
2010-11-15 22:52   ` Kevin Ballard
2010-11-15 23:14     ` Kevin Ballard
2010-11-16  8:11       ` Sebastien Douche
2010-11-16  9:07         ` Kevin Ballard
2010-11-16 10:43           ` Michael J Gruber
2010-11-16 10:49             ` Kevin Ballard

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.