Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/558.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed table rendering misaligning borders for cells containing wide characters (CJK, emoji): widths are now measured in terminal cells instead of characters.
19 changes: 16 additions & 3 deletions src/cleo/ui/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import TYPE_CHECKING
from typing import cast

from cleo._utils import wcswidth
from cleo.formatters.formatter import Formatter
from cleo.io.outputs.output import Output
from cleo.ui.table_cell import TableCell
Expand All @@ -27,6 +28,18 @@
Header = Row


def _visual_width(text: str) -> int:
"""Determine the number of terminal cells needed to display a text.

Falls back to the character count for texts with control characters,
which have no well-defined display width.
"""
width = wcswidth(text)
if width < 0:
return len(text)
return width


class Table:
SEPARATOR_TOP: int = 0
SEPARATOR_TOP_BOTTOM: int = 1
Expand Down Expand Up @@ -291,7 +304,7 @@ def _render_row_separator(
if title is not None:
assert title_format is not None
formatted_title = title_format.format(title)
title_length = len(self._io.remove_format(formatted_title))
title_length = _visual_width(self._io.remove_format(formatted_title))
markup_length = len(markup)
limit = markup_length - 4

Expand Down Expand Up @@ -370,7 +383,7 @@ def _render_cell(self, row: Row, column: int, cell_format: str) -> str:
if isinstance(cell, TableSeparator):
return style.border_format.format(style.border_chars[2] * width)

width += len(cell) - len(self._io.remove_format(cell))
width += len(cell) - _visual_width(self._io.remove_format(cell))
content = style.cell_row_content_format.format(cell)

pad = style.pad
Expand Down Expand Up @@ -666,7 +679,7 @@ def _get_cell_width(self, row: Row, column: int) -> int:

with suppress(IndexError):
cell = row[column]
cell_width = len(self._io.remove_format(cell))
cell_width = _visual_width(self._io.remove_format(cell))

column_width = self._column_widths.get(column, 0)
cell_width = max(cell_width, column_width)
Expand Down
29 changes: 29 additions & 0 deletions tests/ui/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,32 @@ def test_style_for_side_effects(io: BufferedIO) -> None:
output2 = io.fetch_output()

assert output1 != output2


def test_render_cjk_and_wide_chars_align(io: BufferedIO) -> None:
# Wide characters (CJK, emoji) occupy two terminal cells, so measuring
# them as single characters used to push the right border out of
# alignment.
table = Table(io)
table.set_rows([["你好世界", "x"], ["c", "dd"]])
table.render()

assert io.fetch_output() == (
"+----------+----+\n| 你好世界 | x |\n| c | dd |\n+----------+----+\n"
)


def test_render_wide_chars_in_headers_align(io: BufferedIO) -> None:
table = Table(io)
table.set_headers(["Name", "値"])
table.set_rows([["ab", "日本"], ["cde", "f"]])
table.render()

assert io.fetch_output() == (
"+------+------+\n"
"| Name | 値 |\n"
"+------+------+\n"
"| ab | 日本 |\n"
"| cde | f |\n"
"+------+------+\n"
)
Loading