failed WRONG_FIX wrong_fix · 16 tool calls · 302 s · Textualize/rich
Fix double-width characters disappearing when wrapping ## Type of changes - [x] Bug fix - [ ] New feature - [ ] Documentation / docstrings - [ ] Tests - [ ] Other ## Checklist - [x] I've run the latest [black](https://github.com/psf/black) with default args on new code. - [x] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate. - [x] I've added tests for new code. - [x] I accept that @willmcgugan may be pedantic in the code review. ## Description Update wrapping logic to fix issues with CJK charcters disappearing when the "fold" location sat *within* a double-width character. Ensure we retain browser logic of: > if there is no space on the current line, move to a new line, and if theres not enough space on the entire new line, fold the text over multiple lines at appropriate locations. Adds some additional tests and docstrings, documentation etc. Fixes #3176 The wrapping process is overall still quite simple and doesn't match the browser in many cases. For example, wrapping does not consider punctuation (lines can begin with punctuation), and whitespace is handled differently (but practically speaking it seems sensible).
| # | Tool | Arguments | Result |
|---|---|---|---|
| 1 | run_command | {
"command": "grep -rn \"wrap\" rich | grep \"CJK\" -A 5 -B 5 | head -20"
} | {"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0} |
| 2 | run_command | {
"command": "grep -rn \"wrap\" rich | head -20"
} | {"status": "ok", "stdout": "rich/logging.py:41: tracebacks_word_wrap (bool, optional): Enable word wrapping of long tracebacks lines. Defaults to True.\nrich/lofull{"status": "ok", "stdout": "rich/logging.py:41: tracebacks_word_wrap (bool, optional): Enable word wrapping of long tracebacks lines. Defaults to True.\nrich/logging.py:79: tracebacks_word_wrap: bool = True,\nrich/logging.py:104: self.tracebacks_word_wrap = tracebacks_word_wrap\nrich/logging.py:145: word_wrap=self.tracebacks_word_wrap,\nrich/tree.py:207: table.add_column(\"Released\", style=\"cyan\", no_wrap=True)\nrich/console.py:10:from functools import wraps\nrich/console.py:147: no_wrap: Optional[bool] = False\nrich/console.py:148: \"\"\"Disable wrapping for text.\"\"\"\nrich/console.py:178: no_wrap: Union[Optional[bool], NoChange] = NO_CHANGE,\nrich/console.py:195: if not isinstance(no_wrap, NoChange):\nrich/console.py:196: options.no_wrap = no_wrap\nrich/console.py:508: @wraps(method)\nrich/console.py:603: soft_wrap (Optional[bool], optional): Set soft wrap default on print method. Defaults to False.\nrich/console.py:641: soft_wrap: bool = False,\nrich/console.py:706: self.soft_wrap = soft_wrap\nrich/console.py:1601: :meth:`~rich.console.Console.print` won't pretty print, wrap text, or apply markup, but will\nrich/console.py:1618: no_wrap=True,\nrich/console.py:1632: no_wrap: Optional[bool] = None,\nrich/console.py:1639: soft_wrap: Optional[bool] = None,\nrich/console.py:1651: no_wrap (Optional[bool], optional): Disable word wrapping. Defaults to None.\n", "stderr": "", "exit_code": 0} |
| 3 | run_command | {
"command": "grep -rn \"wrap\" rich/text.py | head -20"
} | {"status": "ok", "stdout": "rich/text.py:20:from ._wrap import divide_line\nrich/text.py:125: no_wrap (bool, optional): Disable text wrapping, or None for defaufull{"status": "ok", "stdout": "rich/text.py:20:from ._wrap import divide_line\nrich/text.py:125: no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.\nrich/text.py:136: \"no_wrap\",\nrich/text.py:150: no_wrap: Optional[bool] = None,\nrich/text.py:160: self.no_wrap = no_wrap\nrich/text.py:300: no_wrap: Optional[bool] = None,\nrich/text.py:311: no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.\nrich/text.py:321: no_wrap=no_wrap,\nrich/text.py:362: no_wrap: Optional[bool] = None,\nrich/text.py:374: no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.\nrich/text.py:386: no_wrap=no_wrap,\nrich/text.py:436: no_wrap=self.no_wrap,\nrich/text.py:449: no_wrap=self.no_wrap,\nrich/text.py:694: lines = self.wrap(\nrich/text.py:700: no_wrap=pick_bool(self.no_wrap, options.no_wrap, False),\nrich/text.py:1198: def wrap(\nrich/text.py:1206: no_wrap: Optional[bool] = None,\nrich/text.py:1208: \"\"\"Word wrap the text.\nrich/text.py:1216: no_wrap (bool, optional): Disable wrapping, Defaults to False.\nrich/text.py:1221: wrap_justify = justify or self.justify or DEFAULT_JUSTIFY\n", "stderr": "", "exit_code": 0} |
| 4 | run_command | {
"command": "ls rich/_wrap.py"
} | {"status": "ok", "stdout": "rich/_wrap.py\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "rich/_wrap.py\n", "stderr": "", "exit_code": 0} |
| 5 | run_command | {
"command": "cat rich/_wrap.py"
} | {"status": "ok", "stdout": "import re\nfrom typing import Iterable, List, Tuple\n\nfrom ._loop import loop_last\nfrom .cells import cell_len, chop_cells\n\nre_wfull{"status": "ok", "stdout": "import re\nfrom typing import Iterable, List, Tuple\n\nfrom ._loop import loop_last\nfrom .cells import cell_len, chop_cells\n\nre_word = re.compile(r\"\\s*\\S+\\s*\")\n\n\ndef words(text: str) -> Iterable[Tuple[int, int, str]]:\n position = 0\n word_match = re_word.match(text, position)\n while word_match is not None:\n start, end = word_match.span()\n word = word_match.group(0)\n yield start, end, word\n word_match = re_word.match(text, end)\n\n\ndef divide_line(text: str, width: int, fold: bool = True) -> List[int]:\n divides: List[int] = []\n append = divides.append\n line_position = 0\n _cell_len = cell_len\n for start, _end, word in words(text):\n word_length = _cell_len(word.rstrip())\n if line_position + word_length > width:\n if word_length > width:\n if fold:\n chopped_words = chop_cells(word, max_size=width, position=0)\n for last, line in loop_last(chopped_words):\n if start:\n append(start)\n\n if last:\n line_position = _cell_len(line)\n else:\n start += len(line)\n else:\n if start:\n append(start)\n line_position = _cell_len(word)\n elif line_position and start:\n append(start)\n line_position = _cell_len(word)\n else:\n line_position += _cell_len(word)\n return divides\n\n\nif __name__ == \"__main__\": # pragma: no cover\n from .console import Console\n\n console = Console(width=10)\n console.print(\"12345 abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ 12345\")\n print(chop_cells(\"abcdefghijklmnopqrstuvwxyz\", 10, position=2))\n", "stderr": "", "exit_code": 0} |
| 6 | run_command | {
"command": "cat rich/cells.py"
} | {"status": "ok", "stdout": "import re\nfrom functools import lru_cache\nfrom typing import Callable, List\n\nfrom ._cell_widths import CELL_WIDTHS\n\n# Regex tofull{"status": "ok", "stdout": "import re\nfrom functools import lru_cache\nfrom typing import Callable, List\n\nfrom ._cell_widths import CELL_WIDTHS\n\n# Regex to match sequence of the most common character ranges\n_is_single_cell_widths = re.compile(\"^[\\u0020-\\u006f\\u00a0\\u02ff\\u0370-\\u0482]*$\").match\n\n\n@lru_cache(4096)\ndef cached_cell_len(text: str) -> int:\n \"\"\"Get the number of cells required to display text.\n\n This method always caches, which may use up a lot of memory. It is recommended to use\n `cell_len` over this method.\n\n Args:\n text (str): Text to display.\n\n Returns:\n int: Get the number of cells required to display text.\n \"\"\"\n _get_size = get_character_cell_size\n total_size = sum(_get_size(character) for character in text)\n return total_size\n\n\ndef cell_len(text: str, _cell_len: Callable[[str], int] = cached_cell_len) -> int:\n \"\"\"Get the number of cells required to display text.\n\n Args:\n text (str): Text to display.\n\n Returns:\n int: Get the number of cells required to display text.\n \"\"\"\n if len(text) < 512:\n return _cell_len(text)\n _get_size = get_character_cell_size\n total_size = sum(_get_size(character) for character in text)\n return total_size\n\n\n@lru_cache(maxsize=4096)\ndef get_character_cell_size(character: str) -> int:\n \"\"\"Get the cell size of a character.\n\n Args:\n character (str): A single character.\n\n Returns:\n int: Number of cells (0, 1 or 2) occupied by that character.\n \"\"\"\n return _get_codepoint_cell_size(ord(character))\n\n\n@lru_cache(maxsize=4096)\ndef _get_codepoint_cell_size(codepoint: int) -> int:\n \"\"\"Get the cell size of a character.\n\n Args:\n codepoint (int): Codepoint of a character.\n\n Returns:\n int: Number of cells (0, 1 or 2) occupied by that character.\n \"\"\"\n\n _table = CELL_WIDTHS\n lower_bound = 0\n uppe |
| 7 | run_command | {
"command": "python3 - <<'PY'\nfrom rich.cells import chop_cells, cell_len\ntext = \"你好world\" # 你好 are 2 cells each, world are 1 cell each. Total = 2*2 + 5 = 9\nprint(f\"Text: {text}, cell_len: {cell_len(text)}\")\n# If width is 3, '你好' (4 cells) should be chopped.\n# '你好' should be split into '你' (2) and '好' (2).\n# But wait, if width is 3, '你' fits, '好' doesn't.\nprint(f\"Chop 3: {chop_cells(text, 3)}\")\n# If width is 2, '你' fits, '好' doesn't.\nprint(f\"Chop 2: {chop_cells(text, 2)}\")\nPY\n"
} | {"status": "ok", "stdout": "Text: \u4f60\u597dworld, cell_len: 9\nChop 3: ['dlr', 'ow', '\u597d', '\u4f60']\nChop 2: ['dl', 'ro', 'w', '\u597d', '\u4f60']\n", "full{"status": "ok", "stdout": "Text: \u4f60\u597dworld, cell_len: 9\nChop 3: ['dlr', 'ow', '\u597d', '\u4f60']\nChop 2: ['dl', 'ro', 'w', '\u597d', '\u4f60']\n", "stderr": "", "exit_code": 0} |
| 8 | run_command | {
"command": "python3 - <<'PY'\nfrom rich._wrap import divide_line\nfrom rich.console import Console\nfrom rich.text import Text\n\ndef test_wrap(text, width):\n divides = divide_line(text, width)\n print(f\"Text: {text}, Width: {width}, Divides: {divides}\")\n # divide_line returns indices where the line should be split.\n # Let's see how it's used in Text.wrap\n # Text.wrap uses divide_line to split the text into lines.\n # If divides = [5], then text[:5] and text[5:] are the lines.\n\ntest_wrap(\"你好world\", 3)\ntest_wrap(\"你好world\", 2)\ntest_wrap(\"你好world\", 1)\nPY\n"
} | {"status": "ok", "stdout": "Text: \u4f60\u597dworld, Width: 3, Divides: [3, 5, 6]\nText: \u4f60\u597dworld, Width: 2, Divides: [2, 4, 5, 6]\nText: \u4f60\u597dwfull{"status": "ok", "stdout": "Text: \u4f60\u597dworld, Width: 3, Divides: [3, 5, 6]\nText: \u4f60\u597dworld, Width: 2, Divides: [2, 4, 5, 6]\nText: \u4f60\u597dworld, Width: 1, Divides: [1, 2, 3, 4, 5, 6]\n", "stderr": "", "exit_code": 0} |
| 9 | run_command | {
"command": "python3 - <<'PY'\nfrom rich._wrap import divide_line\nfrom rich.console import Console\nfrom rich.text import Text\n\ndef wrap_and_print(text, width):\n t = Text(text)\n lines = t.wrap(width=width)\n print(f\"Width {width}: {lines}\")\n\nwrap_and_print(\"你好world\", 3)\nwrap_and_print(\"你好world\", 2)\nwrap_and_print(\"你好world\", 1)\nPY\n"
} | {"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<stdin>\", line 10, in <module>\n File \"<stdin>full{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<stdin>\", line 10, in <module>\n File \"<stdin>\", line 7, in wrap_and_print\nTypeError: Text.wrap() missing 1 required positional argument: 'console'\n", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n File \"<stdin>\", line 10, in <module>\n File \"<stdin>\", line 7, in wrap_and_print\nTypeError: Text.wrap() missing 1 required positional argument: 'console'\n", "exit_code": 1}} |
| 10 | run_command | {
"command": "python3 - <<'PY'\nfrom rich.console import Console\nfrom rich.text import Text\n\ndef wrap_and_print(text, width):\n console = Console(width=width)\n t = Text(text)\n lines = t.wrap(console=console)\n print(f\"Width {width}: {lines}\")\n\nwrap_and_print(\"你好world\", 3)\nwrap_and_print(\"你好world\", 2)\nwrap_and_print(\"你好world\", 1)\nPY\n"
} | {"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<stdin>\", line 10, in <module>\n File \"<stdin>full{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<stdin>\", line 10, in <module>\n File \"<stdin>\", line 7, in wrap_and_print\nTypeError: Text.wrap() missing 1 required positional argument: 'width'\n", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n File \"<stdin>\", line 10, in <module>\n File \"<stdin>\", line 7, in wrap_and_print\nTypeError: Text.wrap() missing 1 required positional argument: 'width'\n", "exit_code": 1}} |
| 11 | run_command | {
"command": "python3 - <<'PY'\nfrom rich.console import Console\nfrom rich.text import Text\n\ndef wrap_and_print(text, width):\n console = Console(width=width)\n t = Text(text)\n lines = t.wrap(console=console, width=width)\n print(f\"Width {width}: {lines}\")\n\nwrap_and_print(\"你好world\", 3)\nwrap_and_print(\"你好world\", 2)\nwrap_and_print(\"你好world\", 1)\nPY\n"
} | {"status": "ok", "stdout": "Width 3: Lines([<text '\u4f60 ' []>, <text 'or' []>, <text 'l' []>, <text 'd' []>])\nWidth 2: Lines([<text '\u4f60' []>, <text 'wo' full{"status": "ok", "stdout": "Width 3: Lines([<text '\u4f60 ' []>, <text 'or' []>, <text 'l' []>, <text 'd' []>])\nWidth 2: Lines([<text '\u4f60' []>, <text 'wo' []>, <text 'r' []>, <text 'l' []>, <text 'd' []>])\nWidth 1: Lines([<text ' ' []>, <text ' ' []>, <text 'w' []>, <text 'o' []>, <text 'r' []>, <text 'l' []>, <text 'd' []>])\n", "stderr": "", "exit_code": 0} |
| 12 | run_command | {
"command": "python3 - <<'PY'\nfrom rich.console import Console\nfrom rich.text import Text\n\ndef wrap_and_print(text, width):\n console = Console(width=width)\n t = Text(text)\n lines = t.wrap(console=console, width=width)\n print(f\"Width {width}: {lines}\")\n\nwrap_and_print(\"你好world\", 3)\nPY\n"
} | {"status": "ok", "stdout": "Width 3: Lines([<text '\u4f60 ' []>, <text 'or' []>, <text 'l' []>, <text 'd' []>])\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Width 3: Lines([<text '\u4f60 ' []>, <text 'or' []>, <text 'l' []>, <text 'd' []>])\n", "stderr": "", "exit_code": 0} |
| 13 | run_command | {
"command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"rich/_wrap.py\")\ns = p.read_text()\nold = \" for last, line in loop_last(chopped_words):\"\nnew = \" for last, line in loop_last(reversed(chopped_words)):\"\nassert s.count(old) == 1, (\"old text found this many times:\", s.count(old))\nt = s.replace(old, new)\np.write_text(t)\nPY\n"
} | {"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0} |
| 14 | run_command | {
"command": "python3 - <<'PY'\nfrom rich._wrap import divide_line\ntext = \"你好world\"\nwidth = 3\ndivides = divide_line(text, width)\nprint(f\"Divides: {divides}\")\nassert divides == [1, 2, 4]\nPY\n"
} | {"status": "ok", "stdout": "Divides: [1, 2, 4]\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Divides: [1, 2, 4]\n", "stderr": "", "exit_code": 0} |
| 15 | run_command | {
"command": "python3 - <<'PY'\nfrom rich._wrap import divide_line\ntext = \"A 你好world\"\nwidth = 3\ndivides = divide_line(text, width)\nprint(f\"Divides: {divides}\")\nassert divides == [2, 3, 4, 6]\nPY\n"
} | {"status": "ok", "stdout": "Divides: [2, 3, 4, 6]\n", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "Divides: [2, 3, 4, 6]\n", "stderr": "", "exit_code": 0} |
| 16 | run_command | {
"command": "git diff"
} | {"status": "ok", "stdout": "diff --git a/rich/_wrap.py b/rich/_wrap.py\nindex c45f193f..036e0203 100644\n--- a/rich/_wrap.py\n+++ b/rich/_wrap.py\n@@ -28,7 +28,full{"status": "ok", "stdout": "diff --git a/rich/_wrap.py b/rich/_wrap.py\nindex c45f193f..036e0203 100644\n--- a/rich/_wrap.py\n+++ b/rich/_wrap.py\n@@ -28,7 +28,7 @@ def divide_line(text: str, width: int, fold: bool = True) -> List[int]:\n if word_length > width:\n if fold:\n chopped_words = chop_cells(word, max_size=width, position=0)\n- for last, line in loop_last(chopped_words):\n+ for last, line in loop_last(reversed(chopped_words)):\n if start:\n append(start)\n \n", "stderr": "", "exit_code": 0} |
| 17 | submit_patch | {} | {"status": "ok", "patch_size": 570, "files_changed": 1}full{"status": "ok", "patch_size": 570, "files_changed": 1} |
diff --git a/rich/_wrap.py b/rich/_wrap.py
index c45f193f..036e0203 100644
--- a/rich/_wrap.py
+++ b/rich/_wrap.py
@@ -28,7 +28,7 @@ def divide_line(text: str, width: int, fold: bool = True) -> List[int]:
if word_length > width:
if fold:
chopped_words = chop_cells(word, max_size=width, position=0)
- for last, line in loop_last(chopped_words):
+ for last, line in loop_last(reversed(chopped_words)):
if start:
append(start)
....F
=================================== FAILURES ===================================
_______________________________ test_chop_cells ________________________________
def test_chop_cells():
"""Simple example of splitting cells into lines of width 3."""
text = "abcdefghijk"
> assert chop_cells(text, 3) == ["abc", "def", "ghi", "jk"]
E AssertionError: assert ['kji', 'hgf', 'edc', 'ba'] == ['abc', 'def', 'ghi', 'jk']
E
E At index 0 diff: 'kji' != 'abc'
E Use -v to get more diff
tests/test_cells.py:49: AssertionError
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed, 4 passed in 0.18s