* Execute a Command in PS1 w/o Disturbing Previous Return Value?
@ 2004-08-13 3:35 Aaron Davies
2004-08-13 4:52 ` Philippe Troin
2004-08-13 17:32 ` Bart Schaefer
0 siblings, 2 replies; 5+ messages in thread
From: Aaron Davies @ 2004-08-13 3:35 UTC (permalink / raw)
To: zsh-users
I'm trying to write a prompt that will do two things: include a file
frequently re-downloaded from the web, and indicate the return status
of the previous command. The problem is, if I cat out the file, it
returns 0, overwriting the return status read by the %(0?%) directive,
and guaranteeing that I will always get a prompt indicating success.
What can I do about this?
Here's the whole line. The breakdown is: current temperature, pwd, (if
last command successful, a green smiley, else a red frowny), prompt
char.
export PS1='`cat /tmp/temperature`'" %m%~ %(0?."`echo
'%{\e[1;32m%}:)%{\e[0m%}'`"."`echo \
'%{\e[1;31m%}:(%{\e[0m%}'`") %# "
(/tmp/temperature is refreshed every five minutes by a cron job, in
case you were wondering.)
--
__ __
/ ) / )
/--/ __. .__ ______ / / __. , __o _ _
/ (_(_/|_/ (_(_) / (_ (__/_(_/|_\/ <__</_/_)_
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Execute a Command in PS1 w/o Disturbing Previous Return Value?
2004-08-13 3:35 Execute a Command in PS1 w/o Disturbing Previous Return Value? Aaron Davies
@ 2004-08-13 4:52 ` Philippe Troin
2004-08-13 17:32 ` Bart Schaefer
1 sibling, 0 replies; 5+ messages in thread
From: Philippe Troin @ 2004-08-13 4:52 UTC (permalink / raw)
To: Aaron Davies; +Cc: zsh-users
Aaron Davies <agdavi01@louisville.edu> writes:
> I'm trying to write a prompt that will do two things: include a file
> frequently re-downloaded from the web, and indicate the return status
> of the previous command. The problem is, if I cat out the file, it
> returns 0, overwriting the return status read by the %(0?%) directive,
> and guaranteeing that I will always get a prompt indicating
> success. What can I do about this?
>
> Here's the whole line. The breakdown is: current temperature, pwd, (if
> last command successful, a green smiley, else a red frowny), prompt
> char.
>
> export PS1='`cat /tmp/temperature`'" %m%~ %(0?."`echo
> '%{\e[1;32m%}:)%{\e[0m%}'`"."`echo \
> '%{\e[1;31m%}:(%{\e[0m%}'`") %# "
>
> (/tmp/temperature is refreshed every five minutes by a cron job, in
> case you were wondering.)
What about:
precmd() {
local r=$?
psvar=()
psvar=("$(< /tmp/temperature)")
(( $r )) && psvar=($psvar $r)
}
export PS1="$(print '%1v %m%~ %(2v,%{\e[1;31m%}:( [%2v]%{\e[0m%},%{\e[1;32m%}:%)%{\e[0m%}) %# ')"
I prefer "setopt printexitvalue" myself...
Phil.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Execute a Command in PS1 w/o Disturbing Previous Return Value?
2004-08-13 3:35 Execute a Command in PS1 w/o Disturbing Previous Return Value? Aaron Davies
2004-08-13 4:52 ` Philippe Troin
@ 2004-08-13 17:32 ` Bart Schaefer
2004-08-14 16:07 ` Aaron Davies
1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2004-08-13 17:32 UTC (permalink / raw)
To: zsh-users
On Thu, 12 Aug 2004, Aaron Davies wrote:
> I'm trying to write a prompt that will do two things: include a file
> frequently re-downloaded from the web, and indicate the return status of
> the previous command. The problem is, if I cat out the file, it returns
> 0, overwriting the return status read by the %(0?%) directive
This should probably be considered a bug.
> What can I do about this?
In addition to Philippe's suggestion of using precmd so that $? is saved
and restored for you by the shell -- which is what should be happening
during command substitutions in prompt expansion, but isn't -- a simple
alternative is to use $(<...) instead of `cat ...`.
You can also use $'...' instead of `echo '...'`
export PS1='$(</tmp/temperature)'" %m%~ %(0?."\
$'%{\e[1;32m%}:)%{\e[0m%}.%{\e[1;31m%}:(%{\e[0m%}'") %# "
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Execute a Command in PS1 w/o Disturbing Previous Return Value?
2004-08-13 17:32 ` Bart Schaefer
@ 2004-08-14 16:07 ` Aaron Davies
2004-08-15 15:17 ` Phil!Gregory
0 siblings, 1 reply; 5+ messages in thread
From: Aaron Davies @ 2004-08-14 16:07 UTC (permalink / raw)
To: zsh-users
On Friday, August 13, 2004, at 01:32 PM, Bart Schaefer wrote:
> On Thu, 12 Aug 2004, Aaron Davies wrote:
>
>> I'm trying to write a prompt that will do two things: include a file
>> frequently re-downloaded from the web, and indicate the return status
>> of
>> the previous command. The problem is, if I cat out the file, it
>> returns
>> 0, overwriting the return status read by the %(0?%) directive
>
> This should probably be considered a bug.
I dunno, I can see where someone might want a prompt so complicated it
needed internal error-handling based on return value of executed
commands (cat foo || cat bar, etc.). Would "fixing" this prevent that?
>> What can I do about this?
>
> In addition to Philippe's suggestion of using precmd so that $? is
> saved
> and restored for you by the shell -- which is what should be happening
> during command substitutions in prompt expansion, but isn't -- a simple
> alternative is to use $(<...) instead of `cat ...`.
>
> You can also use $'...' instead of `echo '...'`
>
> export PS1='$(</tmp/temperature)'" %m%~ %(0?."\
> $'%{\e[1;32m%}:)%{\e[0m%}.%{\e[1;31m%}:(%{\e[0m%}'") %# "
Thank you, that works perfectly!
--
__ __
/ ) / )
/--/ __. .__ ______ / / __. , __o _ _
/ (_(_/|_/ (_(_) / (_ (__/_(_/|_\/ <__</_/_)_
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Execute a Command in PS1 w/o Disturbing Previous Return Value?
2004-08-14 16:07 ` Aaron Davies
@ 2004-08-15 15:17 ` Phil!Gregory
0 siblings, 0 replies; 5+ messages in thread
From: Phil!Gregory @ 2004-08-15 15:17 UTC (permalink / raw)
To: zsh-users
* Aaron Davies <agdavi01@louisville.edu> [2004-08-14 12:07 -0400]:
> I dunno, I can see where someone might want a prompt so complicated it
> needed internal error-handling based on return value of executed
> commands (cat foo || cat bar, etc.). Would "fixing" this prevent that?
Shouldn't. IIRC, the code for precmd is as simple as saving $? at the
beginning and then restoring it at the end.
(I ran into this problem a while ago and solved it by simply moving all
execution into precmd().)
--
...computer contrarian of the first order... / http://aperiodic.net/phil/
PGP: 026A27F2 print: D200 5BDB FC4B B24A 9248 9F7A 4322 2D22 026A 27F2
--- --
You can't wake a person who is pretending to be asleep.
-- Navajo Proverb
---- --- --
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2004-08-15 15:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-13 3:35 Execute a Command in PS1 w/o Disturbing Previous Return Value? Aaron Davies
2004-08-13 4:52 ` Philippe Troin
2004-08-13 17:32 ` Bart Schaefer
2004-08-14 16:07 ` Aaron Davies
2004-08-15 15:17 ` Phil!Gregory
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).