zsh-users
 help / color / mirror / code / Atom feed
* get output of a command in prompt
@ 2006-09-16 18:49 arno.
  2006-09-16 19:01 ` Frank Terbeck
  2006-09-17 17:22 ` Phil Pennock
  0 siblings, 2 replies; 7+ messages in thread
From: arno. @ 2006-09-16 18:49 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 785 bytes --]

Hi,
I want to get the output of a command in my prompt 
(actually, I want the temperature of my cpu in RPS1 if it is superior 
to a some predifined value).
So I have something like :
 RPS1="%2v"
 and 

precmd() {
    gettemp
}

with gettemp that gets temperature and puts it in psvar[2] if needed.

Today, I've seen in zsh user guide :

> Some people make the mistake of using it [precmd function] to set up a 
> prompt, but there are enough ways of getting varying information into a 
> fixed prompt string that you shouldn't do that unless you have very odd 
> things in your prompt.
http://zsh.sunsite.dk/Guide/zshguide03.html#l51

But I didn't find a better way to do what I want. Did I miss something, 
or is my goal something "very odd".

thanks 
arno

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: get output of a command in prompt
  2006-09-16 18:49 get output of a command in prompt arno.
@ 2006-09-16 19:01 ` Frank Terbeck
  2006-09-17 14:14   ` arno.
  2006-09-17 17:22 ` Phil Pennock
  1 sibling, 1 reply; 7+ messages in thread
From: Frank Terbeck @ 2006-09-16 19:01 UTC (permalink / raw)
  To: zsh-users

arno. <arno.@no-log.org>:
[..get output of command into prompt..]

Take a look at this:

[snip]
zsh% PS1='$(printf "%s: %d" "This is a one" 1)-%% '
$(printf ": /home/hawk" "This is a one" 1)-% setopt promptsubst 
This is a one: 1-%
[snap]

Regards, Frank


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

* Re: get output of a command in prompt
  2006-09-16 19:01 ` Frank Terbeck
@ 2006-09-17 14:14   ` arno.
  2006-09-17 16:58     ` Bart Schaefer
  2006-09-17 17:09     ` Dan Nelson
  0 siblings, 2 replies; 7+ messages in thread
From: arno. @ 2006-09-17 14:14 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 533 bytes --]

Le Saturday 16 September 2006, à 21:01:55PM +0200, Frank a écrit : 
> arno. <arno.@no-log.org>:
> [..get output of command into prompt..]
> 
> Take a look at this:
> 
> [snip]
> zsh% PS1='$(printf "%s: %d" "This is a one" 1)-%% '
> $(printf ": /home/hawk" "This is a one" 1)-% setopt promptsubst 
> This is a one: 1-%
> [snap]

Yes, I've tried things like that, but the problem is that command output 
will be evaluated once, just when prompt is set up. It will not be 
evaluated each time prompt is displayed

arno

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: get output of a command in prompt
  2006-09-17 14:14   ` arno.
@ 2006-09-17 16:58     ` Bart Schaefer
  2006-09-17 17:09     ` Dan Nelson
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2006-09-17 16:58 UTC (permalink / raw)
  To: zsh-users

On Sep 17,  4:14pm, arno. wrote:
}
} Le Saturday 16 September 2006, à 21:01:55PM +0200, Frank a écrit : 
} > zsh% PS1='$(printf "%s: %d" "This is a one" 1)-%% '
} > $(printf ": /home/hawk" "This is a one" 1)-% setopt promptsubst 
} > This is a one: 1-%
} 
} Yes, I've tried things like that, but the problem is that command output 
} will be evaluated once, just when prompt is set up.

Not true.  The quoting is important:  Note where single quotes (') have
been used and where double quotes have.  Compare:

PS1="$(printf '%s: %d' 'This is a one' 1)-%% " ;: This is the wrong way
PS1='$(printf "%s: %d" "This is a one" 1)-%% ' ;: This is the right way

} It will not be evaluated each time prompt is displayed

It will if the quoting is right and the promptsubst option has been set.

Question for zsh-workers:  Why is the prompt string being evaluated more
than once on every zle reset?

torch% PS1='z is $((++z)) %# '
z is $((++z)) % setopt promptsubst
z is 2 % echo I expect three
I expect three
z is 5 % 
z is 8 % echo I guess I should expect eleven
I guess I should expect eleven
z is 11 % 

This could be expensive for command substitutions in the prompt.


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

* Re: get output of a command in prompt
  2006-09-17 14:14   ` arno.
  2006-09-17 16:58     ` Bart Schaefer
@ 2006-09-17 17:09     ` Dan Nelson
  2006-09-17 17:24       ` arno.
  1 sibling, 1 reply; 7+ messages in thread
From: Dan Nelson @ 2006-09-17 17:09 UTC (permalink / raw)
  To: arno.; +Cc: zsh-users

