Align Unicode Text In Terminal Window Using Default Monospace Font
I am pulling data from the web and want to align it in a table in a terminal window. I can align the text fine in most cases but when the text contains certain symbols or foreign
Solution 1:
The special behaviour for those particular characters can be identified using the East Asian width property from their Unicode data. Taking the suggestion from Programmatically tell if a Unicode character takes up more than one character space in a terminal and using that value for alignment:
#!/usr/bin/python3import unicodedata
items = "Apple tree", "Banana plant", "Orange 으르", "Goodbye"
values = 100, 200, 300, 400for i, v inzip(items, values):
eawid = len(i) + sum(1for v in i if unicodedata.east_asian_width(v) == 'W')
pad = ' ' * (15 - eawid)
print("%s%s : %-4s" % (i, pad, v))
gives:
Apple tree :100Banana plant :200Orange으르:300Goodbye :400
This may appear misaligned if your browser is using a 1.5-width glyph for those characters; in my terminal, plan
is exactly the same width as 으르
.
Syntax here is Python 3, but the same technique works in 2.7.
Post a Comment for "Align Unicode Text In Terminal Window Using Default Monospace Font"