zsh-users
 help / color / mirror / code / Atom feed
* Conditional newline in prompt
@ 2010-12-01 20:42 Christoph Wurm
  2010-12-01 20:58 ` Mikael Magnusson
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Christoph Wurm @ 2010-12-01 20:42 UTC (permalink / raw)
  To: zsh-users

Hello,

I would like to display the exit code of a command if it is not 0 on a 
line of its own, followed by the prompt on the next line.

Is this possible?

I was able to come up with the following:

PROMPT="%0(?..%?)
%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "

However, this prints a newline after every command. Is there some 
equivalent of '\n' that can be used inside the conditional substring? Or 
some other way to do this?

Kind regards
Christoph


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

* Re: Conditional newline in prompt
  2010-12-01 20:42 Conditional newline in prompt Christoph Wurm
@ 2010-12-01 20:58 ` Mikael Magnusson
  2010-12-01 21:15   ` Christoph Wurm
  2010-12-01 21:08 ` Benjamin R. Haskell
  2010-12-02 11:07 ` Julien Jehannet
  2 siblings, 1 reply; 7+ messages in thread
From: Mikael Magnusson @ 2010-12-01 20:58 UTC (permalink / raw)
  To: Christoph Wurm; +Cc: zsh-users

On 1 December 2010 21:42, Christoph Wurm <christoph@cwurm.de> wrote:
> Hello,
>
> I would like to display the exit code of a command if it is not 0 on a line
> of its own, followed by the prompt on the next line.
>
> Is this possible?
>
> I was able to come up with the following:
>
> PROMPT="%0(?..%?)
> %{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>
> However, this prints a newline after every command. Is there some equivalent
> of '\n' that can be used inside the conditional substring? Or some other way
> to do this?

Yes, a newline works fine.
% PS1='%0(?..%?
)%~> '
~> false
1
~> true
~>

-- 
Mikael Magnusson


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

* Re: Conditional newline in prompt
  2010-12-01 20:42 Conditional newline in prompt Christoph Wurm
  2010-12-01 20:58 ` Mikael Magnusson
@ 2010-12-01 21:08 ` Benjamin R. Haskell
  2010-12-01 21:16   ` Christoph Wurm
  2010-12-02 11:07 ` Julien Jehannet
  2 siblings, 1 reply; 7+ messages in thread
From: Benjamin R. Haskell @ 2010-12-01 21:08 UTC (permalink / raw)
  To: Christoph Wurm; +Cc: zsh-users

On Wed, 1 Dec 2010, Christoph Wurm wrote:

> Hello,
>
> I would like to display the exit code of a command if it is not 0 on a 
> line of its own, followed by the prompt on the next line.
>
> Is this possible?
>
> I was able to come up with the following:
>
> PROMPT="%0(?..%?)
> %{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>
> However, this prints a newline after every command. Is there some 
> equivalent of '\n' that can be used inside the conditional substring? 
> Or some other way to do this?

Just put the portion that you want to be conditional inside the parens:

You had:
PROMPT="%0(?..%?)
%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "

Instead use:
PROMPT="%0(?..%?
)%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "

You also don't need the '0' in '%0', since it's the default, so:
PROMPT="%(?..%?
)%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "

Adding in my personal preferences, and the following facts:
1. I prefer $''-style quoting when newlines are involved.
2. Parameter expansion is handled as part of prompt expansion.
3. The lone number sitting on a line by itself seems weird.

I might use:
PROMPT=$'%(?..%{$fg[red]%}error=%?%{$reset_color%}\n)%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} '

-- 
Best,
Ben


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

* Re: Conditional newline in prompt
  2010-12-01 20:58 ` Mikael Magnusson
@ 2010-12-01 21:15   ` Christoph Wurm
  0 siblings, 0 replies; 7+ messages in thread
From: Christoph Wurm @ 2010-12-01 21:15 UTC (permalink / raw)
  To: zsh-users; +Cc: Mikael Magnusson

Sorry, forgot to change the recipient. Might be worth using Reply-To.

On 12/01/2010 09:58 PM, Mikael Magnusson wrote:
> On 1 December 2010 21:42, Christoph Wurm<christoph@cwurm.de>  wrote:
>> Hello,
>>
>> I would like to display the exit code of a command if it is not 0 on a line
>> of its own, followed by the prompt on the next line.
>>
>> Is this possible?
>>
>> I was able to come up with the following:
>>
>> PROMPT="%0(?..%?)
>> %{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>>
>> However, this prints a newline after every command. Is there some equivalent
>> of '\n' that can be used inside the conditional substring? Or some other way
>> to do this?
>
> Yes, a newline works fine.
> % PS1='%0(?..%?
> )%~>  '
> ~>  false
> 1
> ~>  true
> ~>
>

Thanks Mikael, there's an obvious solution I've overlooked while it was 
staring me in the face. ;-)

Christoph


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

* Re: Conditional newline in prompt
  2010-12-01 21:08 ` Benjamin R. Haskell
@ 2010-12-01 21:16   ` Christoph Wurm
  2010-12-01 22:32     ` Benjamin R. Haskell
  0 siblings, 1 reply; 7+ messages in thread
From: Christoph Wurm @ 2010-12-01 21:16 UTC (permalink / raw)
  To: zsh-users; +Cc: Benjamin R. Haskell

