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. 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" + )