Merge pull request #260 from asmeurer/double-width

Use urwid's calc_width in make_canvas
This commit is contained in:
Andreas Klöckner 2017-07-05 09:20:52 +02:00 committed by GitHub
commit 23f834e49e
2 changed files with 15 additions and 2 deletions

View File

@ -1,6 +1,6 @@
from __future__ import absolute_import, division, print_function
import urwid
from urwid.util import _target_encoding
from urwid.util import _target_encoding, calc_width
# generic urwid helpers -------------------------------------------------------
@ -14,7 +14,7 @@ def make_canvas(txt, attr, maxcol, fill_attr=None):
# filter out zero-length attrs
line_attr = [(aname, l) for aname, l in line_attr if l > 0]
diff = maxcol - len(line)
diff = maxcol - calc_width(line, 0, len(line))
if diff > 0:
line += " "*diff
line_attr.append((fill_attr, diff))

View File

@ -49,6 +49,19 @@ def test_byte_boundary():
)
assert list(canvas.content()) == [[('var value', None, b'aaaaaa\xc3\xa9')]]
def test_wide_chars():
text = u"data: '中文'"
canvas = make_canvas(
txt=[text],
attr=[[('var label', 6), ('var value', 4)]],
maxcol=47,
)
assert list(canvas.content()) == [[
('var label', None, b'data: '),
('var value', None, u"'中文'".encode('utf-8')),
(None, None, b' '*(47 - 12)), # 10 chars, 2 of which are double width
]]
if __name__ == "__main__":
import sys