On 12/01/2010 10:08 PM, Benjamin R. Haskell wrote:
> On Wed, 1 Dec 2010, Christoph Wurm wrote:
>
>> Hello,
>>
>> I would like to display the exit code of a command if it is not 0 on a
>> line of its own, followed by the prompt on the next line.
>>
>> Is this possible?
>>
>> I was able to come up with the following:
>>
>> PROMPT="%0(?..%?)
>> %{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>>
>> However, this prints a newline after every command. Is there some
>> equivalent of '\n' that can be used inside the conditional substring?
>> Or some other way to do this?
>
> Just put the portion that you want to be conditional inside the parens:
>
> You had:
> PROMPT="%0(?..%?)
> %{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>
> Instead use:
> PROMPT="%0(?..%?
> )%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>
> You also don't need the '0' in '%0', since it's the default, so:
> PROMPT="%(?..%?
> )%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>
> Adding in my personal preferences, and the following facts:
> 1. I prefer $''-style quoting when newlines are involved.
> 2. Parameter expansion is handled as part of prompt expansion.
> 3. The lone number sitting on a line by itself seems weird.
>
> I might use:
> PROMPT=$'%(?..%{$fg[red]%}error=%?%{$reset_color%}\n)%{$fg[green]%}%n@%m%{$reset_color%}:%~
> %{$fg[red]%}%#%{$reset_color%} '
>

The color sequences ("%{$fg[red]%}" and the like) don't seem to work 
with $'', at least here on my machine.

Christoph


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

* Re: Conditional newline in prompt
  2010-12-01 21:16   ` Christoph Wurm
@ 2010-12-01 22:32     ` Benjamin R. Haskell
  0 siblings, 0 replies; 7+ messages in thread
From: Benjamin R. Haskell @ 2010-12-01 22:32 UTC (permalink / raw)
  To: Christoph Wurm; +Cc: zsh-users

On Wed, 1 Dec 2010, Christoph Wurm wrote:

> On 12/01/2010 10:08 PM, Benjamin R. Haskell wrote:
>> On Wed, 1 Dec 2010, Christoph Wurm wrote:
>> 
>>> Hello,
>>> 
>>> I would like to display the exit code of a command if it is not 0 on a
>>> line of its own, followed by the prompt on the next line.
>>> 
>>> Is this possible?
>>> 
>>> I was able to come up with the following:
>>> 
>>> PROMPT="%0(?..%?)
>>> %{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>>> 
>>> However, this prints a newline after every command. Is there some
>>> equivalent of '\n' that can be used inside the conditional substring?
>>> Or some other way to do this?
>> 
>> Just put the portion that you want to be conditional inside the parens:
>> 
>> You had:
>> PROMPT="%0(?..%?)
>> %{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>> 
>> Instead use:
>> PROMPT="%0(?..%?
>> )%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>> 
>> You also don't need the '0' in '%0', since it's the default, so:
>> PROMPT="%(?..%?
>> )%{$fg[green]%}%n@%m%{$reset_color%}:%~ %{$fg[red]%}%#%{$reset_color%} "
>> 
>> Adding in my personal preferences, and the following facts:
>> 1. I prefer $''-style quoting when newlines are involved.
>> 2. Parameter expansion is handled as part of prompt expansion.
>> 3. The lone number sitting on a line by itself seems weird.
>> 
>> I might use:
>> PROMPT=$'%(?..%{$fg[red]%}error=%?%{$reset_color%}\n)%{$fg[green]%}%n@%m%{$reset_color%}:%~
>> %{$fg[red]%}%#%{$reset_color%} '
>> 
>
> The color sequences ("%{$fg[red]%}" and the like) don't seem to work with 
> $'', at least here on my machine.

Ah. My bad.  I'd forgotten that `setopt prompt_subst` wasn't a default. 
(It's in my .zshrc)  I like the ability to store the literal strings in 
my prompt variable, allowing them to be changed externally.  (Never 
messed with PSVAR/psvar, which probably serves a similar purpose.)

-- 
Best,
Ben


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

* Re: Conditional newline in prompt
  2010-12-01 20:42 Conditional newline in prompt Christoph Wurm
  2010-12-01 20:58 ` Mikael Magnusson
  2010-12-01 21:08 ` Benjamin R. Haskell
@ 2010-12-02 11:07 ` Julien Jehannet
  2 siblings, 0 replies; 7+ messages in thread
From: Julien Jehannet @ 2010-12-02 11:07 UTC (permalink / raw)
  To: Christoph Wurm; +Cc: zsh-users

2010/12/1 Christoph Wurm <christoph@cwurm.de>:
> Hello,
>
> I would like to display the exit code of a command if it is not 0 on a line
> of its own, followed by the prompt on the next line.
>
> Is this possible?

I'm often impressed the way zsh predates user expectations.
In your case, did you try to set the PRINT_EXIT_VALUE option ?
It could avoid prompt intricacy :-)

Regards,
-- 
J u l i e n    J e h a n n e t


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

end of thread, other threads:[~2010-12-02 11:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-01 20:42 Conditional newline in prompt Christoph Wurm
2010-12-01 20:58 ` Mikael Magnusson
2010-12-01 21:15   ` Christoph Wurm
2010-12-01 21:08 ` Benjamin R. Haskell
2010-12-01 21:16   ` Christoph Wurm
2010-12-01 22:32     ` Benjamin R. Haskell
2010-12-02 11:07 ` Julien Jehannet

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).