zsh-users
 help / color / mirror / code / Atom feed
* psvar + color
@ 2006-11-14  0:02 Michael Hernandez
  2006-11-14 10:37 ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Hernandez @ 2006-11-14  0:02 UTC (permalink / raw)
  To: zsh-users

I am trying to dynamically set the color of my battery percentage in  
my prompt. It's a long story, but I am using zsh 4.2.5 on OS X, along  
with a slightly modified version of a zsh script called "battery"  
which I found on line. I set the PROMPT_SUBST option, but that  
doesn't seem to be helping (most likely I am just doing something  
wrong).

I am using a version of the clint prompt and have been hacking away  
at a copy of the file called prompt_clint_setup which is included  
with the zsh distribution.

 From what I see, there are precmd functions that set psvar[2] to the  
current status, and psvar[2] is accessed as %2v

What I'd like to have is %2v (which could be *AC* (94.7%) for  
example) change between green, yellow, or red depending on the  
percentage. In this case it would be green, etc.

The problem is that no matter what I do, I can't get the color to  
change by using %3v, for example, neither:

p_apm="${fg_no_bold[%3v]}%(2v.*%2v.) "

or

p_apm="%{$fg_no_bold[%3v]}%}%(2v.*%2v.) "

work properly.

In the spirit of trying every possible thing I could think of I even  
tried p_apm="${fg_no_bold[$(print -P %3v)]}%(2v.*%2v.) "

If I simply use p_apm="${fg_no_bold[red]}%(2v.*%2v.) " - then the  
percentage comes out in red, so I think my trouble is in the way  
psvar is substituted through the %3v. I didn't think it would be a  
problem because %2v changes frequently (if my battery is charging or  
is unplugged at least) and I see the changes with each new prompt  
when I press return. I was hoping that I could set the color in a  
similar way. I have been checking psvar by echo $psvar[3] and also  
print -P %3v after each prompt and it is being set to the correct  
color. The more I look at it, the more the issue seems to be in the  
color escape being substituted properly, not in the setting of $psvar[3]

What is it that I'm doing wrong? I'd post the whole file but I don't  
want other mistakes of mine to distract attention from this one I am  
currently trying to iron out. It's my hope to share the config in  
it's entirety when it's complete, so that other os x users might  
enjoy a conveniently colored battery state+ percentage in their  
prompts if they wish :)

Any help is appreciated, thanks!

Mike H


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

* Re: psvar + color
  2006-11-14  0:02 psvar + color Michael Hernandez
@ 2006-11-14 10:37 ` Peter Stephenson
  2006-11-14 14:05   ` Michael Hernandez
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2006-11-14 10:37 UTC (permalink / raw)
  To: zsh-users

Michael Hernandez wrote:
> The problem is that no matter what I do, I can't get the color to  
> change by using %3v, for example, neither:
> 
> p_apm="${fg_no_bold[%3v]}%(2v.*%2v.) "
> 
> or
> 
> p_apm="%{$fg_no_bold[%3v]}%}%(2v.*%2v.) "
> 
> work properly.

Unfortunately the substitution is in the other order: the PROMPT_SUBST
replacement of variables comes before % substitution, so this scheme
wouldn't work.

> In the spirit of trying every possible thing I could think of I even  
> tried p_apm="${fg_no_bold[$(print -P %3v)]}%(2v.*%2v.) "

This is close to working: the problem is the double quotes.  This forces
the parameter expansion and command substitution to be performed
during the assignment.  If you run 'print -r -- $p_apm' while changing
psvar you'll see it outputs the same fixed string.  The %'s get
substituted later, but that's not good enough.

However, given that you have PROMPT_SUBST set you don't need to rely on
% substitution at all for the array subscript:

p_apm='%{${fg_no_bold[$psvar[3]]}%}%(2v.*%2v.) '

should do what you want.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* Re: psvar + color
  2006-11-14 10:37 ` Peter Stephenson
@ 2006-11-14 14:05   ` Michael Hernandez
  0 siblings, 0 replies; 3+ messages in thread
From: Michael Hernandez @ 2006-11-14 14:05 UTC (permalink / raw)
  To: zsh-users


On Nov 14, 2006, at 5:37 AM, Peter Stephenson wrote:
>
>
> However, given that you have PROMPT_SUBST set you don't need to  
> rely on
> % substitution at all for the array subscript:
>
> p_apm='%{${fg_no_bold[$psvar[3]]}%}%(2v.*%2v.) '
>
> should do what you want.

Thank you so much, that works perfectly!

MH


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

end of thread, other threads:[~2006-11-14 14:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-14  0:02 psvar + color Michael Hernandez
2006-11-14 10:37 ` Peter Stephenson
2006-11-14 14:05   ` Michael Hernandez

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