我想格式化字符串以使用curses
显示它们。
为此,如果行太短,我想添加空格,如果它太长则剪切它并添加延续字符。
我的问题是计算终端中字符串的实际宽度。在我的字符串中,我可以拥有不会被显示的字符和其他长度为2的字符(并且打印为正方形)。因此,函数len
不是正确的候选者。
要解决此问题,我尝试使用unicodedate.category
来过滤字符:
removed_category = ('Mn', 'So')
string = ''.join(char for char in string \
if unicodedata.category(char) not in removed_category)
但它并不适用于所有情况。例如,' S' S'被检测为大写字母但在终端中它的宽度不是一个。 而且,它很慢而且不是很优雅。
我还尝试string.printable
删除了可以在我的终端中显示的字符。
编辑(因为问题已经结束)
基于unicodedata.east_asian_width
的解决方案不适用于零宽度字符。如果我们将其与unicodedata.category
结合使用,它似乎有效:
def stringWidth(string):
width = 0
for c in string:
# For zero-width characters
if unicodedata.category(c)[0] in ('M', 'C'):
continue
w = unicodedata.east_asian_width(c)
if w in ('N', 'Na', 'H', 'A'):
width += 1
else:
width += 2
return width
根据您的文字,您可能需要为零宽度字符添加其他类别。