From 4ff2ac5f96c330c3dac923b6c373c62c415854bb Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:16:03 -0600 Subject: [PATCH 1/2] fix(table): measure cell width in terminal cells, not characters Table cells containing wide characters (CJK, emoji) rendered with misaligned borders: widths were measured with len(), counting every character as one column, while a wide character occupies two terminal cells. Rows with more wide characters pushed the right border further out than rows without them. Measure with the wcswidth() helper from cleo._utils instead, falling back to the character count for control characters, which have no well-defined display width. The padding arithmetic is unaffected since padding characters are one cell wide each. --- src/cleo/ui/table.py | 19 ++++++++++++++++--- tests/ui/test_table.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/cleo/ui/table.py b/src/cleo/ui/table.py index 72f5c249..a57b1745 100644 --- a/src/cleo/ui/table.py +++ b/src/cleo/ui/table.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/tests/ui/test_table.py b/tests/ui/test_table.py index d10b60c2..c3042df2 100644 --- a/tests/ui/test_table.py +++ b/tests/ui/test_table.py @@ -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" + ) From 8b9c66cce8e7286f3cbe3243fa5096b1393987fe Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:16:41 -0600 Subject: [PATCH 2/2] add news fragment --- news/558.bugfix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/558.bugfix.md diff --git a/news/558.bugfix.md b/news/558.bugfix.md new file mode 100644 index 00000000..556aec37 --- /dev/null +++ b/news/558.bugfix.md @@ -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.