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 Feb 5, 2024
1 parent a501315 commit 3b4f498
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
13 changes: 10 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,12 @@ const consoleMethods = {


warn(...args) {
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args));
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), lazyUtilColors().yellow);
},

error(...args) {
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), lazyUtilColors().red);
},

dir(object, options) {
this[kWriteToConsole](kUseStdout, inspect(object, {
Expand Down Expand Up @@ -689,7 +697,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
40 changes: 22 additions & 18 deletions lib/internal/util/colors.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
'use strict';

const {
ObjectKeys

Check failure on line 4 in lib/internal/util/colors.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
} = 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'

Check failure on line 20 in lib/internal/util/colors.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
}

Check failure on line 21 in lib/internal/util/colors.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon

module.exports = {
blue: '',
green: '',
white: '',
red: '',
gray: '',
clear: '',
hasColors: false,
shouldColorize(stream) {
if (process.env.FORCE_COLOR !== undefined) {
Expand All @@ -23,18 +31,14 @@ 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 => {

Check failure on line 36 in lib/internal/util/colors.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected parentheses around arrow function argument
module.exports[key] = hasColors ? colorsMap[key] : '';

Check failure on line 37 in lib/internal/util/colors.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 6 spaces but found 10
});

module.exports.hasColors = hasColors;
}

Check failure on line 41 in lib/internal/util/colors.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
};

module.exports.refresh();
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 3b4f498

Please sign in to comment.