zsh-users
 help / color / mirror / code / Atom feed
* Get visible length of a string (removing escape sequences)
@ 2015-05-13 22:33 Daniel Hahler
  2015-05-14  3:46 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Hahler @ 2015-05-13 22:33 UTC (permalink / raw)
  To: zsh-users

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I am looking for a function to get the visible length of a string, which would
remove any escape codes for colors etc.

I've found the following via http://stackoverflow.com/a/10564427/15690, but it
throws an error for '`foo`': zsh: command not found: foo

I could escape these probably, but then '$(foo)' would be next etc.

I've tried to throw in "setopt localoptions nopromptsubst", but that appears to not
expand the ANSI color codes anymore.

Hopefully there's a better way?

Maybe there could be some extension to "$#" to make this simpler?


    get_visible_length() {
        local zero='%([BSUbfksu]|([FB]|){*})'
        print ${#${(S%%)1//$~zero}}
    }


Regards,
Daniel.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iD8DBQFVU9EofAK/hT/mPgARArR2AJ99+HEnfnwTvWJrYrly4PPTJj01pACgzwtn
CSlCs3R+9xdGVw1KygyFvoc=
=2I9V
-----END PGP SIGNATURE-----


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

* Re: Get visible length of a string (removing escape sequences)
  2015-05-13 22:33 Get visible length of a string (removing escape sequences) Daniel Hahler
@ 2015-05-14  3:46 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2015-05-14  3:46 UTC (permalink / raw)
  To: zsh-users

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.


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

end of thread, other threads:[~2015-05-14  3:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-13 22:33 Get visible length of a string (removing escape sequences) Daniel Hahler
2015-05-14  3:46 ` Bart Schaefer

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