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

Re: Count only visible characters?



Am 09.04.2010 um 14:08 schrieb Peter Stephenson:

> On Fri, 9 Apr 2010 12:56:02 +0200
> Sebastian Stark <seb-zsh@xxxxxxxxxxx> wrote:
>> print "${(%)a::=%F{green}hello}"
>> hello}
>> % print "${(%)a::=%F{green}hello"
>> hello
>> 
>> Why does the first print statement print the closing brace? How can the
>> second print statement not produce an error?
> 
> ${...} doesn't attempt to balance braces internally, so the first } brings
> it to an end.  The final quoted "}" is then just an ordinary character.

ok, good to know.

> You're being a bit too clever for your own good here by trying to too much
> at once.  If you separate out the assignment from the use it'll work.
> If you absolutely have to do it this way, however, quoting the text you
> want to be treated as a string by the parameter expansion works.
> 
> print "${(%)a::="%F{green}hello"}"
> 
> The outer double quotes are now overloaded too many times to be able to
> tell this is what you mean: see a recent post by Bart for a reference to a
> discussion on the standards mailing list about trouble with double quotes.

The expression works as I expect when I leave out all the double quotes. I didn't think those were the problem.

> I'm not sure you actually want the outer double quotes at all, but I'm not
> sure what you're trying to do (and it probably doesn't matter for this
> particular issue).

Sometimes I want to use expansions that are only available as prompt expansions. It's a bit cumbersome having to assign them to a variable first and then evaluate it with the %-modifier. One example would be a condition that checks wether a background job is running:

  if ((${(%):-%j}))
  then
    print "no reexec because background job is running"
    return $E_JOB
  fi

In this example the above is not an issue. But it could appear in other contexts, when using "inline prompt expansion" in print statements. I admit this is a bit esoteric, but still worth mentioning.


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