Re: PS1 multiline with colors

2013-07-05 Thread Greg Wooledge
On Fri, Jul 05, 2013 at 12:50:53AM +0800, Chris Down wrote:
> > PS1='\h Hello everybody\n\e[1;35m\]Hi\e[0m\]>'

> You need to properly indicate that the control codes are zero-width
> (by using \[ and \]). Without them, this is expected behaviour.
> 
> Better, don't hardcode the escape codes for colours -- it is a naive
> assumption to believe that all terminals will do what you expect. Use
> `tput' instead.

There are two ways of doing this (nobody can agree which way is better).

The first way is to put each tput result in a variable, and use the
variables inside PS1 so that they are expanded when PS1 is evaluated:

red=$(tput setaf 1)
normal=$(tput sgr0)
PS1='\h Hello everybody\n\[$red\]Hi\[$normal\]> '
export red normal PS1

The second way is to expand the variables at the time PS1 is created:

red=$(tput setaf 1)
normal=$(tput sgr0)
PS1='\h Hello everybody\n\['"$red"'\]Hi\['"$normal"'\]> '
export PS1

I prefer the first one myself, but there is no consensus.  I find the
first one more readable, both in the code where you define the prompt,
and in the result of echo "$PS1" should you ever want to SEE the prompt
and understand what it is doing.  Others prefer the second because they
don't want to lug around all the $red $normal $green $blue etc. variables.

See also http://mywiki.wooledge.org/BashFAQ/053



Re: PS1 multiline with colors

2013-07-05 Thread Greg Wooledge
On Fri, Jul 05, 2013 at 12:21:13PM +, BASTIDON, Stéphane wrote:
> Now my case is a little more complex:
> I want my prompt changes colors according to $PWD Then I write something like 
> that:

This is the kind of thing where I suggest using PROMPT_COMMAND.

normal=$(tput sgr0) red=$(tput setaf 1) green=$(tput setaf 2) ...
PROMPT_COMMAND='
  case $PWD in
/usr/local/*) color=$red ;;
/var/*)   color=$green ;;
...
  esac
'
PS1='\u@\h:\[$color\]\w\[$normal\]\$ '
export normal red green ... PROMPT_COMMAND PS1

The PROMPT_COMMAND will be evaluated before PS1.  You can use it to set
variables that PS1 will use, all without the horrible speed penalty of
a fork/exec.



Re: PS1 multiline with colors

2013-07-05 Thread Dan Douglas
This function (colorSet) takes one or more associative array names and
can populate it with a few predefined color palates. Written for
Bash/ksh93/zsh.
http://wiki.bash-hackers.org/snipplets/add_color_to_your_scripts

--
Dan Douglas



RE: PS1 multiline with colors

2013-07-05 Thread BASTIDON , Stéphane
I'll try that asap ...
Thanks


-Message d'origine-
De : Greg Wooledge [mailto:wool...@eeg.ccf.org] 
Envoyé : vendredi 5 juillet 2013 14:31
À : BASTIDON, Stéphane
Cc : bug-bash@gnu.org
Objet : Re: PS1 multiline with colors

On Fri, Jul 05, 2013 at 12:21:13PM +, BASTIDON, Stéphane wrote:
> Now my case is a little more complex:
> I want my prompt changes colors according to $PWD Then I write something like 
> that:

This is the kind of thing where I suggest using PROMPT_COMMAND.

normal=$(tput sgr0) red=$(tput setaf 1) green=$(tput setaf 2) ...
PROMPT_COMMAND='
  case $PWD in
/usr/local/*) color=$red ;;
/var/*)   color=$green ;;
...
  esac
'
PS1='\u@\h:\[$color\]\w\[$normal\]\$ '
export normal red green ... PROMPT_COMMAND PS1

The PROMPT_COMMAND will be evaluated before PS1.  You can use it to set 
variables that PS1 will use, all without the horrible speed penalty of a 
fork/exec.