termstr: Handle strings already containing zero-width chars
This commit is contained in:
parent
f0159eb6ff
commit
766956025d
2 changed files with 17 additions and 3 deletions
|
|
@ -173,8 +173,15 @@ _ZERO_WIDTH_SPACE = "\u200b"
|
||||||
|
|
||||||
|
|
||||||
def _pad_wide_chars(str_):
|
def _pad_wide_chars(str_):
|
||||||
padded_str = "".join(f"{char}{_ZERO_WIDTH_SPACE}"
|
parts = []
|
||||||
if cwcwidth.wcwidth(char) == 2 else char for char in str_)
|
last_width = None
|
||||||
|
for char in str_:
|
||||||
|
width = cwcwidth.wcwidth(char)
|
||||||
|
parts.append(f"{_ZERO_WIDTH_SPACE}{char}" if width != 0 and last_width == 2 else char)
|
||||||
|
last_width = width
|
||||||
|
padded_str = "".join(parts)
|
||||||
|
if len(padded_str) > 0 and cwcwidth.wcwidth(padded_str[-1]) == 2:
|
||||||
|
padded_str += _ZERO_WIDTH_SPACE
|
||||||
return str_ if len(padded_str) == len(str_) else padded_str
|
return str_ if len(padded_str) == len(str_) else padded_str
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -307,7 +314,7 @@ class TermStr(collections.UserString):
|
||||||
if len(data) == 0:
|
if len(data) == 0:
|
||||||
result = ""
|
result = ""
|
||||||
else:
|
else:
|
||||||
first_char = " " if data[0] == _ZERO_WIDTH_SPACE else data[0]
|
first_char = " " if cwcwidth.wcwidth(data[0]) == 0 else data[0]
|
||||||
if len(data) == 1:
|
if len(data) == 1:
|
||||||
result = first_char
|
result = first_char
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -116,6 +116,13 @@ class TermStrTests(unittest.TestCase):
|
||||||
self.assertEqual(baz[3:], termstr.TermStr("z"))
|
self.assertEqual(baz[3:], termstr.TermStr("z"))
|
||||||
self.assertEqual(baz[:2], termstr.TermStr("b "))
|
self.assertEqual(baz[:2], termstr.TermStr("b "))
|
||||||
self.assertEqual(baz[2:], termstr.TermStr(" z"))
|
self.assertEqual(baz[2:], termstr.TermStr(" z"))
|
||||||
|
baz = termstr.TermStr("b⭐" + chr(65039) + "z")
|
||||||
|
self.assertEqual(len(baz), 4)
|
||||||
|
self.assertEqual(baz[3:], termstr.TermStr("z"))
|
||||||
|
self.assertEqual(baz[:2], termstr.TermStr("b "))
|
||||||
|
self.assertEqual(baz[2:], termstr.TermStr(" z"))
|
||||||
|
baz = termstr.TermStr("⭐⭐")
|
||||||
|
self.assertEqual(len(baz), 4)
|
||||||
|
|
||||||
def test_from_term(self):
|
def test_from_term(self):
|
||||||
def test_round_trip(term_str):
|
def test_round_trip(term_str):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue