Skip to content

Commit 471a7d4

Browse files
committed
console: colorize console error and warn
prints console error in red and warn in yellow
1 parent 639c096 commit 471a7d4

File tree

6 files changed

+47
-34
lines changed

6 files changed

+47
-34
lines changed

lib/internal/console/constructor.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
138138

139139
if (groupIndentation !== undefined) {
140140
validateInteger(groupIndentation, 'groupIndentation',
141-
0, kMaxGroupIndentation);
141+
0, kMaxGroupIndentation);
142142
}
143143

144144
if (inspectOptions !== undefined) {
145145
validateObject(inspectOptions, 'options.inspectOptions');
146146

147147
if (inspectOptions.colors !== undefined &&
148-
options.colorMode !== undefined) {
148+
options.colorMode !== undefined) {
149149
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
150150
'options.inspectOptions.color', 'colorMode');
151151
}
@@ -273,7 +273,7 @@ ObjectDefineProperties(Console.prototype, {
273273
[kWriteToConsole]: {
274274
__proto__: null,
275275
...consolePropAttributes,
276-
value: function(streamSymbol, string) {
276+
value: function(streamSymbol, string, color) {
277277
const ignoreErrors = this._ignoreErrors;
278278
const groupIndent = this[kGroupIndent];
279279

@@ -288,6 +288,11 @@ ObjectDefineProperties(Console.prototype, {
288288
}
289289
string = groupIndent + string;
290290
}
291+
292+
if (color && lazyUtilColors().hasColors) {
293+
string = `${color}${string}${lazyUtilColors().clear}`;
294+
}
295+
291296
string += '\n';
292297

293298
if (ignoreErrors === false) return stream.write(string);
@@ -381,9 +386,14 @@ const consoleMethods = {
381386

382387

383388
warn(...args) {
384-
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args));
389+
const color = typeof args === 'string'? lazyUtilColors().yellow : null;
390+
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color);
385391
},
386392

393+
error(...args) {
394+
const color = typeof args === 'string'? lazyUtilColors().red : null;
395+
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color);
396+
},
387397

388398
dir(object, options) {
389399
this[kWriteToConsole](kUseStdout, inspect(object, {
@@ -515,9 +525,9 @@ const consoleMethods = {
515525

516526
const _inspect = (v) => {
517527
const depth = v !== null &&
518-
typeof v === 'object' &&
519-
!isArray(v) &&
520-
ObjectKeys(v).length > 2 ? -1 : 0;
528+
typeof v === 'object' &&
529+
!isArray(v) &&
530+
ObjectKeys(v).length > 2 ? -1 : 0;
521531
const opt = {
522532
depth,
523533
maxArrayLength: 3,
@@ -587,7 +597,7 @@ const consoleMethods = {
587597
for (; i < indexKeyArray.length; i++) {
588598
const item = tabularData[indexKeyArray[i]];
589599
const primitive = item === null ||
590-
(typeof item !== 'function' && typeof item !== 'object');
600+
(typeof item !== 'function' && typeof item !== 'object');
591601
if (properties === undefined && primitive) {
592602
hasPrimitives = true;
593603
valuesKeyArray[i] = _inspect(item);
@@ -596,7 +606,7 @@ const consoleMethods = {
596606
for (const key of keys) {
597607
map[key] ??= [];
598608
if ((primitive && properties) ||
599-
!ObjectPrototypeHasOwnProperty(item, key))
609+
!ObjectPrototypeHasOwnProperty(item, key))
600610
map[key][i] = '';
601611
else
602612
map[key][i] = _inspect(item[key]);
@@ -689,7 +699,6 @@ for (const method of ReflectOwnKeys(consoleMethods))
689699
Console.prototype.debug = Console.prototype.log;
690700
Console.prototype.info = Console.prototype.log;
691701
Console.prototype.dirxml = Console.prototype.log;
692-
Console.prototype.error = Console.prototype.warn;
693702
Console.prototype.groupCollapsed = Console.prototype.group;
694703

695704
function initializeGlobalConsole(globalConsole) {

lib/internal/util/colors.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
'use strict';
22

3+
const {
4+
ObjectKeys,
5+
} = primordials;
6+
37
let internalTTy;
48
function lazyInternalTTY() {
59
internalTTy ??= require('internal/tty');
610
return internalTTy;
711
}
812

13+
const colorsMap = {
14+
blue: '\u001b[34m',
15+
green: '\u001b[32m',
16+
white: '\u001b[39m',
17+
yellow: '\u001b[33m',
18+
red: '\u001b[31m',
19+
gray: '\u001b[90m',
20+
clear: '\u001b[0m',
21+
};
22+
923
module.exports = {
10-
blue: '',
11-
green: '',
12-
white: '',
13-
red: '',
14-
gray: '',
15-
clear: '',
1624
hasColors: false,
1725
shouldColorize(stream) {
1826
if (process.env.FORCE_COLOR !== undefined) {
@@ -23,17 +31,13 @@ module.exports = {
2331
stream.getColorDepth() > 2 : true);
2432
},
2533
refresh() {
26-
if (process.stderr.isTTY) {
27-
const hasColors = module.exports.shouldColorize(process.stderr);
28-
module.exports.blue = hasColors ? '\u001b[34m' : '';
29-
module.exports.green = hasColors ? '\u001b[32m' : '';
30-
module.exports.white = hasColors ? '\u001b[39m' : '';
31-
module.exports.yellow = hasColors ? '\u001b[33m' : '';
32-
module.exports.red = hasColors ? '\u001b[31m' : '';
33-
module.exports.gray = hasColors ? '\u001b[90m' : '';
34-
module.exports.clear = hasColors ? '\u001bc' : '';
35-
module.exports.hasColors = hasColors;
36-
}
34+
const isTTY = process.stderr.isTTY;
35+
const hasColors = isTTY && module.exports.shouldColorize(process.stderr);
36+
ObjectKeys(colorsMap).forEach((key) => {
37+
module.exports[key] = hasColors ? colorsMap[key] : '';
38+
});
39+
40+
module.exports.hasColors = hasColors;
3741
},
3842
};
3943

test/parallel/test-repl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ const errorTests = [
770770
'Object [console] {',
771771
' log: [Function: log],',
772772
' warn: [Function: warn],',
773+
' error: [Function: error],',
773774
' dir: [Function: dir],',
774775
' time: [Function: time],',
775776
' timeEnd: [Function: timeEnd],',
@@ -785,7 +786,6 @@ const errorTests = [
785786
/ {2}debug: \[Function: (debug|log)],/,
786787
/ {2}info: \[Function: (info|log)],/,
787788
/ {2}dirxml: \[Function: (dirxml|log)],/,
788-
/ {2}error: \[Function: (error|warn)],/,
789789
/ {2}groupCollapsed: \[Function: (groupCollapsed|group)],/,
790790
/ {2}Console: \[Function: Console],?/,
791791
...process.features.inspector ? [
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
3-
(Use `* --trace-warnings ...` to show where the warning was created)
2+
[31m(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
3+
(Use `* --trace-warnings ...` to show where the warning was created)[0m
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
3-
(Use `* --trace-warnings ...` to show where the warning was created)
2+
[31m(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
3+
(Use `* --trace-warnings ...` to show where the warning was created)[0m
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
2-
(Use `* --trace-warnings ...` to show where the warning was created)
1+
[31m(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
2+
(Use `* --trace-warnings ...` to show where the warning was created)[0m

0 commit comments

Comments
 (0)