diff options
author | rickyes <mail@zhoumq.cn> | 2020-04-21 12:51:25 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-04-29 04:51:03 +0300 |
commit | f68428b695468f9e3159ea7eea1f87be73ae0de1 (patch) | |
tree | 440371539dee8fe33c9b50bbf33fa38c377824f3 /lib/internal/console | |
parent | 38146e717fed2fabe3aacb6540d839475e0ce1c6 (diff) |
console: support console constructor groupIndentation option
PR-URL: https://github.com/nodejs/node/pull/32964
Fixes: https://github.com/nodejs/node/issues/32947
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/console')
-rw-r--r-- | lib/internal/console/constructor.js | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index dc8dcaa1d05..c5195c77c6e 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -32,6 +32,7 @@ const { ERR_INCOMPATIBLE_OPTION_PAIR, }, } = require('internal/errors'); +const { validateInteger } = require('internal/validators'); const { previewEntries } = internalBinding('util'); const { Buffer: { isBuffer } } = require('buffer'); const { @@ -52,12 +53,14 @@ const kTraceInstant = 'n'.charCodeAt(0); const kSecond = 1000; const kMinute = 60 * kSecond; const kHour = 60 * kMinute; +const kMaxGroupIndentation = 1000; // Lazy loaded for startup performance. let cliTable; // Track amount of indentation required via `console.group()`. const kGroupIndent = Symbol('kGroupIndent'); +const kGroupIndentationWidth = Symbol('kGroupIndentWidth'); const kFormatForStderr = Symbol('kFormatForStderr'); const kFormatForStdout = Symbol('kFormatForStdout'); const kGetInspectOptions = Symbol('kGetInspectOptions'); @@ -93,7 +96,8 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { stderr = stdout, ignoreErrors = true, colorMode = 'auto', - inspectOptions + inspectOptions, + groupIndentation, } = options; if (!stdout || typeof stdout.write !== 'function') { @@ -106,6 +110,11 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { if (typeof colorMode !== 'boolean' && colorMode !== 'auto') throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode); + if (groupIndentation !== undefined) { + validateInteger(groupIndentation, 'groupIndentation', + 0, kMaxGroupIndentation); + } + if (typeof inspectOptions === 'object' && inspectOptions !== null) { if (inspectOptions.colors !== undefined && options.colorMode !== undefined) { @@ -130,7 +139,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { } this[kBindStreamsEager](stdout, stderr); - this[kBindProperties](ignoreErrors, colorMode); + this[kBindProperties](ignoreErrors, colorMode, groupIndentation); } const consolePropAttributes = { @@ -181,7 +190,8 @@ Console.prototype[kBindStreamsLazy] = function(object) { }); }; -Console.prototype[kBindProperties] = function(ignoreErrors, colorMode) { +Console.prototype[kBindProperties] = function(ignoreErrors, colorMode, + groupIndentation = 2) { ObjectDefineProperties(this, { '_stdoutErrorHandler': { ...consolePropAttributes, @@ -200,7 +210,11 @@ Console.prototype[kBindProperties] = function(ignoreErrors, colorMode) { [kCounts]: { ...consolePropAttributes, value: new Map() }, [kColorMode]: { ...consolePropAttributes, value: colorMode }, [kIsConsole]: { ...consolePropAttributes, value: true }, - [kGroupIndent]: { ...consolePropAttributes, value: '' } + [kGroupIndent]: { ...consolePropAttributes, value: '' }, + [kGroupIndentationWidth]: { + ...consolePropAttributes, + value: groupIndentation + }, }); }; @@ -403,12 +417,13 @@ const consoleMethods = { if (data.length > 0) { this.log(...data); } - this[kGroupIndent] += ' '; + this[kGroupIndent] += ' '.repeat(this[kGroupIndentationWidth]); }, groupEnd() { this[kGroupIndent] = - this[kGroupIndent].slice(0, this[kGroupIndent].length - 2); + this[kGroupIndent].slice(0, this[kGroupIndent].length - + this[kGroupIndentationWidth]); }, // https://console.spec.whatwg.org/#table |