Skip to content

Commit

Permalink
fix:TextEmbed: Fix text width computation
Browse files Browse the repository at this point in the history
- Fix: Use `urwid.calc_width()` instead of `urwid.Text.pack()[0]` to
  compute the screen column width of text.

  - Additionally results in a performance improvement.

Avoids buggy behaviour of `Text.pack()`
(see urwid/urwid#924) and
fixes ihabunek/toot#499.
  • Loading branch information
AnonymouX47 committed Aug 31, 2024
1 parent 269b4f0 commit f8c83af
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/urwidgets/text_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ def _uw_update_widget_start_pos(self) -> None:
find_placeholders = type(self)._UW_PLACEHOLDER_PATTERN.finditer
embedded_iter = iter(self._uw_embedded)
self._uw_embedded = [
# Using `Text.pack()` instead of `match.start()` directly to account for
# wide characters
(widget, width, urwid.Text(line[: match.start()]).pack()[0])
# Using `calc_width()` instead of `match.start()` directly to account for
# wide and zero-width characters
(widget, width, urwid.calc_width(line[: match.start()], 0, match.start()))
for line in super().get_text()[0].splitlines()
for match, (widget, width, _) in zip(find_placeholders(line), embedded_iter)
]
Expand Down Expand Up @@ -480,8 +480,9 @@ def _uw_embed(
if len(part) != width:
tail = (width - len(part), canv)
else:
# Should't use `len(part)` because of wide characters
maxcol = urwid.Text(part).pack()[0]
# Using `calc_width()` instead of `len(part)` directly to account for
# wide and zero-width characters
maxcol = urwid.calc_width(part, 0, len(part))
canv = urwid.CompositeCanvas(line_canv)
canv.pad_trim_left_right(-line_index, 0)
canvases.append((canv, None, focus, maxcol))
Expand Down

0 comments on commit f8c83af

Please sign in to comment.