zsh-users
 help / color / Atom feed
* Processing "%?" in PROMPT?
@ 2019-11-20 21:27 Christopher Nebel
  2019-11-20 21:42 ` Roman Perepelitsa
  0 siblings, 1 reply; 2+ messages in thread
From: Christopher Nebel @ 2019-11-20 21:27 UTC (permalink / raw)
  To: zsh-users

In my PROMPT, I want to get the status of the last command executed before the prompt, what in prompt expansion would be %?, but I want to pass it to a function.  (Why?  I like the idea of %? in the prompt, but thought it would be nice to dress it up a bit by rendering it as something other than just a number: “EX_USAGE” instead of “64” or “SIGILL” instead of “132”.  That means passing the value of %? to a function to generate the text that appears in the prompt.)

My first attempt was to set PROMPT to ‘$(pretty-status %?)’, which doesn’t work because the “%?” is passed literally.  I’ve tried various ways of evaluating “%?” — “print -P %?”, the “(%)” expansion flag — but none them work, because they expand to the same thing as “$?”, which is the status of the last command executed anywhere, including inside command substitution for the prompt.  I could use a precmd function to save $? in a global, but only if I could guarantee that it was called before anything else, and that’s not a promise I’m comfortable making.

Any suggestions?  zsh clearly has the value I want, since it uses it in prompt percent expansion, but how do I get at it?  I’m honestly not sure if the behavior I’m seeing in “print -P %?” is a bug or if it’s working as designed.  I’m mostly using zsh 5.3, but I’m seeing the same thing in 5.9.


—Chris N.

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

* Re: Processing "%?" in PROMPT?
  2019-11-20 21:27 Processing "%?" in PROMPT? Christopher Nebel
@ 2019-11-20 21:42 ` Roman Perepelitsa
  0 siblings, 0 replies; 2+ messages in thread
From: Roman Perepelitsa @ 2019-11-20 21:42 UTC (permalink / raw)
  To: Christopher Nebel; +Cc: zsh-users

On Wed, Nov 20, 2019 at 10:28 PM Christopher Nebel <c.nebel@apple.com> wrote:
> My first attempt was to set PROMPT to ‘$(pretty-status %?)’

$? will work the way you want if it's the first command substitution
in PROMPT. The same goes for RPROMPT.

> I could use a precmd function to save $? in a global, but only if
> I could guarantee that it was called before anything else, and that’s
> not a promise I’m comfortable making.

$? (a.k.a. $status) and $pipestatus are guaranteed to work as you
expect when precmd hooks are called. It's quite common to stash them
into global variables there.


Roman.

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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-20 21:27 Processing "%?" in PROMPT? Christopher Nebel
2019-11-20 21:42 ` Roman Perepelitsa

zsh-users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-users

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git