Skip to content

Commit 6ae0046

Browse files
Bugfix in ANSI formatted text. Correctly handle 256/true color sequences.
1 parent 3a0d627 commit 6ae0046

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

prompt_toolkit/formatted_text/ansi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def _select_graphic_rendition(self, attrs):
159159
n = attrs.pop()
160160

161161
# 256 colors.
162-
if n == 5 and len(attrs) > 1:
162+
if n == 5 and len(attrs) >= 1:
163163
if attr == 38:
164164
m = attrs.pop()
165165
self._color = _256_colors.get(m)
@@ -168,7 +168,7 @@ def _select_graphic_rendition(self, attrs):
168168
self._bgcolor = _256_colors.get(m)
169169

170170
# True colors.
171-
if n == 2 and len(attrs) > 3:
171+
if n == 2 and len(attrs) >= 3:
172172
try:
173173
color_str = '%02x%02x%02x' % (
174174
attrs.pop(), attrs.pop(), attrs.pop())

tests/test_formatted_text.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ def test_ansi_formatting():
7878
assert isinstance(to_formatted_text(value), FormattedText)
7979

8080

81+
def test_ansi_256_color():
82+
assert to_formatted_text(ANSI('\x1b[38;5;124mtest')) == [
83+
('#af0000', 't'), ('#af0000', 'e'), ('#af0000', 's'), ('#af0000', 't')
84+
]
85+
86+
8187
def test_interpolation():
8288
value = Template(' {} ').format(HTML('<b>hello</b>'))
8389

0 commit comments

Comments
 (0)