comparison mercurial/util.py @ 7696:0895f95451e7

simplify colwidth a bit
author Matt Mackall <mpm@selenic.com>
date Fri, 23 Jan 2009 14:51:09 -0600
parents deec6628e62b
children 3793802ea41b
comparison
equal deleted inserted replaced
7695:deec6628e62b 7696:0895f95451e7
136 sub = s[max(0, inst.start-10):inst.start+10] 136 sub = s[max(0, inst.start-10):inst.start+10]
137 raise Abort("decoding near '%s': %s!" % (sub, inst)) 137 raise Abort("decoding near '%s': %s!" % (sub, inst))
138 except LookupError, k: 138 except LookupError, k:
139 raise Abort(_("%s, please check your locale settings") % k) 139 raise Abort(_("%s, please check your locale settings") % k)
140 140
141 _colwidth = None
142 def colwidth(s): 141 def colwidth(s):
143 """Find the column width of string to display.""" 142 "Find the column width of a UTF-8 string for display"
144 global _colwidth 143 d = s.decode(_encoding, 'replace')
145 if _colwidth is None: 144 if hasattr(unicodedata, 'east_asian_width'):
146 if hasattr(unicodedata, 'east_asian_width'): 145 w = unicodedata.east_asian_width
147 _colwidth = lambda s: sum([unicodedata.east_asian_width(c) in 'WF' 146 return sum([w(c) in 'WF' and 2 or 1 for c in d])
148 and 2 or 1 for c in s]) 147 return len(d)
149 else:
150 _colwidth = len
151 return _colwidth(s.decode(_encoding, "replace"))
152 148
153 def version(): 149 def version():
154 """Return version information if available.""" 150 """Return version information if available."""
155 try: 151 try:
156 import __version__ 152 import __version__