We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[BUG] Text wrapping edge case
Discovered a possible edge case with text wrapping, probably connected to when a "word" is greater than the available width.

Consider the following:

```python
console.print("Hello, World...123 ", width=10)
```

This produces:

```
Hello, Wor
ld...123
```

I think this occurs because "World...123" doesn't fit in to 10 characters, so folds on to the next line.

I would expect there to be an additional new line after the Hello, like this:

```
Hello,
World...12
3
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `rich/_wrap.py`
Content:
```
1 import re
2 from typing import Iterable, List, Tuple
3 
4 from .cells import cell_len, chop_cells
5 from ._loop import loop_last
6 
7 re_word = re.compile(r"\s*\S+\s*")
8 
9 
10 def words(text: str) -> Iterable[Tuple[int, int, str]]:
11     position = 0
12     word_match = re_word.match(text, position)
13     while word_match is not None:
14         start, end = word_match.span()
15         word = word_match.group(0)
16         yield start, end, word
17         word_match = re_word.match(text, end)
18 
19 
20 def divide_line(text: str, width: int, fold: bool = True) -> List[int]:
21     divides: List[int] = []
22     append = divides.append
23     line_position = 0
24     _cell_len = cell_len
25     for start, _end, word in words(text):
26         word_length = _cell_len(word.rstrip())
27         if line_position + word_length > width:
28             if word_length > width:
29                 if fold:
30                     for last, line in loop_last(
31                         chop_cells(word, width, position=line_position)
32                     ):
33                         if last:
34                             line_position = _cell_len(line)
35                         else:
36                             start += len(line)
37                             append(start)
38                 else:
39                     if start:
40                         append(start)
41                     line_position = _cell_len(word)
42             elif line_position and start:
43                 append(start)
44                 line_position = _cell_len(word)
45         else:
46             line_position += _cell_len(word)
47     return divides
48 
49 
50 if __name__ == "__main__":  # pragma: no cover
51     from .console import Console
52 
53     console = Console(width=10)
54     console.print("12345 abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ 12345")
55     print(chop_cells("abcdefghijklmnopqrstuvwxyz", 10, position=2))
56
```

Path: `rich/cells.py`
Content:
```
1 import re
2 from functools import lru_cache
3 from typing import Dict, List
4 
5 from ._cell_widths import CELL_WIDTHS
6 from ._lru_cache import LRUCache
7 
8 # Regex to match sequence of the most common character ranges
9 _is_single_cell_widths = re.compile("^[\u0020-\u006f\u00a0\u02ff\u0370-\u0482]*$").match
10 
11 
12 def cell_len(text: str, _cache: Dict[str, int] = LRUCache(1024 * 4)) -> int:
13     """Get the number of cells required to display text.
14 
15     Args:
16         text (str): Text to display.
17 
18     Returns:
19         int: Get the number of cells required to display text.
20     """
21     cached_result = _cache.get(text, None)
22     if cached_result is not None:
23         return cached_result
24 
25     _get_size = get_character_cell_size
26     total_size = sum(_get_size(character) for character in text)
27     if len(text) <= 512:
28         _cache[text] = total_size
29     return total_size
30 
31 
32 @lru_cache(maxsize=4096)
33 def get_character_cell_size(character: str) -> int:
34     """Get the cell size of a character.
35 
36     Args:
37         character (str): A single character.
38 
39     Returns:
40         int: Number of cells (0, 1 or 2) occupied by that character.
41     """
42     return _get_codepoint_cell_size(ord(character))
43 
44 
45 @lru_cache(maxsize=4096)
46 def _get_codepoint_cell_size(codepoint: int) -> int:
47     """Get the cell size of a character.
48 
49     Args:
50         character (str): A single character.
51 
52     Returns:
53         int: Number of cells (0, 1 or 2) occupied by that character.
54     """
55 
56     _table = CELL_WIDTHS
57     lower_bound = 0
58     upper_bound = len(_table) - 1
59     index = (lower_bound + upper_bound) // 2
60     while True:
61         start, end, width = _table[index]
62         if codepoint < start:
63             upper_bound = index - 1
64         elif codepoint > end:
65             lower_bound = index + 1
66         else:
67             return 0 if width == -1 else width
68         if upper_bound < lower_bound:
69             break
70         index = (lower_bound + upper_bound) // 2
71     return 1
72 
73 
74 def set_cell_size(text: str, total: int) -> str:
75     """Set the length of a string to fit within given number of cells."""
76 
77     if _is_single_cell_widths(text):
78         size = len(text)
79         if size < total:
80             return text + " " * (total - size)
81         return text[:total]
82 
83     if not total:
84         return ""
85     cell_size = cell_len(text)
86     if cell_size == total:
87         return text
88     if cell_size < total:
89         return text + " " * (total - cell_size)
90 
91     start = 0
92     end = len(text)
93 
94     # Binary search until we find the right size
95     while True:
96         pos = (start + end) // 2
97         before = text[: pos + 1]
98         before_len = cell_len(before)
99         if before_len == total + 1 and cell_len(before[-1]) == 2:
100             return before[:-1] + " "
101         if before_len == total:
102             return before
103         if before_len > total:
104             end = pos
105         else:
106             start = pos
107 
108 
109 # TODO: This is inefficient
110 # TODO: This might not work with CWJ type characters
111 def chop_cells(text: str, max_size: int, position: int = 0) -> List[str]:
112     """Break text in to equal (cell) length strings."""
113     _get_character_cell_size = get_character_cell_size
114     characters = [
115         (character, _get_character_cell_size(character)) for character in text
116     ]
117     total_size = position
118     lines: List[List[str]] = [[]]
119     append = lines[-1].append
120 
121     for character, size in reversed(characters):
122         if total_size + size > max_size:
123             lines.append([character])
124             append = lines[-1].append
125             total_size = size
126         else:
127             total_size += size
128             append(character)
129 
130     return ["".join(line) for line in lines]
131 
132 
133 if __name__ == "__main__":  # pragma: no cover
134 
135     print(get_character_cell_size("😽"))
136     for line in chop_cells("""这是对亚洲语言支持的测试。面对模棱两可的想法，拒绝猜测的诱惑。""", 8):
137         print(line)
138     for n in range(80, 1, -1):
139         print(set_cell_size("""这是对亚洲语言支持的测试。面对模棱两可的想法，拒绝猜测的诱惑。""", n) + "|")
140         print("x" * n)
141
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/rich/_wrap.py b/rich/_wrap.py
--- a/rich/_wrap.py
+++ b/rich/_wrap.py
@@ -1,8 +1,8 @@
 import re
 from typing import Iterable, List, Tuple
 