In the last episode (Sep 17), arno. said:
> Le Saturday 16 September 2006, à 21:01:55PM +0200, Frank a écrit : 
> > arno. <arno.@no-log.org>:
> > [..get output of command into prompt..]
> > 
> > Take a look at this:
> > 
> > [snip]
> > zsh% PS1='$(printf "%s: %d" "This is a one" 1)-%% '
> > $(printf ": /home/hawk" "This is a one" 1)-% setopt promptsubst 
> > This is a one: 1-%
> > [snap]
> 
> Yes, I've tried things like that, but the problem is that command output 
> will be evaluated once, just when prompt is set up. It will not be 
> evaluated each time prompt is displayed

That example does evalute the prompt each time it's printed; you just
can't tell.  Try with a command that changes its output:

zsh% PS1='$(date)-%% ' ; setopt promptsubst
Sun Sep 17 12:06:02 CDT 2006-% <press enter>
Sun Sep 17 12:06:33 CDT 2006-% <press enter>
Sun Sep 17 12:06:34 CDT 2006-% 

Of course in this example you'd be better off using zsh's builtin
date-printing prompt escapes, but you get the idea.

-- 
	Dan Nelson
	dnelson@allantgroup.com


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

* Re: get output of a command in prompt
  2006-09-16 18:49 get output of a command in prompt arno.
  2006-09-16 19:01 ` Frank Terbeck
@ 2006-09-17 17:22 ` Phil Pennock
  1 sibling, 0 replies; 7+ messages in thread
From: Phil Pennock @ 2006-09-17 17:22 UTC (permalink / raw)
  To: zsh-users

On 2006-09-16 at 20:49 +0200, arno. wrote:
> So I have something like :
>  RPS1="%2v"
>  and 
> 
> precmd() {
>     gettemp
> }

> > Some people make the mistake of using it [precmd function] to set up a 
> > prompt, but there are enough ways of getting varying information into a 
> > fixed prompt string that you shouldn't do that unless you have very odd 
> > things in your prompt.
> http://zsh.sunsite.dk/Guide/zshguide03.html#l51
> 
> But I didn't find a better way to do what I want. Did I miss something, 
> or is my goal something "very odd".

I interpret the user guide text to mean "Don't set the prompt in
precmd()", given the following text: "It's a big nuisance having to
redefine precmd to alter your prompt --- especially if you don't know
it's there, since then your prompt apparently magically returns to the
same format when you change it."


So they're saying that instead of using:
 function precmd { RPS1="$(gettemp)" }
you should instead do exactly what you are doing.

For a distributed example of using precmd, see the various
prompt_*_setup routines; in particular, prompt_zefram_setup shows
something manipulating various $psvar values for later display in the
prompt.

Regards,
-Phil


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

* Re: get output of a command in prompt
  2006-09-17 17:09     ` Dan Nelson
@ 2006-09-17 17:24       ` arno.
  0 siblings, 0 replies; 7+ messages in thread
From: arno. @ 2006-09-17 17:24 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1221 bytes --]

Le Sunday 17 September 2006, à 12:09:14PM -0500, Dan a écrit : 
> In the last episode (Sep 17), arno. said:
> > Le Saturday 16 September 2006, à 21:01:55PM +0200, Frank a écrit : 
> > > arno. <arno.@no-log.org>:
> > > [..get output of command into prompt..]
> > > 
> > > Take a look at this:
> > > 
> > > [snip]
> > > zsh% PS1='$(printf "%s: %d" "This is a one" 1)-%% '
> > > $(printf ": /home/hawk" "This is a one" 1)-% setopt promptsubst 
> > > This is a one: 1-%
> > > [snap]
> > 
> > Yes, I've tried things like that, but the problem is that command output 
> > will be evaluated once, just when prompt is set up. It will not be 
> > evaluated each time prompt is displayed
> 
> That example does evalute the prompt each time it's printed; you just
> can't tell.  Try with a command that changes its output:
> 
> zsh% PS1='$(date)-%% ' ; setopt promptsubst
> Sun Sep 17 12:06:02 CDT 2006-% <press enter>
> Sun Sep 17 12:06:33 CDT 2006-% <press enter>
> Sun Sep 17 12:06:34 CDT 2006-% 
> 
> Of course in this example you'd be better off using zsh's builtin
> date-printing prompt escapes, but you get the idea.

Oups,
looks like I did it wrong before.
It works now
thanx a lot

arno

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

end of thread, other threads:[~2006-09-17 17:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-16 18:49 get output of a command in prompt arno.
2006-09-16 19:01 ` Frank Terbeck
2006-09-17 14:14   ` arno.
2006-09-17 16:58     ` Bart Schaefer
2006-09-17 17:09     ` Dan Nelson
2006-09-17 17:24       ` arno.
2006-09-17 17:22 ` Phil Pennock

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