192 # dict to be exported to other modules |
192 # dict to be exported to other modules |
193 environ = dict((tolocal(k.encode(u'utf-8')), tolocal(v.encode(u'utf-8'))) |
193 environ = dict((tolocal(k.encode(u'utf-8')), tolocal(v.encode(u'utf-8'))) |
194 for k, v in os.environ.items()) # re-exports |
194 for k, v in os.environ.items()) # re-exports |
195 |
195 |
196 # How to treat ambiguous-width characters. Set to 'wide' to treat as wide. |
196 # How to treat ambiguous-width characters. Set to 'wide' to treat as wide. |
197 wide = _sysstr(environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide" |
197 _wide = _sysstr(environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide" |
198 and "WFA" or "WF") |
198 and "WFA" or "WF") |
199 |
199 |
200 def colwidth(s): |
200 def colwidth(s): |
201 "Find the column width of a string for display in the local encoding" |
201 "Find the column width of a string for display in the local encoding" |
202 return ucolwidth(s.decode(_sysstr(encoding), u'replace')) |
202 return ucolwidth(s.decode(_sysstr(encoding), u'replace')) |
203 |
203 |
204 def ucolwidth(d): |
204 def ucolwidth(d): |
205 "Find the column width of a Unicode string for display" |
205 "Find the column width of a Unicode string for display" |
206 eaw = getattr(unicodedata, 'east_asian_width', None) |
206 eaw = getattr(unicodedata, 'east_asian_width', None) |
207 if eaw is not None: |
207 if eaw is not None: |
208 return sum([eaw(c) in wide and 2 or 1 for c in d]) |
208 return sum([eaw(c) in _wide and 2 or 1 for c in d]) |
209 return len(d) |
209 return len(d) |
210 |
210 |
211 def getcols(s, start, c): |
211 def getcols(s, start, c): |
212 '''Use colwidth to find a c-column substring of s starting at byte |
212 '''Use colwidth to find a c-column substring of s starting at byte |
213 index start''' |
213 index start''' |