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 8747c98
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
15 changes: 12 additions & 3 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
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;
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color);
},

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

dir(object, options) {
this[kWriteToConsole](kUseStdout, inspect(object, {
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 8747c98

Please sign in to comment.