zsh-users
 help / color / mirror / code / Atom feed
* Dynamic parameters for PROMPT_SUBST functions
@ 2020-06-19  6:32 Sebastian Stark
  2020-06-19  6:58 ` Lawrence Velázquez
  2020-06-19 10:56 ` Daniel Shahaf
  0 siblings, 2 replies; 6+ messages in thread
From: Sebastian Stark @ 2020-06-19  6:32 UTC (permalink / raw)
  To: zsh-users


Hello,

I am trying to have a function call in my prompt that gets the return 
value of the last command as a parameter (%?).

However I am having trouble to get this right. Instead of an actual 
number, what is handed to the function is the literal string %?.

Is there anything I can do better or is this just not possible?

I could probably use $? in the function itself, but that sounds a bit 
fragile to me because I maybe get a different value of $?.

In this example I write the parameter to a file, because if I just print 
it it would get printed as a literal %? and expanded later and thus hide 
the issue I am facing


% zsh -f
% x(){print $1 >>/tmp/lll}
% setopt PROMPT_SUBST
% PROMPT=$'$(x %?) %m%# '
  % cat /tmp/lll
%?


Thanks for any hints.

Sebastian

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

* Re: Dynamic parameters for PROMPT_SUBST functions
  2020-06-19  6:32 Dynamic parameters for PROMPT_SUBST functions Sebastian Stark
@ 2020-06-19  6:58 ` Lawrence Velázquez
  2020-06-19 10:56 ` Daniel Shahaf
  1 sibling, 0 replies; 6+ messages in thread
From: Lawrence Velázquez @ 2020-06-19  6:58 UTC (permalink / raw)
  To: Sebastian Stark; +Cc: zsh-users

Hi Sebastian,

> On Jun 19, 2020, at 2:32 AM, Sebastian Stark <sstark+zsh@mailbox.org>
> wrote:
> 
> I am trying to have a function call in my prompt that gets the
> return value of the last command as a parameter (%?).
> 
> However I am having trouble to get this right. Instead of an actual
> number, what is handed to the function is the literal string %?.

Yes, that is because the PROMPT_SUBST expansions (parameter, command,
and arithmetic) are done before the percent-escape sequences are
expanded.

vq

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

* Re: Dynamic parameters for PROMPT_SUBST functions
  2020-06-19  6:32 Dynamic parameters for PROMPT_SUBST functions Sebastian Stark
  2020-06-19  6:58 ` Lawrence Velázquez
@ 2020-06-19 10:56 ` Daniel Shahaf
  2020-06-19 17:08   ` Sebastian Stark
  1 sibling, 1 reply; 6+ messages in thread
From: Daniel Shahaf @ 2020-06-19 10:56 UTC (permalink / raw)
  To: Sebastian Stark; +Cc: zsh-users

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?

> In this example I write the parameter to a file, because if I just print 
> it it would get printed as a literal %? and expanded later and thus hide 
> the issue I am facing

You could also print to /dev/tty.

Cheers,

Daniel


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

* Re: Dynamic parameters for PROMPT_SUBST functions
  2020-06-19 10:56 ` Daniel Shahaf
@ 2020-06-19 17:08   ` Sebastian Stark
  2020-06-20 11:06     ` Daniel Shahaf
  0 siblings, 1 reply; 6+ messages in thread
From: Sebastian Stark @ 2020-06-19 17:08 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-users

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.

>> In this example I write the parameter to a file, because if I just print
>> it it would get printed as a literal %? and expanded later and thus hide
>> the issue I am facing
>
>You could also print to /dev/tty.

That's at least something I can take away from this, thanks :)


Sebastian

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

* Re: Dynamic parameters for PROMPT_SUBST functions
  2020-06-19 17:08   ` Sebastian Stark
@ 2020-06-20 11:06     ` Daniel Shahaf
  2020-06-21 15:17       ` Sebastian Stark
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Shahaf @ 2020-06-20 11:06 UTC (permalink / raw)
  To: Sebastian Stark; +Cc: zsh-users

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

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

* Re: Dynamic parameters for PROMPT_SUBST functions
  2020-06-20 11:06     ` Daniel Shahaf
@ 2020-06-21 15:17       ` Sebastian Stark
  0 siblings, 0 replies; 6+ messages in thread
From: Sebastian Stark @ 2020-06-21 15:17 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-users

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

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

end of thread, other threads:[~2020-06-21 15:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-19  6:32 Dynamic parameters for PROMPT_SUBST functions 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
2020-06-21 15:17       ` Sebastian Stark

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