changeset 7696:0895f95451e7

simplify colwidth a bit
author Matt Mackall <mpm@selenic.com>
date Fri, 23 Jan 2009 14:51:09 -0600
parents deec6628e62b
children 9bb33d4610b6
files mercurial/util.py
diffstat 1 files changed, 6 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Wed Jan 21 20:29:47 2009 +0900
+++ b/mercurial/util.py	Fri Jan 23 14:51:09 2009 -0600
@@ -138,17 +138,13 @@
     except LookupError, k:
         raise Abort(_("%s, please check your locale settings") % k)
 
-_colwidth = None
 def colwidth(s):
-    """Find the column width of string to display."""
-    global _colwidth
-    if _colwidth is None:
-        if hasattr(unicodedata, 'east_asian_width'):
-            _colwidth = lambda s: sum([unicodedata.east_asian_width(c) in 'WF'
-                                       and 2 or 1 for c in s])
-        else:
-            _colwidth = len
-    return _colwidth(s.decode(_encoding, "replace"))
+    "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)
 
 def version():
     """Return version information if available."""