← or20_qwen27b_v2

rich_3278

failed NO_PATCH NO_PATCH UNSUBMITTED agent_error · 3 tool calls · 365 s · Textualize/rich

Task input

Strip problematic private escape sequences

## 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

["Private escape sequences"](https://en.wikipedia.org/wiki/ANSI_escape_code#Fp_Escape_sequences) are escape sequences that are reserved for private use, though `\x1b7` and `\x1b8` are commonly used for storing and restoring the current cursor position. When those escape codes are not stripped the cursor jumps around and causes Rich to write garbage output. An example of a program that uses this cursor store/restore functionality is the APK package manager in Alpine Linux:

https://gitlab.alpinelinux.org/alpine/apk-tools/-/blob/48d91f482eb48a0a107b714ee183bb7e07782e14/src/print.c#L232-240

This commit updates the ANSI parser to ignore the `\x1b0`-`\x1b?` escape sequences, thus preventing them from being printed and causing havoc.

Tool calls (3)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"ansi\" --include=*.py rich/ | head -30"
}
{"status": "ok", "stdout": "rich/console.py:944: # Return False for Idle which claims to be a tty but can't handle ansi codes\nrich/console.py:2130: styles (boo
full
{"status": "ok", "stdout": "rich/console.py:944:            # Return False for Idle which claims to be a tty but can't handle ansi codes\nrich/console.py:2130:            styles (bool, optional): If ``True``, ansi escape codes will be included. ``False`` for plain text.\nrich/console.py:2163:            styles (bool, optional): If ``True``, ansi style codes will be included. ``False`` for plain text.\nrich/color.py:372:            return theme.ansi_colors[self.number]\nrich/color.py:381:    def from_ansi(cls, number: int) -> \"Color\":\nrich/color.py:382:        \"\"\"Create a Color number from it's 8-bit ansi number.\nrich/color.py:485:    def get_ansi_codes(self, foreground: bool = True) -> Tuple[str, ...]:\nrich/style.py:73:        \"_ansi\",\nrich/style.py:143:        self._ansi: Optional[str] = None\nrich/style.py:214:        style._ansi = None\nrich/style.py:235:        style._ansi = None\nrich/style.py:282:        \"\"\"Get a link id, used in ansi code for links.\"\"\"\nrich/style.py:335:    def _make_ansi_codes(self, color_system: ColorSystem) -> str:\nrich/style.py:345:        if self._ansi is None:\nrich/style.py:368:                sgr.extend(self._color.downgrade(color_system).get_ansi_codes())\nrich/style.py:371:                    self._bgcolor.downgrade(color_system).get_ansi_codes(\nrich/style.py:375:            self._ansi = \";\".join(sgr)\nrich/style.py:376:        return self._ansi\nrich/style.py:483:        style._ansi = None\nrich/style.py:635:        style._ansi = self._ansi\nrich/style.py:658:        style._ansi = self._ansi\nrich/style.py:681:        style._ansi = self._ansi\nrich/style.py:712:        attrs = self._ansi or self._make_ansi_codes(color_system)\nrich/style.py:739:        new_style._ansi = None\nrich/traceback.py:273:        self.theme = Syntax.get_theme(theme or \"ansi_dark\")\nrich/_palettes.py:26:# # The standard ansi colors (including bright variants)\nrich/file_proxy.py:4:from .ansi import AnsiDecoder\nrich/file_proxy.py:18:
2read_file
{
 "filepath": "rich/ansi.py"
}
{"status": "ok", "filepath": "rich/ansi.py", "content": "import re\nimport sys\nfrom contextlib import suppress\nfrom typing import Iterable, NamedTuple, Option
full
{"status": "ok", "filepath": "rich/ansi.py", "content": "import re\nimport sys\nfrom contextlib import suppress\nfrom typing import Iterable, NamedTuple, Optional\n\nfrom .color import Color\nfrom .style import Style\nfrom .text import Text\n\nre_ansi = re.compile(\n    r\"\"\"\n(?:\\x1b\\](.*?)\\x1b\\\\)|\n(?:\\x1b([(@-Z\\\\-_]|\\[[0-?]*[ -/]*[@-~]))\n\"\"\",\n    re.VERBOSE,\n)\n\n\nclass _AnsiToken(NamedTuple):\n    \"\"\"Result of ansi tokenized string.\"\"\"\n\n    plain: str = \"\"\n    sgr: Optional[str] = \"\"\n    osc: Optional[str] = \"\"\n\n\ndef _ansi_tokenize(ansi_text: str) -> Iterable[_AnsiToken]:\n    \"\"\"Tokenize a string in to plain text and ANSI codes.\n\n    Args:\n        ansi_text (str): A String containing ANSI codes.\n\n    Yields:\n        AnsiToken: A named tuple of (plain, sgr, osc)\n    \"\"\"\n\n    position = 0\n    sgr: Optional[str]\n    osc: Optional[str]\n    for match in re_ansi.finditer(ansi_text):\n        start, end = match.span(0)\n        osc, sgr = match.groups()\n        if start > position:\n            yield _AnsiToken(ansi_text[position:start])\n        if sgr:\n            if sgr == \"(\":\n                position = end + 1\n                continue\n            if sgr.endswith(\"m\"):\n                yield _AnsiToken(\"\", sgr[1:-1], osc)\n        else:\n            yield _AnsiToken(\"\", sgr, osc)\n        position = end\n    if position < len(ansi_text):\n        yield _AnsiToken(ansi_text[position:])\n\n\nSGR_STYLE_MAP = {\n    1: \"bold\",\n    2: \"dim\",\n    3: \"italic\",\n    4: \"underline\",\n    5: \"blink\",\n    6: \"blink2\",\n    7: \"reverse\",\n    8: \"conceal\",\n    9: \"strike\",\n    21: \"underline2\",\n    22: \"not dim not bold\",\n    23: \"not italic\",\n    24: \"not underline\",\n    25: \"not blink\",\n    26: \"not blink2\",\n    27: \"not reverse\",\n    28: \"not conceal\",\n    29: \"not strike\",\n    30: \"color(0)\",\n    31: \"color(1)\",\n    32: \"color(2)\",\n    33: \"color
3read_file
{
 "filepath": "rich/ansi.py",
 "start_line": 150,
 "end_line": 240
}
{"status": "ok", "filepath": "rich/ansi.py", "content": " append = text.append\n line = line.rsplit(\"\\r\", 1)[-1]\n for plain_text, sgr, osc in _ansi_tokenize
full
{"status": "ok", "filepath": "rich/ansi.py", "content": "        append = text.append\n        line = line.rsplit(\"\\r\", 1)[-1]\n        for plain_text, sgr, osc in _ansi_tokenize(line):\n            if plain_text:\n                append(plain_text, self.style or None)\n            elif osc is not None:\n                if osc.startswith(\"8;\"):\n                    _params, semicolon, link = osc[2:].partition(\";\")\n                    if semicolon:\n                        self.style = self.style.update_link(link or None)\n            elif sgr is not None:\n                # Translate in to semi-colon separated codes\n                # Ignore invalid codes, because we want to be lenient\n                codes = [\n                    min(255, int(_code) if _code else 0)\n                    for _code in sgr.split(\";\")\n                    if _code.isdigit() or _code == \"\"\n                ]\n                iter_codes = iter(codes)\n                for code in iter_codes:\n                    if code == 0:\n                        # reset\n                        self.style = _Style.null()\n                    elif code in SGR_STYLE_MAP:\n                        # styles\n                        self.style += _Style.parse(SGR_STYLE_MAP[code])\n                    elif code == 38:\n                        # \u00a0Foreground\n                        with suppress(StopIteration):\n                            color_type = next(iter_codes)\n                            if color_type == 5:\n                                self.style += _Style.from_color(\n                                    from_ansi(next(iter_codes))\n                                )\n                            elif color_type == 2:\n                                self.style += _Style.from_color(\n                                    from_rgb(\n                                        next(iter_codes),\n                                        next(iter_codes),\n