zsh-users
 help / color / mirror / code / Atom feed
* Colored PS1
@ 2005-09-07  3:25 Meino Christian Cramer
  2005-09-07  7:53 ` Tim Kruse
  0 siblings, 1 reply; 9+ messages in thread
From: Meino Christian Cramer @ 2005-09-07  3:25 UTC (permalink / raw)
  To: zsh-users

Hi,

 from the german version of the zsh-lovers page I got the following
 hint to colorise the prompt of the shell:

     autoload -U colors && colors
     echo "$fg_bold[red]zsh $fg_no_bold[white]is $bg[blue]$fg_bold[green]nice"

 which prints a colored string. So far so nice.

 Now I thought my previous PS1-string

     export PS1="%B%(?..[%?] )%b%n@%U%m%u:%1~>"

 would be nicer if I would change it to:

     export PS1="%B%(?..[%?] )$fg_bold[red]%b$fg_no_bold[black]%n@%U%m%u:%1~>"

 but this does not work for me. The prompt remains the same but
 expansion/expanding on the commandline gets a hickup. My TERM is set
 to "xterm" and normally colored output is no problem.

 What is wrong in my assumption, that first the $fg_bold[red] and
 $fg_no_bold[black] get expanded to their "real" escape sequences and
 then and thing like %b get replaced by their current value and then
 the string is sent to the terminal which finally colors the output
 accordingly? 

 If I do a 

     echo $PS1"

 I get a correctly colored output (the special expanding of for
 example %b is missing then, which is ok).

 Hmmm....?

 Thank you very much for any help in advance !

 Have a nice week!
 Meino


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

* Re: Colored PS1
  2005-09-07  3:25 Colored PS1 Meino Christian Cramer
@ 2005-09-07  7:53 ` Tim Kruse
  2005-09-07 14:57   ` Bart Schaefer
  2005-09-07 17:21   ` Meino Christian Cramer
  0 siblings, 2 replies; 9+ messages in thread
From: Tim Kruse @ 2005-09-07  7:53 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1277 bytes --]

* On 07.09.2005 Meino Christian Cramer wrote:

>      export PS1="%B%(?..[%?] )$fg_bold[red]%b$fg_no_bold[black]%n@%U%m%u:%1~>"
> 
>  but this does not work for me. The prompt remains the same but
>  expansion/expanding on the commandline gets a hickup. My TERM is set
>  to "xterm" and normally colored output is no problem.

 You should enclose your color definitions between '%{' and '%}'

 From the manual page:

,-----[man zshall]
| PROMPT EXPANSION
|     [...]
|     Visual effects
|         [...]
|         %{...%}
|               Include a string as a literal escape sequence.  The  string  within
|               the  braces should not change the cursor position.  Brace pairs can
|               nest.
`-----

 Try something like this

% ps_beg="%B%{$fg_bold[red]%}%(?..[%?] )%b%{$fg_no_bold[black]%}%n@%U%m%u:"
% ps_end="%1~>%{$fg_bold[default]$fg_no_bold[default]%}"
% export PS1="${ps_beg}${ps_end}"

 I don't know if "%fg_bold[default]$fg_no_bold[default]" is
really necessary here ...

 Btw, %b you mentioned is the end of bold output. See also the
manual page (man zshmisc search for PROMPT EXPANSION)

 So long,
tkr

-- 
*** Topic for #redhat:  ReDHaT is the answer to all your problems. It
    could be the start too!

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: Colored PS1
  2005-09-07  7:53 ` Tim Kruse
@ 2005-09-07 14:57   ` Bart Schaefer
  2005-09-07 17:21   ` Meino Christian Cramer
  1 sibling, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2005-09-07 14:57 UTC (permalink / raw)
  To: zsh-users

On Sep 7,  9:53am, Tim Kruse wrote:
}
}  I don't know if "%fg_bold[default]$fg_no_bold[default]" is
} really necessary here ...

Just $reset_color would be sufficient.


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

* Re: Colored PS1
  2005-09-07  7:53 ` Tim Kruse
  2005-09-07 14:57   ` Bart Schaefer
