From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27425 invoked by alias); 10 Apr 2010 04:02:49 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 14999 Received: (qmail 27476 invoked from network); 10 Apr 2010 04:02:38 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <100409210222.ZM16418@torch.brasslantern.com> Date: Fri, 09 Apr 2010 21:02:22 -0700 In-reply-to: <20100409205023.21295434@pws-pc> Comments: In reply to Peter Stephenson "Re: Count only visible characters?" (Apr 9, 8:50pm) References: <20100409205023.21295434@pws-pc> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: Count only visible characters? MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Apr 9, 8:50pm, Peter Stephenson wrote: } On Thu, 8 Apr 2010 22:38:05 +0000 (UTC) } Seth House wrote: } > Is there any way to count only visible characters? } > } > testing="%F{green}hello" } > echo ${(%)#testing} } > } > The number I'm interested in is 5 not 14. Incidentally, ${(%)#testing} doesn't do what you think it does. # is applied BEFORE (%), making (%) rather useless. What you meant was ${#${(%)testing}}. } That's correct, it isn't trivial using shell code. (It's not *so* hard } to strip the various highlighting codes and %{ ... %} sections if you } want an exercise in shell programming.) There's code to do part of this in Functions/Prompts/prompt_bart_setup, in the prompt_bart_precmd function. Search for "zero". It doesn't handle %{ %} pairs, though, just the other zero-width strings. } However, the code to do this is already built into the shell itself, } so making this available is a straightforward change. Note you need } the `#' inside the parentheses---the normal `#' is too long } established for me to be keen on changing its meaning even in } combination with `(%)'. Hmm. Might I suggest that this is of somewhat limited utility? A better approach might be to add an option that produces the string with the zero-width characters removed, and then simply apply the old normal '#' operator to that result. Perhaps tripling the (%) flag means this, e.g. print ${(%%%):-%F{red}hello%f there} hello there print ${#${(%%%):-%F{red}hello%f there}} 11 My one qualm about (%%%) is that (%%) does more expansion than (%) and one might want e.g. to count the result of (%) without doing the PROMPT_SUBST etc. expansion ... I realize this isn't as straightforward as what you just patched. A related suggestion that doesn't actually solve the problem of terminal escapes: Doubling the (V) flag could remove all special characters from the result, rather than making them visible. (That should also have the effect of removing any fragmentary multibyte characters, I think.)