def string_width(string):
"""Get the visible width of a unicode string.
Some CJK unicode characters are more than one byte unlike ASCII and latin unicode characters.
From: https://github.com/Robpol86/terminaltables/pull/9
:param str string: String to measure.
:return: String's width.
:rtype: int
"""
# Colorclass instance.
if hasattr(string, 'value_no_colors'):
string = string.value_no_colors
# Convert to unicode.
try:
decoded = string.decode('u8')
except (AttributeError, UnicodeEncodeError):
decoded = string
width = 0
for char in decoded:
if unicodedata.east_asian_width(char) in ('F', 'W'):
width += 2
else:
width += 1
return width
width_and_alignment.py 文件源码
python
阅读 22
收藏 0
点赞 0
评论 0
评论列表
文章目录