@ 2005-09-07 17:21   ` Meino Christian Cramer
  2005-09-08  2:39     ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Meino Christian Cramer @ 2005-09-07 17:21 UTC (permalink / raw)
  To: irrlicht2c; +Cc: zsh-users

From: Tim Kruse <irrlicht2c@gmx.net>
Subject: Re: Colored PS1
Date: Wed, 7 Sep 2005 09:53:01 +0200

> * On 07.09.2005 Meino Christian Cramer wrote:
> 
> >      export PS1="%B%(?..[%?] )$fg_bold[red]%b$fg_no_bold[black]%n@%U%m%u:%1~>"
> > 
> >  but this does not work for me. The prompt remains the same but
> >  expansion/expanding on the commandline gets a hickup. My TERM is set
> >  to "xterm" and normally colored output is no problem.
> 
>  You should enclose your color definitions between '%{' and '%}'
> 
>  From the manual page:
> 
> ,-----[man zshall]
> | PROMPT EXPANSION
> |     [...]
> |     Visual effects
> |         [...]
> |         %{...%}
> |               Include a string as a literal escape sequence.  The  string  within
> |               the  braces should not change the cursor position.  Brace pairs can
> |               nest.
> `-----
> 
>  Try something like this
> 
> % ps_beg="%B%{$fg_bold[red]%}%(?..[%?] )%b%{$fg_no_bold[black]%}%n@%U%m%u:"
> % ps_end="%1~>%{$fg_bold[default]$fg_no_bold[default]%}"
> % export PS1="${ps_beg}${ps_end}"
> 
>  I don't know if "%fg_bold[default]$fg_no_bold[default]" is
> really necessary here ...
> 
>  Btw, %b you mentioned is the end of bold output. See also the
> manual page (man zshmisc search for PROMPT EXPANSION)
> 
>  So long,
> tkr
> 
> -- 
> *** Topic for #redhat:  ReDHaT is the answer to all your problems. It
>     could be the start too!

Hi Tim, hi Bart !

 Thank you very much for your prompt reply :)

 Let's have a look, whether I understood things right:

 First of all my all simple PS1:

	   export PS1=%M:%2d
 
 This should be the base.

 Now: I want my machine being displayed in bold black (technical
 gadget have to be in black, isn't it? ;)

 Now I would write:

	   export PS1=$fb_bold[black]%M$fb_no_bold[black]:%2d
 
 But this would not work (that's why I wrote my mail...). As I
 understand your mail, I had to enclose the "literally escape
 sequence" (whatever this is...I am no native English speaker and if
 translated to german this makes really no sense - as with so many 
 technospeak expressions...

 And how should I figure out, whether the cursor position would
 have been changed...???


 So I would guess this cames more closer to what is right:

	   export PS1=%{$fg_bold[black]%}%M%{$fg_no_bold[black]%}:%2d

 but this one produces 

       }solfire:/home/mccramer

 brace levels and nesting seems right for me and my editor--but there
 is one '}' left behind. No colors, no bold. "autoload -U colors &&
 colors" was executed before.

 Enclosing the whole thing in ${...} gives me no prompt at all.

 And what is the reason for the ps_beg/ps_end trick? Why no to write
 everyting in one big term ?

 Oh, damn, I fear I am too stupid... ;O)

 Have a colorfull evening! 
 Meino (slightly %{confused%})


 




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

