Use unicode_width instead of len, improve in_box.

master
voussoir 2021-12-20 16:45:47 -08:00
parent ca4b78c654
commit 0a0bbae88c
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 18 additions and 6 deletions

View File

@ -12,6 +12,7 @@ these functions.
import shutil
from voussoirkit import dotdict
from voussoirkit import stringtools
SINGLE_BOX = dotdict.DotDict(
upper_left='',
@ -36,7 +37,7 @@ def equals_header(text):
Sample text
===========
'''
return text + '\n' + ('=' * len(text))
return text + '\n' + ('=' * stringtools.unicode_width(text))
def in_box(text, *, boxchars=SINGLE_BOX, title=''):
'''
@ -50,16 +51,27 @@ def in_box(text, *, boxchars=SINGLE_BOX, title=''):
There is breaking news about an urgent topic
and you'll never guess what it is │
This function does not perform text wrapping. Wrap your text before putting
it in the box.
'''
lines = text.splitlines()
longest_line = max(max(len(line) for line in lines), len(title))
top = title + boxchars.top * (longest_line - len(title))
bottom = boxchars.top * longest_line
widths = {line: stringtools.unicode_width(line) for line in lines}
if len(widths) == 0:
longest_line = 0
else:
longest_line = max(widths.values())
box_width = max(longest_line, stringtools.unicode_width(title))
top = title + boxchars.top * (box_width - stringtools.unicode_width(title))
bottom = boxchars.top * box_width
new_lines = []
new_lines.append(boxchars.upper_left + top + boxchars.upper_right)
for line in lines:
new_lines.append(boxchars.side + line.ljust(longest_line, ' ') + boxchars.side)
space_needed = box_width - widths[line]
space = ' ' * space_needed
new_lines.append(f'{boxchars.side}{line}{space}{boxchars.side}')
new_lines.append(boxchars.lower_left + bottom + boxchars.lower_right)
return '\n'.join(new_lines)
@ -69,6 +81,6 @@ def solid_hash_header(text):
'''
cli_width = shutil.get_terminal_size()[0]
# One left hash, space, and space after text.
right_count = cli_width - (len(text) + 3)
right_count = cli_width - (stringtools.unicode_width(text) + 3)
right_hashes = '#' * right_count
return f'# {text} {right_hashes}'