Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Improving Debug Console Styling. Adding underline and background support #374

Merged
merged 1 commit into from
Oct 29, 2018
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
70 changes: 45 additions & 25 deletions src/chrome/consoleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ function resolveParams(args: Crdp.Runtime.RemoteObject[], skipFormatSpecifiers?:
return processedArgs;
}

const colorRegex = /color: (.*?)(?:;|$)/;
const fontWeightRegex = /font-weight: (.*?)(?:;|$)/;

function formatArg(formatSpec: string, arg: Crdp.Runtime.RemoteObject): string | Crdp.Runtime.RemoteObject {
const paramValue = String(typeof arg.value !== 'undefined' ? arg.value : arg.description);

Expand All @@ -150,31 +147,41 @@ function formatArg(formatSpec: string, arg: Crdp.Runtime.RemoteObject): string |
} else if (formatSpec === 'f') {
return +paramValue + '';
} else if (formatSpec === 'c') {
// value:"color: gray; font-weight: light
const colorMatches = colorRegex.exec(arg.value);
const fontWeightMatches = fontWeightRegex.exec(arg.value);
const cssRegex = /\s*(.*?)\s*:\s*(.*?)\s*(?:;|$)/g;
Copy link
Member

Choose a reason for hiding this comment

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

Isn't the .*? redundant or is that different than .*?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its not, * is greedy by default, and will try to match as much text as possible and will eventually match multiple parameters. ? makes it lazy.

test: aaaa; test2: bbbbb;

If ? gets removed, the first match on this text will be the whole string, and that's not what we want.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, didn't know that, thanks.


let escapedSequence = '';
let match = cssRegex.exec(arg.value);
while (match != null) {
if (match.length === 3) {
switch (match[1]) {
case 'color':
const color = getAnsi16Color(match[2]);
if (color) {
escapedSequence += `;${color}`;
}
break;
case 'background':
const background = getAnsi16Color(match[2]);
if (background) {
escapedSequence += `;${background + 10}`;
}
break;
case 'font-weight':
if (match[2] === 'bold') {
escapedSequence += ';1';
}
break;
case 'text-decoration':
if (match[2] === 'underline') {
escapedSequence += ';4';
}
break;
default:
// css not mapped, skip
}
}

if (colorMatches && colorMatches.length > 0) {
try {
// Color can parse, hex and color names
const color = new Color(colorMatches[1]);
const ansi = color.ansi16().object().ansi16;
escapedSequence = `;${ansi}`;
} catch (ex) {
// Unable to parse Color
// For instance, "inherit" color will throw
}
}

if (fontWeightMatches && fontWeightMatches.length > 0) {
switch (fontWeightMatches[1]) {
case 'bold':
escapedSequence += ';1';
break;
default:
}
match = cssRegex.exec(arg.value);
}

if (escapedSequence.length > 0) {
Expand Down Expand Up @@ -212,3 +219,16 @@ function stackTraceToString(stackTrace: Crdp.Runtime.StackTrace): string {
})
.join('\n');
}

function getAnsi16Color(colorString: string): number {
try {
// Color can parse hex and color names
const color = new Color(colorString);
return color.ansi16().object().ansi16;
} catch (ex) {
// Unable to parse Color
// For instance, "inherit" color will throw
}

return undefined;
}
14 changes: 13 additions & 1 deletion test/chrome/consoleHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ suite('ConsoleHelper', () => {
});

test('handles %c patterns with font-weight', () => {
doAssertForString(Runtime.makeLog('foo %cbar', 'color: red;font-weight: bold'), 'foo \x1b[0;91;1mbar');
doAssertForString(Runtime.makeLog('foo %cbar', 'font-weight: bold'), 'foo \x1b[0;1mbar');
});

test('handles %c patterns with background', () => {
doAssertForString(Runtime.makeLog('foo %cbar', 'background: red'), 'foo \x1b[0;101mbar');
});

test('handles %c patterns with text-decoration', () => {
doAssertForString(Runtime.makeLog('foo %cbar', 'text-decoration: underline'), 'foo \x1b[0;4mbar');
});

test('handles %c patterns with multiple attributes', () => {
doAssertForString(Runtime.makeLog('foo %cbar', 'color: red; background: blue; font-weight: bold; text-decoration: underline'), 'foo \x1b[0;91;104;1;4mbar');
});

test('starts with non-string', () => {
Expand Down