Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Dynamic parameters for PROMPT_SUBST functions



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