* Re: Colored PS1
  2005-09-07 17:21   ` Meino Christian Cramer
@ 2005-09-08  2:39     ` Bart Schaefer
  2005-09-08  3:31       ` Meino Christian Cramer
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2005-09-08  2:39 UTC (permalink / raw)
  To: Meino Christian Cramer; +Cc: zsh-users

On Sep 7,  7:21pm, Meino Christian Cramer wrote:
}
}  Now I would write:
} 
} 	   export PS1=$fb_bold[black]%M$fb_no_bold[black]:%2d
}  
}  But this would not work (that's why I wrote my mail...).  As I
}  understand your mail, I had to enclose the "literally escape
}  sequence" (whatever this is...I am no native English speaker and if
}  translated to german this makes really no sense - as with so many 
}  technospeak expressions...

"literal" (not "literally") in that context means "unchanged; exactly
the way it appears."

"escape sequence" in the context of terminals means "a series of ASCII
characters, often beginning with the ESC (\033, \x1B) character."  It
is common for terminals and terminal emulators to recognize such a
series of characters in the output and alter the terminal's behavior.

The "colors" (aka "colours") function sets up an associative array
with values that are various such terminal control strings (which
would have been a better thing to call them than "escape sequences",
but historically they were named the latter because they allow the
terminal to "escape" from it's routine job of displaying and instead
do something special).  So when you write $fg_bold[black], you are
asking zsh to insert the control string that changes the terminal's
foreground to bold and black.

The trouble is that "escape sequnce" in the context of a prompt means
one of the pairs of characters that begins with a percent (%) sign.
Again this is from the concept of a special combination of characters
causing a change (an escape from) the usual interpretation of such
characters.

Thus, one uses "prompt escape sequences" to surround "terminal escape
sequences," and this clash of terminology is often enough to confuse
native English speakers; so you shouldn't feel too bad.
 
}  And how should I figure out, whether the cursor position would
}  have been changed...???

In the worst case, you go look at the documentation for an ANSI standard
terminal, find the control string that's being used, and determine from
the definition whether any cursor movement is involved.

Usually, though, you just use your common sense.  Does the cursor move
when the color changes from white to red, or plain to bold?  No, it does
not, so the string value of $fg_bold[black] does not move the cursor.

}  So I would guess this cames more closer to what is right:
} 
} 	   export PS1=%{$fg_bold[black]%}%M%{$fg_no_bold[black]%}:%2d
} 
}  but this one produces 
} 
}        }solfire:/home/mccramer

Are you quite sure that you have all the % signs in the right places?
My first suspicion would be that you wrote "PS1=%{$fg_bold[black]}..."
(note missing % before the } there).

This can also be affected by various setopts such as SH_WORD_SPLIT and
PROMPT_SUBST and KSH_ARRAYS.  For example, $fg_bold[black] means
something entirely different when KSH_ARRAYS is set.

I strongly suggest that you unsetopt PROMPT_SUBST, and protect against
SH_WORD_SPLIT by placing double quotes around the assignment:

    export PS1="%{$fg_bold[black]%}%M%{$fg_no_bold[black]%}:%2d "

}  Enclosing the whole thing in ${...} gives me no prompt at all.

I'm surprised it didn't give a syntax error.  One almost never wants
${...} around "the whole thing" unless one understands exactly what
one is doing.
 
}  And what is the reason for the ps_beg/ps_end trick? Why no to write
}  everyting in one big term ?

Easier to read it, and to edit it later.  That's all.


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

* Re: Colored PS1
  2005-09-08  2:39     ` Bart Schaefer
