Subject: Re: Add colors to the prompt for status indicators

From: Andrew Sayers <[email-masked]>
Date: Sun, 14 Nov 2010 20:50:10 +0000

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