failed WRONG_FIX UNSUBMITTED wrong_fix_unsubmitted(None) · None tool calls · 0 s · Textualize/rich
(not found in data/tasks.jsonl)
| # | Tool | Arguments | Result |
|---|---|---|---|
| No trace captured. | |||
--- a/rich/ansi.py
+++ b/rich/ansi.py
@@ -1,22 +1,27 @@
import re
import sys
from contextlib import suppress
-from typing import Iterable, NamedTuple
+from typing import Iterable, NamedTuple, Optional
from .color import Color
from .style import Style
from .text import Text
-re_ansi = re.compile(r"(?:\x1b\[(.*?)m)|(?:\x1b\](.*?)\x1b\\)")
-re_csi = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
+re_ansi = re.compile(
+ r"""
+(?:\x1b\](.*?)\x1b\\)|
+(?:\x1b([(@-Z\\-_]|\[[0-?]*[ -/]*[@-~]))
+""",
+ re.VERBOSE,
+)
class _AnsiToken(NamedTuple):
"""Result of ansi tokenized string."""
plain: str = ""
- sgr: str | None = ""
- osc: str | None = ""
+ sgr: Optional[str] = ""
+ osc: Optional[str] = ""
def _ansi_tokenize(ansi_text: str) -> Iterable[_AnsiToken]:
@@ -29,22 +34,22 @@ def _ansi_tokenize(ansi_text: str) -> Iterable[_AnsiToken]:
AnsiToken: A named tuple of (plain, sgr, osc)
"""
- def remove_csi(ansi_text: str) -> str:
- """Remove unknown CSI sequences."""
- return re_csi.sub("", ansi_text)
-
position = 0
- sgr: str | None
- osc: str | None
+ sgr: Optional[str]
+ osc: Optional[str]
for match in re_ansi.finditer(ansi_text):
start, end = match.span(0)
- sgr, osc = match.groups()
+ osc, sgr = match.groups()
if start > position:
- yield _AnsiToken(remove_csi(ansi_text[position:start]))
- yield _AnsiToken("", sgr, osc)
+ yield _AnsiToken(ansi_text[position:start])
+ if sgr:
+ if sgr.endswith("m"):
+ yield _AnsiToken("", sgr[1:-1], osc)
+ else:
+ yield _AnsiToken("", sgr, osc)
position = end
if position < len(ansi_text):
- yield _AnsiToken(remove_csi(ansi_text[position:]))
+ yield _AnsiToken(ansi_text[position:])
SGR_STYLE_MAP = {
@@ -153,7 +158,7 @@ class AnsiDecoder:
# Translate in to semi-colon separated codes
# Ignore invalid codes, because we want to be lenient
codes = [
- min(255, int(_code or "0"))
+ min(255, int(_code) if _code else 0)
for _code in sgr.split(";")
if _code.isdigit() or _code == ""
]
F
=================================== FAILURES ===================================
_________________________________ test_decode __________________________________
def test_decode():
console = Console(
force_terminal=True, legacy_windows=False, color_system="truecolor"
)
console.begin_capture()
console.print("Hello")
console.print("[b]foo[/b]")
console.print("[link http://example.org]bar")
console.print("[#ff0000 on color(200)]red")
console.print("[color(200) on #ff0000]red")
terminal_codes = console.end_capture()
decoder = AnsiDecoder()
lines = list(decoder.decode(terminal_codes))
expected = [
Text("Hello"),
Text("foo", spans=[Span(0, 3, Style.parse("bold"))]),
Text("bar", spans=[Span(0, 3, Style.parse("link http://example.org"))]),
Text("red", spans=[Span(0, 3, Style.parse("#ff0000 on color(200)"))]),
Text("red", spans=[Span(0, 3, Style.parse("color(200) on #ff0000"))]),
]
> assert lines == expected
E AssertionError: assert [<text 'Hello...3, Style())]>] == [<text 'Hello... blue=0))))]>]
E
E At index 3 diff: <text 'red' [Span(0, 3, Style())]> != <text 'red' [Span(0, 3, Style(color=Color('#ff0000', ColorType.TRUECOLOR, triplet=ColorTriplet(red=255, green=0, blue=0)), bgcolor=Color('color(200)', ColorType.EIGHT_BIT, number=200)))]>
E Use -v to get more diff
tests/test_ansi.py:30: AssertionError
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed in 0.17s