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

Re: Dynamic parameters for PROMPT_SUBST functions



Am Samstag, den 20. Juni 2020 um 13:06 schrieb Daniel Shahaf:
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.

Yes, I noticed that this is not entirely clean.

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

Thanks for this, I didn't know about the signals array. Also, your solution looks pretty much like what I was looking for.

However, in my prompt variable this expands to the number, not the signal name. Your code does the right thing when used as PS1 without anything else, but not in combination with my other things in there. For now I will put that on my pile of unfinished things, as it seems to be complicated for little outcome.

Thanks again!


Sebastian



Messages sorted by: Reverse Date, Date, Thread, Author