Skip to content

Commit 7b1567c

Browse files
committed
fix parsing of SGR RGB codes using colons
1 parent 9c9b011 commit 7b1567c

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Fixed extraction of recursive exceptions https://github.com/Textualize/rich/pull/3772
2020
- Fixed padding applied to Syntax https://github.com/Textualize/rich/pull/3782
2121
- Fixed `Panel` title missing the panel background style https://github.com/Textualize/rich/issues/3569
22+
- Fixed parsing of SGR color codes using colons https://github.com/Textualize/rich/pull/3789
2223

2324
### Added
2425

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The following people have contributed to the development of Rich:
1111
- [Robin Bowes](https://github.com/yo61)
1212
- [Dennis Brakhane](https://github.com/brakhane)
1313
- [Darren Burns](https://github.com/darrenburns)
14+
- [Alex Carney](https://github.com/alcarney)
1415
- [Ceyda Cinarel](https://github.com/cceyda)
1516
- [Jim Crist-Harif](https://github.com/jcrist)
1617
- [Ed Davis](https://github.com/davised)

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)