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
27 changes: 20 additions & 7 deletions packages/trace-viewer/src/ui/consoleTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ type ConsoleTabModel = {

const ConsoleListView = ListView<ConsoleEntry>;

const ansiColours = {
log: {
bg: 'var(--vscode-editor-background)', fg: 'var(--vscode-editor-foreground)'
},
warning: {
fg: 'var(--vscode-list-warningForeground)', bg: 'var(--vscode-inputValidation-warningBackground)'
},
error: {
fg: 'var(--vscode-list-errorForeground)', bg: 'var(--vscode-inputValidation-errorBackground)'
}
};

export function useConsoleTabModel(model: TraceModel | undefined, selectedTime: Boundaries | undefined): ConsoleTabModel {
const { entries } = React.useMemo(() => {
Expand Down Expand Up @@ -76,7 +87,8 @@ export function useConsoleTabModel(model: TraceModel | undefined, selectedTime:
});
for (const event of logEvents) {
if (event.type === 'console') {
const body = event.args && event.args.length ? format(event.args) : formatAnsi(event.text);
const colours = event.messageType === 'error' ? ansiColours.error : event.messageType === 'warning' ? ansiColours.warning : ansiColours.log;
const body = event.args && event.args.length ? format(event.args, colours) : formatAnsi(event.text, colours);
const url = event.location.url;
const filename = url ? url.substring(url.lastIndexOf('/') + 1) : '<anonymous>';
const location = `${filename}:${event.location.lineNumber}`;
Expand All @@ -102,10 +114,11 @@ export function useConsoleTabModel(model: TraceModel | undefined, selectedTime:
}
if (event.type === 'stderr' || event.type === 'stdout') {
let html = '';
const colours = event.type === 'stderr' ? ansiColours.error : ansiColours.log;
if (event.text)
html = ansi2html(event.text.trim()) || '';
html = ansi2html(event.text.trim(), colours) || '';
if (event.base64)
html = ansi2html(atob(event.base64).trim()) || '';
html = ansi2html(atob(event.base64).trim(), colours) || '';

addEntry({
nodeMessage: { html },
Expand Down Expand Up @@ -188,9 +201,9 @@ export const ConsoleTab: React.FunctionComponent<{
</div>;
};

function format(args: { preview: string, value: any }[]): React.JSX.Element[] {
function format(args: { preview: string, value: any }[], colours: { fg: string, bg: string }): React.JSX.Element[] {
if (args.length === 1)
return formatAnsi(args[0].preview);
return formatAnsi(args[0].preview, colours);

const hasMessageFormat = typeof args[0].value === 'string' && args[0].value.includes('%');
const messageFormat = hasMessageFormat ? args[0].value as string : '';
Expand Down Expand Up @@ -237,9 +250,9 @@ function format(args: { preview: string, value: any }[]): React.JSX.Element[] {
return formatted;
}

function formatAnsi(text: string): React.JSX.Element[] {
function formatAnsi(text: string, colours: { fg: string, bg: string }): React.JSX.Element[] {
// eslint-disable-next-line react/jsx-key
return [<span dangerouslySetInnerHTML={{ __html: ansi2html(text.trim()) }}></span>];
return [<span dangerouslySetInnerHTML={{ __html: ansi2html(text.trim(), colours) }}></span>];
}

function parseCSSStyle(cssFormat: string): Record<string, string | number> {
Expand Down
7 changes: 3 additions & 4 deletions packages/web/src/ansi2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

export function ansi2html(text: string, defaultColors?: { bg: string, fg: string }): string {
export function ansi2html(text: string, defaultColors: { bg: string, fg: string }): string {
const regex = /(\x1b\[(\d+(;\d+)*)m)|([^\x1b]+)/g;
const tokens: string[] = [];
let match;
Expand Down Expand Up @@ -109,9 +109,8 @@ export function ansi2html(text: string, defaultColors?: { bg: string, fg: string
const color = reverse ? bg : fg;
if (color !== undefined)
styleCopy['color'] = color;
const backgroundColor = reverse ? fg : bg;
if (backgroundColor !== undefined)
styleCopy['background-color'] = backgroundColor;
if (reverse && fg)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

without this, hovering the container makes the background stick out: Image

styleCopy['background-color'] = fg;
tokens.push(`<span style="${styleBody(styleCopy)}">${escapeHTML(text)}</span>`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/components/codeMirrorWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const CodeMirrorWrapper: React.FC<SourceProps> = ({

if (h.type === 'error') {
const errorWidgetElement = document.createElement('div');
errorWidgetElement.innerHTML = ansi2html(h.message || '');
errorWidgetElement.innerHTML = ansi2html(h.message || '', { bg: 'var(--vscode-inputValidation-errorBackground)', fg: 'var(--vscode-editor-foreground)' });
errorWidgetElement.className = 'source-line-error-widget';
widgets.push(codemirror.addLineWidget(h.line, errorWidgetElement, { above: true, coverGutter: false }));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/components/errorMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ import './errorMessage.css';
export const ErrorMessage: React.FC<{
error: string;
}> = ({ error }) => {
const html = React.useMemo(() => ansi2html(error), [error]);
const html = React.useMemo(() => ansi2html(error, { bg: 'var(--vscode-editor-background)', fg: 'var(--vscode-editor-foreground)' }), [error]);
return <div className='error-message' dangerouslySetInnerHTML={{ __html: html || '' }}></div>;
};
1 change: 0 additions & 1 deletion tests/playwright-test/reporter-html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,6 @@ for (const useIntermediateMergeReport of [true, false] as const) {
await expect(page.locator('.test-error-view').getByText('begin ', { exact: true })).toHaveCSS('background-color', 'rgb(205, 49, 49)');

await expect(page.locator('.test-error-view').getByText('inner', { exact: true })).toHaveCSS('color', 'rgb(205, 49, 49)');
await expect(page.locator('.test-error-view').getByText('inner', { exact: true })).toHaveCSS('background-color', 'rgb(246, 248, 250)');

await expect(page.locator('.test-error-view').getByText('end ', { exact: true })).toHaveCSS('color', 'rgb(246, 248, 250)');
await expect(page.locator('.test-error-view').getByText('end ', { exact: true })).toHaveCSS('background-color', 'rgb(205, 49, 49)');
Expand Down
Loading