-from .cells import cell_len, chop_cells
 from ._loop import loop_last
+from .cells import cell_len, chop_cells
 
 re_word = re.compile(r"\s*\S+\s*")
 
@@ -27,14 +27,15 @@
         if line_position + word_length > width:
             if word_length > width:
                 if fold:
-                    for last, line in loop_last(
-                        chop_cells(word, width, position=line_position)
-                    ):
+                    chopped_words = chop_cells(word, max_size=width, position=0)
+                    for last, line in loop_last(chopped_words):
+                        if start:
+                            append(start)
+
                         if last:
                             line_position = _cell_len(line)
                         else:
                             start += len(line)
-                            append(start)
                 else:
                     if start:
                         append(start)
diff --git a/rich/cells.py b/rich/cells.py
--- a/rich/cells.py
+++ b/rich/cells.py
@@ -109,7 +109,8 @@
 # TODO: This is inefficient
 # TODO: This might not work with CWJ type characters
 def chop_cells(text: str, max_size: int, position: int = 0) -> List[str]:
-    """Break text in to equal (cell) length strings."""
+    """Break text in to equal (cell) length strings, returning the characters in reverse
+    order"""
     _get_character_cell_size = get_character_cell_size
     characters = [
         (character, _get_character_cell_size(character)) for character in text

{"golden_diff": "diff --git a/rich/_wrap.py b/rich/_wrap.py\n--- a/rich/_wrap.py\n+++ b/rich/_wrap.py\n@@ -1,8 +1,8 @@\n import re\n from typing import Iterable, List, Tuple\n \n-from .cells import cell_len, chop_cells\n from ._loop import loop_last\n+from .cells import cell_len, chop_cells\n \n re_word = re.compile(r\"\\s*\\S+\\s*\")\n \n@@ -27,14 +27,15 @@\n         if line_position + word_length > width:\n             if word_length > width:\n                 if fold:\n-                    for last, line in loop_last(\n-                        chop_cells(word, width, position=line_position)\n-                    ):\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-                            append(start)\n                 else:\n                     if start:\n                         append(start)\ndiff --git a/rich/cells.py b/rich/cells.py\n--- a/rich/cells.py\n+++ b/rich/cells.py\n@@ -109,7 +109,8 @@\n # TODO: This is inefficient\n # TODO: This might not work with CWJ type characters\n def chop_cells(text: str, max_size: int, position: int = 0) -> List[str]:\n-    \"\"\"Break text in to equal (cell) length strings.\"\"\"\n+    \"\"\"Break text in to equal (cell) length strings, returning the characters in reverse\n+    order\"\"\"\n     _get_character_cell_size = get_character_cell_size\n     characters = [\n         (character, _get_character_cell_size(character)) for character in text\n", "issue": "[BUG] Text wrapping edge case\nDiscovered a possible edge case with text wrapping, probably connected to when a \"word\" is greater than the available width.\r\n\r\nConsider the following:\r\n\r\n```python\r\nconsole.print(\"Hello, World...123 \", width=10)\r\n```\r\n\r\nThis produces:\r\n\r\n```\r\nHello, Wor\r\nld...123\r\n```\r\n\r\nI think this occurs because \"World...123\" doesn't fit in to 10 characters, so folds on to the next line.\r\n\r\nI would expect there to be an additional new line after the Hello, like this:\r\n\r\n```\r\nHello,\r\nWorld...12\r\n3\r\n```\n", "before_files": [{"content": "import re\nfrom typing import Iterable, List, Tuple\n\nfrom .cells import cell_len, chop_cells\nfrom ._loop import loop_last\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                    for last, line in loop_last(\n                        chop_cells(word, width, position=line_position)\n                    ):\n                        if last:\n                            line_position = _cell_len(line)\n                        else:\n                            start += len(line)\n                            append(start)\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", "path": "rich/_wrap.py"}, {"content": "import re\nfrom functools import lru_cache\nfrom typing import Dict, List\n\nfrom ._cell_widths import CELL_WIDTHS\nfrom ._lru_cache import LRUCache\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\ndef cell_len(text: str, _cache: Dict[str, int] = LRUCache(1024 * 4)) -> 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    cached_result = _cache.get(text, None)\n    if cached_result is not None:\n        return cached_result\n\n    _get_size = get_character_cell_size\n    total_size = sum(_get_size(character) for character in text)\n    if len(text) <= 512:\n        _cache[text] = total_size\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        character (str): A single 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    upper_bound = len(_table) - 1\n    index = (lower_bound + upper_bound) // 2\n    while True:\n        start, end, width = _table[index]\n        if codepoint < start:\n            upper_bound = index - 1\n        elif codepoint > end:\n            lower_bound = index + 1\n        else:\n            return 0 if width == -1 else width\n        if upper_bound < lower_bound:\n            break\n        index = (lower_bound + upper_bound) // 2\n    return 1\n\n\ndef set_cell_size(text: str, total: int) -> str:\n    \"\"\"Set the length of a string to fit within given number of cells.\"\"\"\n\n    if _is_single_cell_widths(text):\n        size = len(text)\n        if size < total:\n            return text + \" \" * (total - size)\n        return text[:total]\n\n    if not total:\n        return \"\"\n    cell_size = cell_len(text)\n    if cell_size == total:\n        return text\n    if cell_size < total:\n        return text + \" \" * (total - cell_size)\n\n    start = 0\n    end = len(text)\n\n    # Binary search until we find the right size\n    while True:\n        pos = (start + end) // 2\n        before = text[: pos + 1]\n        before_len = cell_len(before)\n        if before_len == total + 1 and cell_len(before[-1]) == 2:\n            return before[:-1] + \" \"\n        if before_len == total:\n            return before\n        if before_len > total:\n            end = pos\n        else:\n            start = pos\n\n\n# TODO: This is inefficient\n# TODO: This might not work with CWJ type characters\ndef chop_cells(text: str, max_size: int, position: int = 0) -> List[str]:\n    \"\"\"Break text in to equal (cell) length strings.\"\"\"\n    _get_character_cell_size = get_character_cell_size\n    characters = [\n        (character, _get_character_cell_size(character)) for character in text\n    ]\n    total_size = position\n    lines: List[List[str]] = [[]]\n    append = lines[-1].append\n\n    for character, size in reversed(characters):\n        if total_size + size > max_size:\n            lines.append([character])\n            append = lines[-1].append\n            total_size = size\n        else:\n            total_size += size\n            append(character)\n\n    return [\"\".join(line) for line in lines]\n\n\nif __name__ == \"__main__\":  # pragma: no cover\n\n    print(get_character_cell_size(\"\ud83d\ude3d\"))\n    for line in chop_cells(\"\"\"\u8fd9\u662f\u5bf9\u4e9a\u6d32\u8bed\u8a00\u652f\u6301\u7684\u6d4b\u8bd5\u3002\u9762\u5bf9\u6a21\u68f1\u4e24\u53ef\u7684\u60f3\u6cd5\uff0c\u62d2\u7edd\u731c\u6d4b\u7684\u8bf1\u60d1\u3002\"\"\", 8):\n        print(line)\n    for n in range(80, 1, -1):\n        print(set_cell_size(\"\"\"\u8fd9\u662f\u5bf9\u4e9a\u6d32\u8bed\u8a00\u652f\u6301\u7684\u6d4b\u8bd5\u3002\u9762\u5bf9\u6a21\u68f1\u4e24\u53ef\u7684\u60f3\u6cd5\uff0c\u62d2\u7edd\u731c\u6d4b\u7684\u8bf1\u60d1\u3002\"\"\", n) + \"|\")\n        print(\"x\" * n)\n", "path": "rich/cells.py"}], "after_files": [{"content": "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", "path": "rich/_wrap.py"}, {"content": "import re\nfrom functools import lru_cache\nfrom typing import Dict, List\n\nfrom ._cell_widths import CELL_WIDTHS\nfrom ._lru_cache import LRUCache\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\ndef cell_len(text: str, _cache: Dict[str, int] = LRUCache(1024 * 4)) -> 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    cached_result = _cache.get(text, None)\n    if cached_result is not None:\n        return cached_result\n\n    _get_size = get_character_cell_size\n    total_size = sum(_get_size(character) for character in text)\n    if len(text) <= 512:\n        _cache[text] = total_size\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        character (str): A single 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    upper_bound = len(_table) - 1\n    index = (lower_bound + upper_bound) // 2\n    while True:\n        start, end, width = _table[index]\n        if codepoint < start:\n            upper_bound = index - 1\n        elif codepoint > end:\n            lower_bound = index + 1\n        else:\n            return 0 if width == -1 else width\n        if upper_bound < lower_bound:\n            break\n        index = (lower_bound + upper_bound) // 2\n    return 1\n\n\ndef set_cell_size(text: str, total: int) -> str:\n    \"\"\"Set the length of a string to fit within given number of cells.\"\"\"\n\n    if _is_single_cell_widths(text):\n        size = len(text)\n        if size < total:\n            return text + \" \" * (total - size)\n        return text[:total]\n\n    if not total:\n        return \"\"\n    cell_size = cell_len(text)\n    if cell_size == total:\n        return text\n    if cell_size < total:\n        return text + \" \" * (total - cell_size)\n\n    start = 0\n    end = len(text)\n\n    # Binary search until we find the right size\n    while True:\n        pos = (start + end) // 2\n        before = text[: pos + 1]\n        before_len = cell_len(before)\n        if before_len == total + 1 and cell_len(before[-1]) == 2:\n            return before[:-1] + \" \"\n        if before_len == total:\n            return before\n        if before_len > total:\n            end = pos\n        else:\n            start = pos\n\n\n# TODO: This is inefficient\n# TODO: This might not work with CWJ type characters\ndef chop_cells(text: str, max_size: int, position: int = 0) -> List[str]:\n    \"\"\"Break text in to equal (cell) length strings, returning the characters in reverse\n    order\"\"\"\n    _get_character_cell_size = get_character_cell_size\n    characters = [\n        (character, _get_character_cell_size(character)) for character in text\n    ]\n    total_size = position\n    lines: List[List[str]] = [[]]\n    append = lines[-1].append\n\n    for character, size in reversed(characters):\n        if total_size + size > max_size:\n            lines.append([character])\n            append = lines[-1].append\n            total_size = size\n        else:\n            total_size += size\n            append(character)\n\n    return [\"\".join(line) for line in lines]\n\n\nif __name__ == \"__main__\":  # pragma: no cover\n\n    print(get_character_cell_size(\"\ud83d\ude3d\"))\n    for line in chop_cells(\"\"\"\u8fd9\u662f\u5bf9\u4e9a\u6d32\u8bed\u8a00\u652f\u6301\u7684\u6d4b\u8bd5\u3002\u9762\u5bf9\u6a21\u68f1\u4e24\u53ef\u7684\u60f3\u6cd5\uff0c\u62d2\u7edd\u731c\u6d4b\u7684\u8bf1\u60d1\u3002\"\"\", 8):\n        print(line)\n    for n in range(80, 1, -1):\n        print(set_cell_size(\"\"\"\u8fd9\u662f\u5bf9\u4e9a\u6d32\u8bed\u8a00\u652f\u6301\u7684\u6d4b\u8bd5\u3002\u9762\u5bf9\u6a21\u68f1\u4e24\u53ef\u7684\u60f3\u6cd5\uff0c\u62d2\u7edd\u731c\u6d4b\u7684\u8bf1\u60d1\u3002\"\"\", n) + \"|\")\n        print(\"x\" * n)\n", "path": "rich/cells.py"}]}