[PATCH] Also find correct column width of wide characters

Matt Mackall mpm at selenic.com
Fri Jan 23 14:50:46 CST 2009

On Wed, 2009-01-21 at 20:35 +0900, Shun-ichi Goto wrote:
> # HG changeset patch
> # User Shun-ichi GOTO <shunichi.goto at gmail.com>
> # Date 1232537387 -32400
> # Node ID 432d222ae950d7265f49c7d596303f904f66fc57
> # Parent  06cf09c822c47244be2549c616a1d0b04724d642
> Also find correct column width of wide characters.
> Use unicodedata.east_asian_width() to determine wide/full width
> characters if available. Otherwise, return character count as before.

Thanks. I went ahead and simplified this even further:

def colwidth(s):
    "Find the column width of a UTF-8 string for display"
    d = s.decode(_encoding, 'replace')
    if hasattr(unicodedata, 'east_asian_width'):
        w = unicodedata.east_asian_width
        return sum([w(c) in 'WF' and 2 or 1 for c in d])
    return len(d)

http://selenic.com : development and support for Mercurial and Linux

More information about the Mercurial-devel mailing list