git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Add colors to the prompt for status indicators

From
Kevin Ballard <kevin@sb.org>
Date
Nov 16, 2010, 10:49 UTC
Message-ID
<5FCB4CFB-65F1-4694-AFB4-36A9612BE4F3@sb.org>
In-Reply-To
<4CE2603D.8080209@drmicha.warpmail.net>
On Nov 16, 2010, at 2:43 AM, Michael J Gruber wrote:
Show 37 quoted lines
> 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
Previous: Michael J Gruber
Message 9 of 9 in “Add colors to the prompt for status indicators”
  1. Sebastien DoucheOct 31, 2010
  2. Kevin BallardOct 31, 2010
  3. Andrew SayersNov 14, 2010
  4. Kevin BallardNov 15, 2010
  5. Kevin BallardNov 15, 2010
  6. Sebastien DoucheNov 16, 2010
  7. Kevin BallardNov 16, 2010
  8. Michael J GruberNov 16, 2010
  9. Kevin BallardNov 16, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.