-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
ref(ui): replace ansi-to-react with shared AnsiText component #126237
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
JoshuaKGoldberg
wants to merge
5
commits into
master
Choose a base branch
from
joshgoldberg/de-1651-remove-ansi-to-react-in-favor-of-ansi-sequence-parser
base: master
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.
+276
−259
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce3ac45
ref(ui): Replace ansi-to-react with shared AnsiText component
JoshuaKGoldberg 7f9792d
fix(ui): Apply carriage returns in .log previews
JoshuaKGoldberg 29bbffd
ref(ui): Make AnsiText whitespace preservation opt-in
JoshuaKGoldberg 2250b86
ref(ui): Soften dark theme ANSI green
JoshuaKGoldberg fda6aa7
Revert "ref(ui): Soften dark theme ANSI green"
JoshuaKGoldberg 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,122 @@ | ||
| import {render, screen} from 'sentry-test/reactTestingLibrary'; | ||
|
|
||
| import {AnsiText} from 'sentry/components/ansiText'; | ||
|
|
||
| function renderSegments(text: string) { | ||
| render(<AnsiText>{text}</AnsiText>); | ||
|
|
||
| const wrapper = screen.getByText('FATAL').parentElement; | ||
|
|
||
| return { | ||
| segments: Array.from(wrapper?.querySelectorAll('span') ?? [], span => ({ | ||
| text: span.textContent, | ||
| colored: span.style.color !== '', | ||
| })), | ||
| wrapper, | ||
| }; | ||
| } | ||
|
|
||
| describe('AnsiText', () => { | ||
| it('renders text without escape codes when given ANSI colors', () => { | ||
| const {container} = render(<AnsiText>{'\x1B[31mfailed\x1B[0m to connect'}</AnsiText>); | ||
|
|
||
| expect(container).toHaveTextContent(/^failed to connect$/); | ||
| }); | ||
|
|
||
| it('blends ANSI colors into the text and background colors when given colored segments', () => { | ||
| render(<AnsiText>{'\x1B[31;44mcolored'}</AnsiText>); | ||
|
|
||
| const segment = screen.getByText('colored'); | ||
| expect(segment.style.color).toContain('color-mix(in srgb,'); | ||
| expect(segment.style.backgroundColor).toContain('color-mix(in srgb,'); | ||
| }); | ||
|
|
||
| it('resolves 256-color and truecolor codes to their RGB values', () => { | ||
| render( | ||
| <AnsiText>{'\x1B[38;5;208mpalette\x1B[0m \x1B[38;2;1;2;3mtruecolor'}</AnsiText> | ||
| ); | ||
|
|
||
| expect(screen.getByText('palette').style.color).toContain('rgb(255, 135, 0)'); | ||
| expect(screen.getByText('truecolor').style.color).toContain('rgb(1, 2, 3)'); | ||
| }); | ||
|
|
||
| it('applies every decoration when given combined decoration codes', () => { | ||
| render(<AnsiText>{'\x1B[1;3;4;9mdecorated'}</AnsiText>); | ||
|
|
||
| expect(screen.getByText('decorated')).toHaveStyle({ | ||
| fontWeight: 'bold', | ||
| fontStyle: 'italic', | ||
| textDecorationLine: 'underline line-through', | ||
| }); | ||
| }); | ||
|
|
||
| it('removes non-color escape codes when given cursor control sequences', () => { | ||
| const {container} = render(<AnsiText>{'\x1B[2Kdone\x1B[?25h'}</AnsiText>); | ||
|
|
||
| expect(container).toHaveTextContent(/^done$/); | ||
| }); | ||
|
|
||
| it('colors only the escaped text when given uncolored spaces around a colored word', () => { | ||
| const {segments} = renderSegments(' \x1B[31mFATAL\x1B[0m '); | ||
|
|
||
| expect(segments).toEqual([ | ||
| {text: ' ', colored: false}, | ||
| {text: 'FATAL', colored: true}, | ||
| {text: ' ', colored: false}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('colors a trailing space when given the space inside the escape codes', () => { | ||
| const {segments} = renderSegments(' \x1B[31mFATAL \x1B[0m '); | ||
|
|
||
| expect(segments).toEqual([ | ||
| {text: ' ', colored: false}, | ||
| {text: 'FATAL ', colored: true}, | ||
| {text: ' ', colored: false}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('colors a leading space when given the space inside the escape codes', () => { | ||
| const {segments} = renderSegments(' \x1B[31m FATAL\x1B[0m '); | ||
|
|
||
| expect(segments).toEqual([ | ||
| {text: ' ', colored: false}, | ||
| {text: ' FATAL', colored: true}, | ||
| {text: ' ', colored: false}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('colors only the escaped word when given uncolored text before it and a space after it', () => { | ||
| const {segments} = renderSegments('ABC\x1B[31mFATAL\x1B[0m '); | ||
|
|
||
| expect(segments).toEqual([ | ||
| {text: 'ABC', colored: false}, | ||
| {text: 'FATAL', colored: true}, | ||
| {text: ' ', colored: false}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('colors only the escaped word when given a space before it and uncolored text after it', () => { | ||
| const {segments} = renderSegments(' \x1B[31mFATAL\x1B[0mABC'); | ||
|
|
||
| expect(segments).toEqual([ | ||
| {text: ' ', colored: false}, | ||
| {text: 'FATAL', colored: true}, | ||
| {text: 'ABC', colored: false}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('collapses whitespace by default when given escape codes', () => { | ||
| const {wrapper} = renderSegments(' \x1B[41m FATAL \x1B[0m '); | ||
|
|
||
| expect(wrapper).not.toHaveStyle({whiteSpaceCollapse: 'preserve'}); | ||
| }); | ||
|
|
||
| it('preserves whitespace when given escape codes and preserveWhitespace', () => { | ||
| render(<AnsiText preserveWhitespace>{' \x1B[41m FATAL \x1B[0m '}</AnsiText>); | ||
|
|
||
| expect(screen.getByText('FATAL').parentElement).toHaveStyle({ | ||
| whiteSpaceCollapse: 'preserve', | ||
| }); | ||
| }); | ||
| }); |
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,92 @@ | ||
| import {useMemo, type CSSProperties, type ReactNode} from 'react'; | ||
| import {useTheme} from '@emotion/react'; | ||
| import { | ||
| createColorPalette, | ||
| parseAnsiSequences, | ||
| type ParseToken, | ||
| } from 'ansi-sequence-parser'; | ||
|
|
||
| import {hasAnsi, stripAnsi} from 'sentry/utils/ansiEscapeCodes'; | ||
|
|
||
| const ANSI_BACKGROUND_STRENGTH = '15%'; | ||
| const ANSI_TEXT_STRENGTH = '50%'; | ||
|
|
||
| const PRESERVE_WHITESPACE_STYLE: CSSProperties = {whiteSpaceCollapse: 'preserve'}; | ||
|
|
||
| type ColorPalette = ReturnType<typeof createColorPalette>; | ||
|
|
||
| interface AnsiTextProps { | ||
| children: string; | ||
| preserveWhitespace?: boolean; | ||
| renderText?: (text: string) => ReactNode; | ||
| } | ||
|
|
||
| export function AnsiText({ | ||
| children, | ||
| preserveWhitespace, | ||
| renderText = text => text, | ||
| }: AnsiTextProps) { | ||
| const theme = useTheme(); | ||
| const palette = useMemo(() => createColorPalette(theme.tokens.syntax.ansi), [theme]); | ||
| const tokens = useMemo( | ||
| () => | ||
| hasAnsi(children) | ||
| ? parseAnsiSequences(children) | ||
| .map(token => ({...token, value: stripAnsi(token.value)})) | ||
| .filter(token => token.value) | ||
| : undefined, | ||
| [children] | ||
| ); | ||
|
|
||
| if (!tokens) { | ||
| return renderText(children); | ||
| } | ||
|
|
||
| return ( | ||
| <span style={preserveWhitespace ? PRESERVE_WHITESPACE_STYLE : undefined}> | ||
| {tokens.map((token, index) => ( | ||
| <span key={index} style={getTokenStyle(token, palette)}> | ||
| {renderText(token.value)} | ||
| </span> | ||
| ))} | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| function getTokenStyle( | ||
| {background, decorations, foreground}: ParseToken, | ||
| palette: ColorPalette | ||
| ): CSSProperties { | ||
| const style: CSSProperties = {}; | ||
|
|
||
| if (foreground) { | ||
| style.color = `color-mix(in srgb, ${palette.value(foreground)} ${ANSI_TEXT_STRENGTH}, currentColor)`; | ||
| } | ||
|
|
||
| if (background) { | ||
| style.backgroundColor = `color-mix(in srgb, ${palette.value(background)} ${ANSI_BACKGROUND_STRENGTH}, transparent)`; | ||
| } | ||
|
|
||
| if (decorations.has('bold')) { | ||
| style.fontWeight = 'bold'; | ||
| } | ||
|
|
||
| if (decorations.has('dim')) { | ||
| style.opacity = 0.7; | ||
| } | ||
|
|
||
| if (decorations.has('italic')) { | ||
| style.fontStyle = 'italic'; | ||
| } | ||
|
|
||
| const lines = [ | ||
| decorations.has('underline') && 'underline', | ||
| decorations.has('strikethrough') && 'line-through', | ||
| decorations.has('overline') && 'overline', | ||
| ].filter(Boolean); | ||
| if (lines.length) { | ||
| style.textDecorationLine = lines.join(' '); | ||
| } | ||
|
|
||
| return style; | ||
| } | ||
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.
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.