← baseline_full_v2

rich_3278

failed NO_PATCH NO_PATCH UNSUBMITTED empty_patch(None) · None tool calls · 0 s · Textualize/rich

Task input

Strip problematic private escape sequences

## Type of changes

- [x] Bug fix
- [ ] New feature
- [ ] Documentation / docstrings
- [ ] Tests
- [ ] Other

## Checklist

- [x] I've run the latest [black](https://github.com/psf/black) with default args on new code.
- [x] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
- [x] I've added tests for new code.
- [x] I accept that @willmcgugan may be pedantic in the code review.

## Description

["Private escape sequences"](https://en.wikipedia.org/wiki/ANSI_escape_code#Fp_Escape_sequences) are escape sequences that are reserved for private use, though `\x1b7` and `\x1b8` are commonly used for storing and restoring the current cursor position. When those escape codes are not stripped the cursor jumps around and causes Rich to write garbage output. An example of a program that uses this cursor store/restore functionality is the APK package manager in Alpine Linux:

https://gitlab.alpinelinux.org/alpine/apk-tools/-/blob/48d91f482eb48a0a107b714ee183bb7e07782e14/src/print.c#L232-240

This commit updates the ANSI parser to ignore the `\x1b0`-`\x1b?` escape sequences, thus preventing them from being printed and causing havoc.

Tool calls (0)

#ToolArgumentsResult
No trace captured.

Test output

show
.......F
=================================== FAILURES ===================================
____________________ test_strip_private_escape_sequences[0] ____________________

code = '0'

    @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
E       AssertionError: assert '\x1b0x\n' == 'x\n'
E         
E         - x
E         + 0x

tests/test_ansi.py:84: AssertionError
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed, 7 passed in 0.11s