Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip problematic private escape sequences #3278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Strip problematic private escape sequences (like `\x1b7`)

## [13.7.0] - 2023-11-15

### Added
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Expand Up @@ -25,6 +25,7 @@ The following people have contributed to the development of Rich:
- [Kenneth Hoste](https://github.com/boegel)
- [Lanqing Huang](https://github.com/lqhuang)
- [Finn Hughes](https://github.com/finnhughes)
- [Logan Hunt](https://github.com/dosisod)
- [Ionite](https://github.com/ionite34)
- [Josh Karpel](https://github.com/JoshKarpel)
- [Jan Katins](https://github.com/jankatins)
Expand Down
1 change: 1 addition & 0 deletions rich/ansi.py
Expand Up @@ -9,6 +9,7 @@

re_ansi = re.compile(
r"""
(?:\x1b[0-?])|
(?:\x1b\](.*?)\x1b\\)|
(?:\x1b([(@-Z\\-_]|\[[0-?]*[ -/]*[@-~]))
""",
Expand Down
14 changes: 14 additions & 0 deletions tests/test_ansi.py
Expand Up @@ -68,3 +68,17 @@ def test_decode_issue_2688(ansi_bytes, expected_text):
text = Text.from_ansi(ansi_bytes.decode())

assert str(text) == expected_text


@pytest.mark.parametrize("code", [*"0123456789:;<=>?"])
def test_strip_private_escape_sequences(code):
text = Text.from_ansi(f"\x1b{code}x")

console = Console(force_terminal=True)

with console.capture() as capture:
console.print(text)

expected = "x\n"

assert capture.get() == expected