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

Get visible length of a string (removing escape sequences)



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



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