Fix string length calculate for double byte characters
This commit is contained in:
@@ -383,11 +383,18 @@ def shortenString(string, width, placeholder='..'):
|
|||||||
string_len_cjk = stringLenCJK(str(string))
|
string_len_cjk = stringLenCJK(str(string))
|
||||||
# if double byte width is too big
|
# if double byte width is too big
|
||||||
if string_len_cjk > width:
|
if string_len_cjk > width:
|
||||||
# substract the place holder
|
# set current length and output string
|
||||||
# subtract difference between double byte and character lenght
|
cur_len = 0
|
||||||
width -= ((string_len_cjk - len(str(string))) + len(placeholder))
|
out_string = ''
|
||||||
# return string width new width
|
# loop through each character
|
||||||
return "{}{}".format(str(string)[:width], placeholder)
|
for char in str(string):
|
||||||
|
# set the current length if we add the character
|
||||||
|
cur_len += 2 if unicodedata.east_asian_width(char) in "WF" else 1
|
||||||
|
# if the new length is smaller than the output length to shorten too add the char
|
||||||
|
if cur_len <= (width - len(placeholder)):
|
||||||
|
out_string += char
|
||||||
|
# return string with new width and placeholder
|
||||||
|
return "{}{}".format(out_string, placeholder)
|
||||||
else:
|
else:
|
||||||
return str(string)
|
return str(string)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user