| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d169d0f commit db76c58
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ const { | |||
| 8 | 8 | ArrayIsArray, | |
| 9 | 9 | ArrayPrototypeForEach, | |
| 10 | 10 | ArrayPrototypePush, | |
| 11 | + ArrayPrototypeSome, | ||
| 11 | 12 | ArrayPrototypeUnshift, | |
| 12 | 13 | Boolean, | |
| 13 | 14 | ErrorCaptureStackTrace, | |
@@ -67,6 +68,7 @@ const { | |||
| 67 | 68 | CHAR_LOWERCASE_N: kTraceInstant, | |
| 68 | 69 | CHAR_UPPERCASE_C: kTraceCount, | |
| 69 | 70 | } = require('internal/constants'); | |
| 71 | + const { styleText } = require('util'); | ||
| 70 | 72 | const kCounts = Symbol('counts'); | |
| 71 | 73 | ||
| 72 | 74 | const kTraceConsoleCategory = 'node,node.console'; | |
@@ -273,7 +275,7 @@ ObjectDefineProperties(Console.prototype, { | |||
| 273 | 275 | [kWriteToConsole]: { | |
| 274 | 276 | __proto__: null, | |
| 275 | 277 | ...consolePropAttributes, | |
| 276 | - value: function(streamSymbol, string) { | ||
| 278 | + value: function(streamSymbol, string, color = '') { | ||
| 277 | 279 | const ignoreErrors = this._ignoreErrors; | |
| 278 | 280 | const groupIndent = this[kGroupIndent]; | |
| 279 | 281 | ||
@@ -288,6 +290,11 @@ ObjectDefineProperties(Console.prototype, { | |||
| 288 | 290 | } | |
| 289 | 291 | string = groupIndent + string; | |
| 290 | 292 | } | |
| 293 | + | ||
| 294 | + if (color) { | ||
| 295 | + string = styleText(color, string); | ||
| 296 | + } | ||
| 297 | + | ||
| 291 | 298 | string += '\n'; | |
| 292 | 299 | ||
| 293 | 300 | if (ignoreErrors === false) return stream.write(string); | |
@@ -378,12 +385,15 @@ const consoleMethods = { | |||
| 378 | 385 | log(...args) { | |
| 379 | 386 | this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args)); | |
| 380 | 387 | }, | |
| 381 | - | ||
| 382 | - | ||
| 383 | 388 | warn(...args) { | |
| 384 | - this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args)); | ||
| 389 | + const color = (shouldColorize(args) && 'yellow') || ''; | ||
| 390 | + this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color); | ||
| 385 | 391 | }, | |
| 386 | 392 | ||
| 393 | + error(...args) { | ||
| 394 | + const color = (shouldColorize(args) && 'red') || ''; | ||
| 395 | + this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color); | ||
| 396 | + }, | ||
| 387 | 397 | ||
| 388 | 398 | dir(object, options) { | |
| 389 | 399 | this[kWriteToConsole](kUseStdout, inspect(object, { | |
@@ -681,6 +691,12 @@ const iterKey = '(iteration index)'; | |||
| 681 | 691 | ||
| 682 | 692 | const isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v); | |
| 683 | 693 | ||
| 694 | + // TODO: remove string type check once the styleText supports objects | ||
| 695 | + // Return true if all args are type string | ||
| 696 | + const shouldColorize = (args) => { | ||
| 697 | + return lazyUtilColors().hasColors && !ArrayPrototypeSome(args, (arg) => typeof arg !== 'string'); | ||
| 698 | + }; | ||
| 699 | + | ||
| 684 | 700 | function noop() {} | |
| 685 | 701 | ||
| 686 | 702 | for (const method of ReflectOwnKeys(consoleMethods)) | |
@@ -689,7 +705,6 @@ for (const method of ReflectOwnKeys(consoleMethods)) | |||
| 689 | 705 | Console.prototype.debug = Console.prototype.log; | |
| 690 | 706 | Console.prototype.info = Console.prototype.log; | |
| 691 | 707 | Console.prototype.dirxml = Console.prototype.log; | |
| 692 | - Console.prototype.error = Console.prototype.warn; | ||
| 693 | 708 | Console.prototype.groupCollapsed = Console.prototype.group; | |
| 694 | 709 | ||
| 695 | 710 | function initializeGlobalConsole(globalConsole) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -770,6 +770,7 @@ const errorTests = [ | |||
| 770 | 770 | 'Object [console] {', | |
| 771 | 771 | ' log: [Function: log],', | |
| 772 | 772 | ' warn: [Function: warn],', | |
| 773 | + ' error: [Function: error],', | ||
| 773 | 774 | ' dir: [Function: dir],', | |
| 774 | 775 | ' time: [Function: time],', | |
| 775 | 776 | ' timeEnd: [Function: timeEnd],', | |
@@ -785,7 +786,6 @@ const errorTests = [ | |||
| 785 | 786 | / {2}debug: \[Function: (debug|log)],/, | |
| 786 | 787 | / {2}info: \[Function: (info|log)],/, | |
| 787 | 788 | / {2}dirxml: \[Function: (dirxml|log)],/, | |
| 788 | - / {2}error: \[Function: (error|warn)],/, | ||
| 789 | 789 | / {2}groupCollapsed: \[Function: (groupCollapsed|group)],/, | |
| 790 | 790 | / {2}Console: \[Function: Console],?/, | |
| 791 | 791 | ...process.features.inspector ? [ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,3 @@ | |||
| 1 | 1 | ||
| 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 | + *(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)* | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,3 @@ | |||
| 1 | 1 | ||
| 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 | + *(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)* | ||
| Original file line number | Diff line number | Diff 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 | + *(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)* | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments