zsh-users
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Sebastian Stark <sstark+zsh@mailbox.org>
Cc: zsh-users@zsh.org
Subject: Re: Dynamic parameters for PROMPT_SUBST functions
Date: Sat, 20 Jun 2020 11:06:29 +0000	[thread overview]
Message-ID: <20200620110629.3a7f26ab@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <20200619170847.iq2bjrnrmon5cctd@singold>

Sebastian Stark wrote on Fri, 19 Jun 2020 19:08 +0200:
> Am Freitag, den 19. Juni 2020 um 13:04 schrieb Daniel Shahaf:
> >Sebastian Stark wrote on Fri, 19 Jun 2020 08:32 +0200:  
> >> I am trying to have a function call in my prompt that gets the return
> >> value of the last command as a parameter (%?).  
> >
> >What would you do with «%?» if you could get its value?  
> 
> I would try to split it into signal and return value information, so I 
> do not get "130" if I ctrl-c, but something like 0 and INT.

That information isn't available via %?:
.
    % PS1='%?%# '
    0% perl -E 'kill 9, $$' 
    zsh: killed     perl -E 'kill 9, $$'
    137% perl -E 'exit (9 + 128)' 
    137% 

Note how %? expanded to the same value in both cases.

The information is not available via $pipestatus either.

However, the "killed" (or "interrupted", etc) message is only printed
when a job exited with a signal.

Furthermore, if you don't care about programs whose exit codes just
happen to be in the 128+signal range, you can do:
.
    % setopt promptsubst
    % PS1='$signals[1 + ($? - 128)]%# '
    % (exit 137)
    zsh: exit 137   ( exit 137; )
    KILL% 

The variable «$signals» is predefined.  Just make sure that $? is the
right value.  (If you have other $(…) in there, they might overwrite
«$?»?  I haven't tested.)

And to show the numeric exit code when there isn't a signal associated,
using the ternary condition syntax:
.
    PS1='${signals[1 + ($? - 128)]:-"%(?..%?)"}%# '

This does not handle the case that ${signals[…]} is EXIT, ZERR, or DEBUG.

Cheers,

Daniel

  reply	other threads:[~2020-06-20 11:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-19  6:32 Sebastian Stark
2020-06-19  6:58 ` Lawrence Velázquez
2020-06-19 10:56 ` Daniel Shahaf
2020-06-19 17:08   ` Sebastian Stark
2020-06-20 11:06     ` Daniel Shahaf [this message]
2020-06-21 15:17       ` Sebastian Stark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200620110629.3a7f26ab@tarpaulin.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=sstark+zsh@mailbox.org \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).