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

Re: Add colors to the prompt for status indicators

From
ASAndrew Sayers <andrew-git@pileofstuff.org>
Date
Nov 14, 2010, 20:50 UTC
Message-ID
<4CE04B82.1040804@pileofstuff.org>
In-Reply-To
<AANLkTi=ZdR4_reQgxL+xRaFE=SaqBYAWTrEuGEbLGynt@mail.gmail.com>

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
Previous: Kevin BallardNext: Kevin Ballard
Message 3 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.