From 8747c9897817716506b7336b64c894da7b436b73 Mon Sep 17 00:00:00 2001 From: Jithil P Ponnan Date: Thu, 1 Feb 2024 19:26:01 +1100 Subject: [PATCH] console: colorize console error and warn prints console error in red and warn in yellow --- lib/internal/console/constructor.js | 15 ++++++-- lib/internal/util/colors.js | 38 ++++++++++--------- test/parallel/test-repl.js | 2 +- .../test-tty-color-support-warning-2.out | 4 +- .../test-tty-color-support-warning.out | 4 +- test/pseudo-tty/test-tty-color-support.out | 4 +- 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 454652e3632472..73572ef19e4f40 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -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]; @@ -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); @@ -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, { @@ -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) { diff --git a/lib/internal/util/colors.js b/lib/internal/util/colors.js index 31e3e9b22585c5..1599a98f740326 100644 --- a/lib/internal/util/colors.js +++ b/lib/internal/util/colors.js @@ -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) { @@ -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; }, }; diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 98a0bc79c64d91..0d467e2b35dbdc 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -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],', @@ -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 ? [ diff --git a/test/pseudo-tty/test-tty-color-support-warning-2.out b/test/pseudo-tty/test-tty-color-support-warning-2.out index 37b470a5f108f9..14b5ded243647f 100644 --- a/test/pseudo-tty/test-tty-color-support-warning-2.out +++ b/test/pseudo-tty/test-tty-color-support-warning-2.out @@ -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 \ No newline at end of file diff --git a/test/pseudo-tty/test-tty-color-support-warning.out b/test/pseudo-tty/test-tty-color-support-warning.out index b25d2e42cf7244..24d43e5e3cd420 100644 --- a/test/pseudo-tty/test-tty-color-support-warning.out +++ b/test/pseudo-tty/test-tty-color-support-warning.out @@ -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 \ No newline at end of file diff --git a/test/pseudo-tty/test-tty-color-support.out b/test/pseudo-tty/test-tty-color-support.out index df5831c555be19..263ee51bb101f0 100644 --- a/test/pseudo-tty/test-tty-color-support.out +++ b/test/pseudo-tty/test-tty-color-support.out @@ -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 \ No newline at end of file