Skip to content
Merged
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
32 changes: 32 additions & 0 deletions packages/cli/src/ui/utils/CodeColorizer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,36 @@ describe('colorizeCode', () => {
expect(lastFrame()).toMatch(/line 1\s*\n\s*\n\s*line 3/);
unmount();
});

it('does not let colors from ansi escape codes leak into colorized code', async () => {
const code = 'line 1\n\x1b[41mline 2 with red background\x1b[0m\nline 3';
const settings = new LoadedSettings(
{ path: '', settings: {}, originalSettings: {} },
{ path: '', settings: {}, originalSettings: {} },
{
path: '',
settings: { ui: { useAlternateBuffer: true, showLineNumbers: false } },
originalSettings: {
ui: { useAlternateBuffer: true, showLineNumbers: false },
},
},
{ path: '', settings: {}, originalSettings: {} },
true,
[],
);

const result = colorizeCode({
code,
language: 'javascript',
maxWidth: 80,
settings,
hideLineNumbers: true,
});

const renderResult = renderWithProviders(<>{result}</>);
await renderResult.waitUntilReady();

await expect(renderResult).toMatchSvgSnapshot();
renderResult.unmount();
});
Comment on lines +54 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This test case verifies that ANSI escape codes do not leak colors into the colorized code. It uses a snapshot test to compare the rendered output with a pre-defined snapshot. Ensure that the snapshot is updated whenever the rendering logic changes. Additionally, when using renderWithProviders, the returned unmount function must be called at the end of the test for proper cleanup.

  it('does not let colors from ansi escape codes leak into colorized code', async () => {
    const code = 'line 1\n\x1b[41mline 2 with red background\x1b[0m\nline 3';
    const settings = new LoadedSettings(
      { path: '', settings: {}, originalSettings: {} },
      { path: '', settings: {}, originalSettings: {} },
      {
        path: '',
        settings: { ui: { useAlternateBuffer: true, showLineNumbers: false } },
        originalSettings: {
          ui: { useAlternateBuffer: true, showLineNumbers: false },
        },
      },
      { path: '', settings: {}, originalSettings: {} },
      true,
      [],
    );

    const result = colorizeCode({
      code,
      language: 'javascript',
      maxWidth: 80,
      settings,
      hideLineNumbers: true,
    });

    const renderResult = renderWithProviders(<>{
    result
  }</>);
    await renderResult.waitUntilReady();

    await expect(renderResult).toMatchSvgSnapshot();
    renderResult.unmount();
  });
References
  1. When using renderWithProviders in tests, the returned unmount function must be called at the end of the test to ensure proper cleanup and prevent resource leaks.

});
12 changes: 7 additions & 5 deletions packages/cli/src/ui/utils/CodeColorizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
ElementContent,
RootContent,
} from 'hast';
import stripAnsi from 'strip-ansi';
import { themeManager } from '../themes/theme-manager.js';
import type { Theme } from '../themes/theme.js';
import {
Expand Down Expand Up @@ -98,16 +99,17 @@ function highlightAndRenderLine(
theme: Theme,
): React.ReactNode {
try {
const strippedLine = stripAnsi(line);
const getHighlightedLine = () =>
!language || !lowlight.registered(language)
? lowlight.highlightAuto(line)
: lowlight.highlight(language, line);
? lowlight.highlightAuto(strippedLine)
: lowlight.highlight(language, strippedLine);

const renderedNode = renderHastNode(getHighlightedLine(), theme, undefined);

return renderedNode !== null ? renderedNode : line;
return renderedNode !== null ? renderedNode : strippedLine;
} catch (_error) {
return line;
return stripAnsi(line);
}
}

Expand Down Expand Up @@ -238,7 +240,7 @@ export function colorizeCode({
<Text color={activeTheme.defaultColor}>{`${index + 1}`}</Text>
</Box>
)}
<Text color={activeTheme.colors.Gray}>{line}</Text>
<Text color={activeTheme.colors.Gray}>{stripAnsi(line)}</Text>
</Box>
));

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`colorizeCode > does not let colors from ansi escape codes leak into colorized code 1`] = `
"line 1
line 2 with red background
line 3"
`;
Loading