Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

Unreleased
----------

Fixes:
- Escape a C1 CSI in ``ANSI.format`` so an argument cannot set a color (#2113).

3.0.53: 2026-07-26
------------------

Expand Down
3 changes: 2 additions & 1 deletion src/prompt_toolkit/formatted_text/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ def ansi_escape(text: object) -> str:
"""
Replace characters with a special meaning.
"""
return str(text).replace("\x1b", "?").replace("\b", "?")
# \x9b is the C1 form of ESC [. Leaving it lets format() set a color.
return str(text).replace("\x1b", "?").replace("\x9b", "?").replace("\b", "?")


class ANSIFormatter(Formatter):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_formatted_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,38 @@ def test_ansi_interpolation():
]


def test_ansi_c1_interpolation():
# A C1 CSI in an argument must not set a color. The template still can.
value = ANSI("\x9b32mOK")
assert to_formatted_text(value) == [
("ansigreen", "O"),
("ansigreen", "K"),
]

value = ANSI("x{}").format("\x9b31mRED")
assert to_formatted_text(value) == [
("", "x"),
("", "?"),
("", "3"),
("", "1"),
("", "m"),
("", "R"),
("", "E"),
("", "D"),
]

value = ANSI("%s") % "\x9b31mRED"
assert to_formatted_text(value) == [
("", "?"),
("", "3"),
("", "1"),
("", "m"),
("", "R"),
("", "E"),
("", "D"),
]


def test_interpolation():
value = Template(" {} ").format(HTML("<b>hello</b>"))

Expand Down
Loading