Show More
@@ -1328,15 +1328,26 b' def uirepr(s):' | |||||
1328 | #### naming convention of below implementation follows 'textwrap' module |
|
1328 | #### naming convention of below implementation follows 'textwrap' module | |
1329 |
|
1329 | |||
1330 | class MBTextWrapper(textwrap.TextWrapper): |
|
1330 | class MBTextWrapper(textwrap.TextWrapper): | |
|
1331 | """ | |||
|
1332 | Extend TextWrapper for double-width characters. | |||
|
1333 | ||||
|
1334 | Some Asian characters use two terminal columns instead of one. | |||
|
1335 | A good example of this behavior can be seen with u'\u65e5\u672c', | |||
|
1336 | the two Japanese characters for "Japan": | |||
|
1337 | len() returns 2, but when printed to a terminal, they eat 4 columns. | |||
|
1338 | ||||
|
1339 | (Note that this has nothing to do whatsoever with unicode | |||
|
1340 | representation, or encoding of the underlying string) | |||
|
1341 | """ | |||
1331 | def __init__(self, **kwargs): |
|
1342 | def __init__(self, **kwargs): | |
1332 | textwrap.TextWrapper.__init__(self, **kwargs) |
|
1343 | textwrap.TextWrapper.__init__(self, **kwargs) | |
1333 |
|
1344 | |||
1334 | def _cutdown(self, str, space_left): |
|
1345 | def _cutdown(self, str, space_left): | |
1335 | l = 0 |
|
1346 | l = 0 | |
1336 | ucstr = unicode(str, encoding.encoding) |
|
1347 | ucstr = unicode(str, encoding.encoding) | |
1337 | w = unicodedata.east_asian_width |
|
1348 | colwidth = unicodedata.east_asian_width | |
1338 | for i in xrange(len(ucstr)): |
|
1349 | for i in xrange(len(ucstr)): | |
1339 | l += w(ucstr[i]) in 'WFA' and 2 or 1 |
|
1350 | l += colwidth(ucstr[i]) in 'WFA' and 2 or 1 | |
1340 | if space_left < l: |
|
1351 | if space_left < l: | |
1341 | return (ucstr[:i].encode(encoding.encoding), |
|
1352 | return (ucstr[:i].encode(encoding.encoding), | |
1342 | ucstr[i:].encode(encoding.encoding)) |
|
1353 | ucstr[i:].encode(encoding.encoding)) |
General Comments 0
You need to be logged in to leave comments.
Login now