Skip to content

Commit de24e39

Browse files
committed
fix parsing of SGR RGB codes using colons
1 parent 49117ce commit de24e39

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

rich/ansi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def decode_line(self, line: str) -> Text:
144144
Returns:
145145
Text: A Text instance marked up according to ansi codes.
146146
"""
147+
147148
from_ansi = Color.from_ansi
148149
from_rgb = Color.from_rgb
149150
_Style = Style
@@ -163,7 +164,7 @@ def decode_line(self, line: str) -> Text:
163164
# Ignore invalid codes, because we want to be lenient
164165
codes = [
165166
min(255, int(_code) if _code else 0)
166-
for _code in sgr.split(";")
167+
for _code in sgr.replace("::", ";").replace(":", ";").split(";")
167168
if _code.isdigit() or _code == ""
168169
]
169170
iter_codes = iter(codes)

tests/test_ansi.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from rich.ansi import AnsiDecoder
43
from rich.console import Console
54
from rich.style import Style
@@ -70,6 +69,23 @@ def test_decode_issue_2688(ansi_bytes, expected_text):
7069
assert str(text) == expected_text
7170

7271

72+
@pytest.mark.parametrize(
73+
"text,expected",
74+
[
75+
(
76+
"\x1b[38;2;255;0;0mred\x1b[0m text",
77+
Text("red text", spans=[Span(0, 3, Style.parse("#ff0000"))]),
78+
),
79+
(
80+
"\x1b[38:2::255:0:0mred\x1b[0m text",
81+
Text("red text", spans=[Span(0, 3, Style.parse("#ff0000"))]),
82+
),
83+
],
84+
)
85+
def test_decode_sgr_rgb(text, expected):
86+
assert Text.from_ansi(text) == expected
87+
88+
7389
@pytest.mark.parametrize("code", [*"0123456789:;<=>?"])
7490
def test_strip_private_escape_sequences(code):
7591
text = Text.from_ansi(f"\x1b{code}x")

0 commit comments

Comments
 (0)