-
Notifications
You must be signed in to change notification settings - Fork 963
fix(python-sdk): port current JS stripAnsi regex to strip_ansi_escape_codes #1545
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
Open
mishushakov
wants to merge
3
commits into
main
Choose a base branch
from
strip-ansi-python-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "e2b": patch | ||
| "@e2b/python-sdk": patch | ||
| --- | ||
|
|
||
| Align ANSI stripping of template build log messages across both SDKs. The Python SDK's `strip_ansi_escape_codes` now ports the JS SDK's `stripAnsi` regex: OSC sequences (hyperlinks, window titles) are matched non-greedily up to the first string terminator — including sequences spanning newlines — and CSI sequences are stripped without requiring a terminator. Both implementations additionally strip the remaining ECMA-48 string controls (DCS/Sixel, SOS, PM, APC) through their string terminator so control payloads no longer leak into cleaned logs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { expect, test } from 'vitest' | ||
| import { stripAnsi } from '../src/utils' | ||
|
|
||
| // Mirrors packages/python-sdk/tests/shared/template/utils/test_strip_ansi_escape_codes.py | ||
|
|
||
| test('strips basic SGR color', () => { | ||
| expect(stripAnsi('\x1b[31mred\x1b[0m')).toBe('red') | ||
| }) | ||
|
|
||
| test('strips semicolon-separated params', () => { | ||
| expect(stripAnsi('\x1b[1;31;42mhi\x1b[0m')).toBe('hi') | ||
| }) | ||
|
|
||
| test('strips semicolon 256-color', () => { | ||
| expect(stripAnsi('\x1b[38;5;82mX\x1b[0m')).toBe('X') | ||
| }) | ||
|
|
||
| test('strips colon 256-color', () => { | ||
| expect(stripAnsi('\x1b[38:5:82mX\x1b[0m')).toBe('X') | ||
| }) | ||
|
|
||
| test('strips colon truecolor', () => { | ||
| expect(stripAnsi('\x1b[38:2::255:0:0mRED\x1b[0m')).toBe('RED') | ||
| }) | ||
|
|
||
| test('strips colon curly underline', () => { | ||
| expect(stripAnsi('\x1b[4:3mX\x1b[0m')).toBe('X') | ||
| }) | ||
|
|
||
| test('leaves plain text unchanged', () => { | ||
| expect(stripAnsi('no escape codes here')).toBe('no escape codes here') | ||
| }) | ||
|
|
||
| test('strips OSC hyperlink', () => { | ||
| expect(stripAnsi('\x1b]8;;https://e2b.dev\x07E2B\x1b]8;;\x07')).toBe('E2B') | ||
| }) | ||
|
|
||
| test('strips OSC window title (BEL terminated)', () => { | ||
| expect(stripAnsi('\x1b]0;my title\x07text')).toBe('text') | ||
| }) | ||
|
|
||
| test('strips OSC window title (ESC backslash terminated)', () => { | ||
| expect(stripAnsi('\x1b]0;my title\x1b\\text')).toBe('text') | ||
| }) | ||
|
|
||
| test('strips OSC spanning newlines', () => { | ||
| expect(stripAnsi('\x1b]0;line1\nline2\x07after')).toBe('after') | ||
| }) | ||
|
|
||
| test('strips only to the first OSC terminator', () => { | ||
| expect(stripAnsi('\x1b]0;title\x07middle\x1b]0;other\x07end')).toBe( | ||
| 'middleend' | ||
| ) | ||
| }) | ||
|
|
||
| test('strips cursor movement', () => { | ||
| expect(stripAnsi('\x1b[2Ax\x1b[1000Dy')).toBe('xy') | ||
| }) | ||
|
|
||
| test('strips erase line', () => { | ||
| expect(stripAnsi('\x1b[2Kdone')).toBe('done') | ||
| }) | ||
| test('strips DCS (ESC backslash terminated)', () => { | ||
| expect(stripAnsi('\x1bPabc\x1b\\done')).toBe('done') | ||
| }) | ||
|
|
||
| test('strips DCS sixel payload', () => { | ||
| expect(stripAnsi('\x1bPq#0;2;0;0;0~~@@\x1b\\image')).toBe('image') | ||
| }) | ||
|
|
||
| test('strips SOS', () => { | ||
| expect(stripAnsi('\x1bXhidden\x1b\\ok')).toBe('ok') | ||
| }) | ||
|
|
||
| test('strips PM', () => { | ||
| expect(stripAnsi('\x1b^private msg\x1b\\ok')).toBe('ok') | ||
| }) | ||
|
|
||
| test('strips APC', () => { | ||
| expect(stripAnsi('\x1b_app command\x07ok')).toBe('ok') | ||
| }) | ||
|
|
||
| test('strips only the intro of an unterminated DCS', () => { | ||
| expect(stripAnsi('\x1bPno terminator here')).toBe('no terminator here') | ||
| }) | ||
| test('leaves CSI with non-ASCII digits intact', () => { | ||
| expect(stripAnsi('\x1b[٣١mred\x1b[0m')).toBe('\x1b[٣١mred') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.