@ 2005-09-08  3:31       ` Meino Christian Cramer
  2005-09-08 16:05         ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Meino Christian Cramer @ 2005-09-08  3:31 UTC (permalink / raw)
  To: schaefer; +Cc: zsh-users

From: Bart Schaefer <schaefer@brasslantern.com>
Subject: Re: Colored PS1
Date: Thu, 08 Sep 2005 02:39:21 +0000

Hi Bart !

 First of all and most important:
 ***$fg_very_very_bold[RED]THANK YOU VERY VERY MUCH!$fg_very_very_bold[BLACK]***
 for your help and patience with my English confusions !!!

 This helps me a lot to understand things !

> On Sep 7,  7:21pm, Meino Christian Cramer wrote:
> }
> }  Now I would write:
> } 
> } 	   export PS1=$fb_bold[black]%M$fb_no_bold[black]:%2d
> }  
> }  But this would not work (that's why I wrote my mail...).  As I
> }  understand your mail, I had to enclose the "literally escape
> }  sequence" (whatever this is...I am no native English speaker and if
> }  translated to german this makes really no sense - as with so many 
> }  technospeak expressions...
> 
> "literal" (not "literally") in that context means "unchanged; exactly
> the way it appears."

  ...do have to say more about my English? I think...no...this is
  selfdocumenting! 

> "escape sequence" in the context of terminals means "a series of ASCII
> characters, often beginning with the ESC (\033, \x1B) character."  It
> is common for terminals and terminal emulators to recognize such a
> series of characters in the output and alter the terminal's behavior.

  ...oh I see...something like "commands"! :)

  ( "assembler for terminals" -- either it works or you gut stuck ;)

> The "colors" (aka "colours") function sets up an associative array
> with values that are various such terminal control strings (which
> would have been a better thing to call them than "escape sequences",
> but historically they were named the latter because they allow the
> terminal to "escape" from it's routine job of displaying and instead
> do something special).  So when you write $fg_bold[black], you are
> asking zsh to insert the control string that changes the terminal's
> foreground to bold and black.
>
> The trouble is that "escape sequnce" in the context of a prompt means
> one of the pairs of characters that begins with a percent (%) sign.
> Again this is from the concept of a special combination of characters
> causing a change (an escape from) the usual interpretation of such
> characters.
>
> ...do I understand this cxorrectly? In other words:
  The trouble is, that a escape seuence can be misinterpreted as a
> special sign of the prompt expanding itsself ?
> 
> Thus, one uses "prompt escape sequences" to surround "terminal escape
> sequences," and this clash of terminology is often enough to confuse
> native English speakers; so you shouldn't feel too bad.
>  
> }  And how should I figure out, whether the cursor position would
> }  have been changed...???
> 
> In the worst case, you go look at the documentation for an ANSI standard
> terminal, find the control string that's being used, and determine from
> the definition whether any cursor movement is involved.

  URKS! I am not lazy in reading documentations, I am lesser good in
  understanding (english) documentations...but the effort must be in a
  reasonable relation to what I want to achieve.

> Usually, though, you just use your common sense.  Does the cursor move
> when the color changes from white to red, or plain to bold?  No, it does
> not, so the string value of $fg_bold[black] does not move the cursor.
>
> }  So I would guess this cames more closer to what is right:
> } 
> } 	   export PS1=%{$fg_bold[black]%}%M%{$fg_no_bold[black]%}:%2d
> } 
> }  but this one produces 
> } 
> }        }solfire:/home/mccramer
> 
> Are you quite sure that you have all the % signs in the right places?
> My first suspicion would be that you wrote "PS1=%{$fg_bold[black]}..."
> (note missing % before the } there).

  I dont think, that braces got messed up. I checked that with
  MicroEmacs's (the editor I use to use...) "goto-matching-fence" and
  kept an eye on the %'s....

> This can also be affected by various setopts such as SH_WORD_SPLIT and
> PROMPT_SUBST and KSH_ARRAYS.  For example, $fg_bold[black] means
> something entirely different when KSH_ARRAYS is set.

  I didn't set KSH_ARRAYS and SH_WORD_SPLIT. But I am not sure whether
  I set "various setopts" ;)
 
> I strongly suggest that you unsetopt PROMPT_SUBST, and protect against
> SH_WORD_SPLIT by placing double quotes around the assignment:
>
>     export PS1="%{$fg_bold[black]%}%M%{$fg_no_bold[black]%}:%2d "
> 
> }  Enclosing the whole thing in ${...} gives me no prompt at all.
> 
> I'm surprised it didn't give a syntax error.  One almost never wants
> ${...} around "the whole thing" unless one understands exactly what
> one is doing.

  Yes, you are right...I didnt want it ;))))
 
> }  And what is the reason for the ps_beg/ps_end trick? Why no to write
> }  everyting in one big term ?
> 
> Easier to read it, and to edit it later.  That's all.
> 

Finally this works for me:

autoload -U colors && colors
export prompt=%{$fg_no_bold[red]%}%M%{$fg_no_bold[black]%}:%{$fg_no_bold[blue]%}
%2d%{$fg_no_bold[black]%}\>
export PS1=$prompt

Setting PS1 directly without the addtional step using $prompt it does
not work.

No I am happy haveing a colored prompt -- only wihing that sometimes
zshall would be more understandable than totally complete. Only me two
cent....

Thank you very much for all your help and patience, Bart ! :O)

Have a nice day!
Meino


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

* Re: Colored PS1
  2005-09-08  3:31       ` Meino Christian Cramer
@ 2005-09-08 16:05         ` Bart Schaefer
  2005-09-08 16:59           ` Meino Christian Cramer
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2005-09-08 16:05 UTC (permalink / raw)
  To: Meino Christian Cramer, zsh-users

