Mercurial > hg
comparison mercurial/util.py @ 13316:d119403fd266
util: delay loading of textwrap
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Fri, 28 Jan 2011 17:02:29 -0600 |
parents | 0c493e5ce8e9 |
children | 2dc7a2a96cfd |
comparison
equal
deleted
inserted
replaced
13315:0d1dca7d2a04 | 13316:d119403fd266 |
---|---|
1389 | 1389 |
1390 def uirepr(s): | 1390 def uirepr(s): |
1391 # Avoid double backslash in Windows path repr() | 1391 # Avoid double backslash in Windows path repr() |
1392 return repr(s).replace('\\\\', '\\') | 1392 return repr(s).replace('\\\\', '\\') |
1393 | 1393 |
1394 #### naming convention of below implementation follows 'textwrap' module | 1394 # delay import of textwrap |
1395 | 1395 def MBTextWrapper(**kwargs): |
1396 class MBTextWrapper(textwrap.TextWrapper): | 1396 class tw(textwrap.TextWrapper): |
1397 """ | 1397 """ |
1398 Extend TextWrapper for double-width characters. | 1398 Extend TextWrapper for double-width characters. |
1399 | 1399 |
1400 Some Asian characters use two terminal columns instead of one. | 1400 Some Asian characters use two terminal columns instead of one. |
1401 A good example of this behavior can be seen with u'\u65e5\u672c', | 1401 A good example of this behavior can be seen with u'\u65e5\u672c', |
1402 the two Japanese characters for "Japan": | 1402 the two Japanese characters for "Japan": |
1403 len() returns 2, but when printed to a terminal, they eat 4 columns. | 1403 len() returns 2, but when printed to a terminal, they eat 4 columns. |
1404 | 1404 |
1405 (Note that this has nothing to do whatsoever with unicode | 1405 (Note that this has nothing to do whatsoever with unicode |
1406 representation, or encoding of the underlying string) | 1406 representation, or encoding of the underlying string) |
1407 """ | 1407 """ |
1408 def __init__(self, **kwargs): | 1408 def __init__(self, **kwargs): |
1409 textwrap.TextWrapper.__init__(self, **kwargs) | 1409 textwrap.TextWrapper.__init__(self, **kwargs) |
1410 | 1410 |
1411 def _cutdown(self, str, space_left): | 1411 def _cutdown(self, str, space_left): |
1412 l = 0 | 1412 l = 0 |
1413 ucstr = unicode(str, encoding.encoding) | 1413 ucstr = unicode(str, encoding.encoding) |
1414 colwidth = unicodedata.east_asian_width | 1414 colwidth = unicodedata.east_asian_width |
1415 for i in xrange(len(ucstr)): | 1415 for i in xrange(len(ucstr)): |
1416 l += colwidth(ucstr[i]) in 'WFA' and 2 or 1 | 1416 l += colwidth(ucstr[i]) in 'WFA' and 2 or 1 |
1417 if space_left < l: | 1417 if space_left < l: |
1418 return (ucstr[:i].encode(encoding.encoding), | 1418 return (ucstr[:i].encode(encoding.encoding), |
1419 ucstr[i:].encode(encoding.encoding)) | 1419 ucstr[i:].encode(encoding.encoding)) |
1420 return str, '' | 1420 return str, '' |
1421 | 1421 |
1422 # ---------------------------------------- | 1422 # overriding of base class |
1423 # overriding of base class | 1423 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): |
1424 | 1424 space_left = max(width - cur_len, 1) |
1425 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): | 1425 |
1426 space_left = max(width - cur_len, 1) | 1426 if self.break_long_words: |
1427 | 1427 cut, res = self._cutdown(reversed_chunks[-1], space_left) |
1428 if self.break_long_words: | 1428 cur_line.append(cut) |
1429 cut, res = self._cutdown(reversed_chunks[-1], space_left) | 1429 reversed_chunks[-1] = res |
1430 cur_line.append(cut) | 1430 elif not cur_line: |
1431 reversed_chunks[-1] = res | 1431 cur_line.append(reversed_chunks.pop()) |
1432 elif not cur_line: | 1432 |
1433 cur_line.append(reversed_chunks.pop()) | 1433 global MBTextWrapper |
1434 | 1434 MBTextWrapper = tw |
1435 #### naming convention of above implementation follows 'textwrap' module | 1435 return tw(**kwargs) |
1436 | 1436 |
1437 def wrap(line, width, initindent='', hangindent=''): | 1437 def wrap(line, width, initindent='', hangindent=''): |
1438 maxindent = max(len(hangindent), len(initindent)) | 1438 maxindent = max(len(hangindent), len(initindent)) |
1439 if width <= maxindent: | 1439 if width <= maxindent: |
1440 # adjust for weird terminal size | 1440 # adjust for weird terminal size |