Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Dynamic parameters for PROMPT_SUBST functions
- X-seq: zsh-users 24938
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: Sebastian Stark <sstark+zsh@xxxxxxxxxxx>
- Subject: Re: Dynamic parameters for PROMPT_SUBST functions
- Date: Sat, 20 Jun 2020 11:06:29 +0000
- Cc: zsh-users@xxxxxxx
- In-reply-to: <20200619170847.iq2bjrnrmon5cctd@singold>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <20200619063233.zybs27xvqp56t4p5@singold> <20200619105640.17bd3107@tarpaulin.shahaf.local2> <20200619170847.iq2bjrnrmon5cctd@singold>
- Sender: zsh-users@xxxxxxx
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author