Fix text truncation for full-width characters (#4278)

* Fix text truncation for full-width characters

* Add test for TruncatedText
This commit is contained in:
kjy00302 2022-04-06 18:17:55 +09:00 committed by GitHub
parent 6d67a405a9
commit 0454f63e98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 3 deletions

View File

@ -18,6 +18,8 @@
([#5217](https://github.com/mitmproxy/mitmproxy/issues/5217), @randomstuff) ([#5217](https://github.com/mitmproxy/mitmproxy/issues/5217), @randomstuff)
* Improve cut addon to better handle binary contents * Improve cut addon to better handle binary contents
([#3965](https://github.com/mitmproxy/mitmproxy/issues/3965), @mhils) ([#3965](https://github.com/mitmproxy/mitmproxy/issues/3965), @mhils)
* Fix text truncation for full-width characters
([#4278](https://github.com/mitmproxy/mitmproxy/issues/4278), @kjy00302)
## 19 March 2022: mitmproxy 8.0.0 ## 19 March 2022: mitmproxy 8.0.0

View File

@ -191,7 +191,7 @@ class TruncatedText(urwid.Widget):
text = text[::-1] text = text[::-1]
attr = attr[::-1] attr = attr[::-1]
text_len = len(text) # TODO: unicode? text_len = urwid.util.calc_width(text, 0, len(text))
if size is not None and len(size) > 0: if size is not None and len(size) > 0:
width = size[0] width = size[0]
else: else:
@ -206,8 +206,10 @@ class TruncatedText(urwid.Widget):
c_text = text c_text = text
c_attr = attr c_attr = attr
else: else:
visible_len = width - len(SYMBOL_ELLIPSIS) trim = urwid.util.calc_trim_text(text, 0, width - 1, 0, width - 1)
visible_text = text[0:visible_len] visible_text = text[0:trim[1]]
if trim[3] == 1:
visible_text += ' '
c_text = visible_text + SYMBOL_ELLIPSIS c_text = visible_text + SYMBOL_ELLIPSIS
c_attr = (urwid.util.rle_subseg(attr, 0, len(visible_text.encode())) + c_attr = (urwid.util.rle_subseg(attr, 0, len(visible_text.encode())) +
[('focus', len(SYMBOL_ELLIPSIS.encode()))]) [('focus', len(SYMBOL_ELLIPSIS.encode()))])

View File

@ -36,3 +36,10 @@ def test_format_keyvals():
("aa", wrapped) ("aa", wrapped)
] ]
) )
def test_truncated_text():
half_width_text = common.TruncatedText("Half-width", [])
full_width_text = common.TruncatedText("", [])
assert half_width_text.render((10,))
assert full_width_text.render((10,))