On Sep 8,  5:31am, Meino Christian Cramer wrote:
}
} > "escape sequence" in the context of terminals means "a series of ASCII
} > characters, often beginning with the ESC (\033, \x1B) character."  It
} > is common for terminals and terminal emulators to recognize such a
} > series of characters in the output and alter the terminal's behavior.
} 
}   ...oh I see...something like "commands"! :)

Yes, except that the "commands" are embedded in the same stream as the
ordinary non-command data.  It's more like a very crude form of markup.
In HTML, you'd write
    I am not bold but <bold>I am brave!</bold>
and in "terminal markup" you'd write
    I am not bold but ^[[01;39mI am brave!^[[00m
(where ^[ is the ESC character).  In neither case do you see the markup
when the text is displayed (hence, no cursor movement).

} > The trouble is that "escape sequnce" in the context of a prompt means
} > one of the pairs of characters that begins with a percent (%) sign.
} > Again this is from the concept of a special combination of characters
} > causing a change (an escape from) the usual interpretation of such
} > characters.
} >
} ...do I understand this cxorrectly? In other words:
}   The trouble is, that a escape seuence can be misinterpreted as a
} special sign of the prompt expanding itsself ?

No, nothing like that.  I'm only talking about humans misunderstanding
the confusing terminology, not zsh misinterpreting the strings.  Look
at what I was just saying about markup ... prompts have "markup" too:

    I am not bold but %BI am brave!%b

Prompt markup can't change colors, though, so sometimes you have to mix
terminal markup and prompt markup.  The prompt markup %{ and %} tell zsh
that what's in between is some other kind of markup, so that it should
not count it as something that occupies space on the display.

} > In the worst case, you go look at the documentation for an ANSI standard
} > terminal
} > the definition whether any cursor movement is involved.
} 
}   URKS!

Yeah, but that's why I said "in the worst case."

} Setting PS1 directly without the addtional step using $prompt it does
} not work.

That makes no sense at all.  Something is happening that you don't know
how to tell us, and I don't know what to ask to learn what it is.

However ...

} I am happy haveing a colored prompt

... if you're happy, we don't need to pursue it any further right now.

} ... only wihing that sometimes
} zshall would be more understandable than totally complete.

Wishing?  Buy Oliver and Peter's book!


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

* Re: Colored PS1
  2005-09-08 16:05         ` Bart Schaefer
@ 2005-09-08 16:59           ` Meino Christian Cramer
  2005-09-09 14:32             ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Meino Christian Cramer @ 2005-09-08 16:59 UTC (permalink / raw)
  To: schaefer; +Cc: zsh-users

From: Bart Schaefer <schaefer@brasslantern.com>
Subject: Re: Colored PS1
Date: Thu, 08 Sep 2005 16:05:13 +0000

Hi Bart,

 thanks again for your explanations !

> 
> Wishing?  Buy Oliver and Peter's book!
> 

Oh, yeah...I didn't know of it. It is based on what zsh-version ?


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

* Re: Colored PS1
  2005-09-08 16:59           ` Meino Christian Cramer
@ 2005-09-09 14:32             ` Bart Schaefer
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2005-09-09 14:32 UTC (permalink / raw)
  To: Meino Christian Cramer; +Cc: zsh-users

On Sep 8,  6:59pm, Meino Christian Cramer wrote:
}
} > Wishing?  Buy Oliver and Peter's book!
} 
} Oh, yeah...I didn't know of it. It is based on what zsh-version ?

I was hoping one of the authors would jump in here ... but ...

It assumes zsh 4.0.x as a baseline, but covers a number of 4.2 features
and makes mention in most cases where things differ in older versions.


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

end of thread, other threads:[~2005-09-09 14:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-07  3:25 Colored PS1 Meino Christian Cramer
2005-09-07  7:53 ` Tim Kruse
2005-09-07 14:57   ` Bart Schaefer
2005-09-07 17:21   ` Meino Christian Cramer
2005-09-08  2:39     ` Bart Schaefer
2005-09-08  3:31       ` Meino Christian Cramer
2005-09-08 16:05         ` Bart Schaefer
2005-09-08 16:59           ` Meino Christian Cramer
2005-09-09 14:32             ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).