diff mercurial/encoding.py @ 15066:24efa83d81cb stable

i18n: calculate terminal columns by width information of each characters neither number of 'bytes' in any encoding nor 'characters' is appropriate to calculate terminal columns for specified string. this patch modifies MBTextWrapper for: - overriding '_wrap_chunks()' to make it use not built-in 'len()' but 'encoding.colwidth()' for columns of string - fixing '_cutdown()' to make it use 'encoding.colwidth()' instead of local, similar but incorrect implementation this patch also modifies 'encoding.py': - dividing 'colwith()' into 2 pieces: one for calculation columns of specified UNICODE string, and another for rest part of original one. the former is used from MBTextWrapper in 'util.py'. - preventing 'colwidth()' from evaluating HGENCODINGAMBIGUOUS configuration per each invocation: 'unicodedata.east_asian_width' checking is kept intact for reducing startup cost.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Sat, 27 Aug 2011 04:56:12 +0900
parents 1f581a8b1948
children 176882876780
line wrap: on
line diff
--- a/mercurial/encoding.py	Sat Aug 06 23:52:20 2011 +0200
+++ b/mercurial/encoding.py	Sat Aug 27 04:56:12 2011 +0900
@@ -135,16 +135,17 @@
         raise error.Abort("%s, please check your locale settings" % k)
 
 # How to treat ambiguous-width characters. Set to 'wide' to treat as wide.
-ambiguous = os.environ.get("HGENCODINGAMBIGUOUS", "narrow")
+wide = (os.environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide"
+        and "WFA" or "WF")
 
 def colwidth(s):
     "Find the column width of a UTF-8 string for display"
-    d = s.decode(encoding, 'replace')
+    return ucolwidth(s.decode(encoding, 'replace'))
+
+def ucolwidth(d):
+    "Find the column width of a Unicode string for display"
     eaw = getattr(unicodedata, 'east_asian_width', None)
     if eaw is not None:
-        wide = "WF"
-        if ambiguous == "wide":
-            wide = "WFA"
         return sum([eaw(c) in wide and 2 or 1 for c in d])
     return len(d)