From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 172 invoked by alias); 14 May 2015 03:46:56 -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: 20211 Received: (qmail 25646 invoked from network); 14 May 2015 03:46:45 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version:content-type; bh=HOnmG0sd8RVVHaRZJIhjmUuAmiyJt/WPLTSJsCSoL7Q=; b=mxwVMefJn7quXSyZVTW022rZ12BMgyfu3SCertPvS9jSv1HO2SeNwjoNrybmpOTWu4 pEpiDOpMYkiBfxk8Y9RPgW8ocmIzamkOY69v8QQq4IYit44bMA6GR07SinzwfGoIohQV VMBkzxtjse5gWYKzwrOKC8H4i5j09tdXxl25mztREByV6oVOwf+rf7Wb/4aq62JHGlB+ PrN6xvohqkj6X1LPyK7QsC2TKLeprNEsnnekzATmLaCOA4rGOmeKQkkH+1f2ayqZX2pE ThHoaTiBxkOXO7iP98ed0mRQrQFLHebneXSwzikIQU2demRpWJ1xaKCSO05n2B7ybMjI 85ag== X-Gm-Message-State: ALoCoQkY6XKbWzLBOoYljkeRhCjYGlHi2S8IDWYq+vori4OjazKB3mhnEsvkt0EJD2bu7YJyEph7 X-Received: by 10.202.94.215 with SMTP id s206mr1662230oib.15.1431575203620; Wed, 13 May 2015 20:46:43 -0700 (PDT) From: Bart Schaefer Message-Id: <150513204639.ZM29550@torch.brasslantern.com> Date: Wed, 13 May 2015 20:46:39 -0700 In-Reply-To: <5553D128.70203@thequod.de> Comments: In reply to Daniel Hahler "Get visible length of a string (removing escape sequences)" (May 14, 12:33am) References: <5553D128.70203@thequod.de> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: Get visible length of a string (removing escape sequences) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On May 14, 12:33am, Daniel Hahler wrote: } } I am looking for a function to get the visible length of a string, } which would remove any escape codes for colors etc. The example you found on stackoverflow appears to have been lifted from Functions/Prompts/prompt_bart_setup (the prompt_bart_precmd function). It does NOT compute the "visible" length of an arbitrary string. It gets the visible length of a string formatted as a $PROMPT value, which means all the color codes and other zero-width stuff is expected to be wrapped in %{...%} or to be represented by prompt escapes like %B, %U, etc. If you have a string with actual ANSI escapes but not %{...%} delimiters, that example won't apply. } but it throws an error for '`foo`': zsh: command not found: foo What precisely would you want to happen in that case? $(somecommand) or `somecommand` can insert an arbitrary string if promptsubst is set. You need to count the length of that string. If the command is not found or otherwise fails, how do you know what to count? } I've tried to throw in "setopt localoptions nopromptsubst", but that } appears to not expand the ANSI color codes anymore. Isn't the whole point to not expand the ANSI color codes, but rather to discard them so they're not counted? Anyway turning off promptsubst would at best count `foo` as 5 characters, when it might expand to one or a thousand or anything in actual practice. } Hopefully there's a better way? } } Maybe there could be some extension to "$#" to make this simpler? It's still not clear to me what "this" is. Zsh is not going to grow the ability to understand ANSI escapes just for the purpose of skipping them when counting with $#. On the other hand there are a limited number of patterns than an ANSI escape might match; an analogue of the $zero pattern in the example from stackoverflow could probably be written to filter raw escapes, if that's what you need.