Skip to content

Commit

Permalink
console: colorize console error and warn
Browse files Browse the repository at this point in the history
prints console error in red and warn in yellow
  • Loading branch information
MrJithil committed Mar 20, 2024
1 parent 639c096 commit 471a7d4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
29 changes: 19 additions & 10 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {

if (groupIndentation !== undefined) {
validateInteger(groupIndentation, 'groupIndentation',
0, kMaxGroupIndentation);
0, kMaxGroupIndentation);

Check failure on line 141 in lib/internal/console/constructor.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 20 spaces but found 6
}

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

if (inspectOptions.colors !== undefined &&
options.colorMode !== undefined) {
options.colorMode !== undefined) {
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
'options.inspectOptions.color', 'colorMode');
}
Expand Down Expand Up @@ -273,7 +273,7 @@ ObjectDefineProperties(Console.prototype, {
[kWriteToConsole]: {
__proto__: null,
...consolePropAttributes,
value: function(streamSymbol, string) {
value: function(streamSymbol, string, color) {
const ignoreErrors = this._ignoreErrors;
const groupIndent = this[kGroupIndent];

Expand All @@ -288,6 +288,11 @@ ObjectDefineProperties(Console.prototype, {
}
string = groupIndent + string;
}

if (color && lazyUtilColors().hasColors) {
string = `${color}${string}${lazyUtilColors().clear}`;
}

string += '\n';

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


warn(...args) {
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args));
const color = typeof args === 'string'? lazyUtilColors().yellow : null;

Check failure on line 389 in lib/internal/console/constructor.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Operator '?' must be spaced
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color);
},

error(...args) {
const color = typeof args === 'string'? lazyUtilColors().red : null;

Check failure on line 394 in lib/internal/console/constructor.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Operator '?' must be spaced
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color);
},

dir(object, options) {
this[kWriteToConsole](kUseStdout, inspect(object, {
Expand Down Expand Up @@ -515,9 +525,9 @@ const consoleMethods = {

const _inspect = (v) => {
const depth = v !== null &&
typeof v === 'object' &&
!isArray(v) &&
ObjectKeys(v).length > 2 ? -1 : 0;
typeof v === 'object' &&
!isArray(v) &&
ObjectKeys(v).length > 2 ? -1 : 0;
const opt = {
depth,
maxArrayLength: 3,
Expand Down Expand Up @@ -587,7 +597,7 @@ const consoleMethods = {
for (; i < indexKeyArray.length; i++) {
const item = tabularData[indexKeyArray[i]];
const primitive = item === null ||
(typeof item !== 'function' && typeof item !== 'object');
(typeof item !== 'function' && typeof item !== 'object');
if (properties === undefined && primitive) {
hasPrimitives = true;
valuesKeyArray[i] = _inspect(item);
Expand All @@ -596,7 +606,7 @@ const consoleMethods = {
for (const key of keys) {
map[key] ??= [];
if ((primitive && properties) ||
!ObjectPrototypeHasOwnProperty(item, key))
!ObjectPrototypeHasOwnProperty(item, key))
map[key][i] = '';
else
map[key][i] = _inspect(item[key]);
Expand Down Expand Up @@ -689,7 +699,6 @@ for (const method of ReflectOwnKeys(consoleMethods))
Console.prototype.debug = Console.prototype.log;
Console.prototype.info = Console.prototype.log;
Console.prototype.dirxml = Console.prototype.log;
Console.prototype.error = Console.prototype.warn;
Console.prototype.groupCollapsed = Console.prototype.group;

function initializeGlobalConsole(globalConsole) {
Expand Down
38 changes: 21 additions & 17 deletions lib/internal/util/colors.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
'use strict';

const {
ObjectKeys,
} = primordials;

let internalTTy;
function lazyInternalTTY() {
internalTTy ??= require('internal/tty');
return internalTTy;
}

const colorsMap = {
blue: '\u001b[34m',
green: '\u001b[32m',
white: '\u001b[39m',
yellow: '\u001b[33m',
red: '\u001b[31m',
gray: '\u001b[90m',
clear: '\u001b[0m',
};

module.exports = {
blue: '',
green: '',
white: '',
red: '',
gray: '',
clear: '',
hasColors: false,
shouldColorize(stream) {
if (process.env.FORCE_COLOR !== undefined) {
Expand All @@ -23,17 +31,13 @@ module.exports = {
stream.getColorDepth() > 2 : true);
},
refresh() {
if (process.stderr.isTTY) {
const hasColors = module.exports.shouldColorize(process.stderr);
module.exports.blue = hasColors ? '\u001b[34m' : '';
module.exports.green = hasColors ? '\u001b[32m' : '';
module.exports.white = hasColors ? '\u001b[39m' : '';
module.exports.yellow = hasColors ? '\u001b[33m' : '';
module.exports.red = hasColors ? '\u001b[31m' : '';
module.exports.gray = hasColors ? '\u001b[90m' : '';
module.exports.clear = hasColors ? '\u001bc' : '';
module.exports.hasColors = hasColors;
}
const isTTY = process.stderr.isTTY;
const hasColors = isTTY && module.exports.shouldColorize(process.stderr);
ObjectKeys(colorsMap).forEach((key) => {
module.exports[key] = hasColors ? colorsMap[key] : '';
});

module.exports.hasColors = hasColors;
},
};

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ const errorTests = [
'Object [console] {',
' log: [Function: log],',
' warn: [Function: warn],',
' error: [Function: error],',
' dir: [Function: dir],',
' time: [Function: time],',
' timeEnd: [Function: timeEnd],',
Expand All @@ -785,7 +786,6 @@ const errorTests = [
/ {2}debug: \[Function: (debug|log)],/,
/ {2}info: \[Function: (info|log)],/,
/ {2}dirxml: \[Function: (dirxml|log)],/,
/ {2}error: \[Function: (error|warn)],/,
/ {2}groupCollapsed: \[Function: (groupCollapsed|group)],/,
/ {2}Console: \[Function: Console],?/,
...process.features.inspector ? [
Expand Down
4 changes: 2 additions & 2 deletions test/pseudo-tty/test-tty-color-support-warning-2.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `* --trace-warnings ...` to show where the warning was created)
[31m(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `* --trace-warnings ...` to show where the warning was created)[0m
4 changes: 2 additions & 2 deletions test/pseudo-tty/test-tty-color-support-warning.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `* --trace-warnings ...` to show where the warning was created)
[31m(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `* --trace-warnings ...` to show where the warning was created)[0m
4 changes: 2 additions & 2 deletions test/pseudo-tty/test-tty-color-support.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `* --trace-warnings ...` to show where the warning was created)
[31m(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `* --trace-warnings ...` to show where the warning was created)[0m

0 comments on commit 471a7d4

Please sign in to comment.