##// END OF EJS Templates
util: delay loading of textwrap
Matt Mackall -
r13316:d119403f default
parent child Browse files
Show More
@@ -1391,48 +1391,48 b' 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):
1426 space_left = max(width - cur_len, 1)
1427
1425
1428 if self.break_long_words:
1426 if self.break_long_words:
1429 cut, res = self._cutdown(reversed_chunks[-1], space_left)
1427 cut, res = self._cutdown(reversed_chunks[-1], space_left)
1430 cur_line.append(cut)
1428 cur_line.append(cut)
1431 reversed_chunks[-1] = res
1429 reversed_chunks[-1] = res
1432 elif not cur_line:
1430 elif not cur_line:
1433 cur_line.append(reversed_chunks.pop())
1431 cur_line.append(reversed_chunks.pop())
1434
1432
1435 #### naming convention of above implementation follows 'textwrap' module
1433 global MBTextWrapper
1434 MBTextWrapper = tw
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))
General Comments 0
You need to be logged in to leave comments. Login now