From 483dec7b9b16556af3bae900d84e5af5c57119cc Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 23 Aug 2017 20:36:54 -0700 Subject: [PATCH 1/2] console: improve console.group() Preserve indentation for multiline strings, objects that span multiple lines, etc. --- lib/console.js | 25 ++++++++++++++++++------- test/parallel/test-console-group.js | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lib/console.js b/lib/console.js index 13e3c05631aad1..2264d41b3b085d 100644 --- a/lib/console.js +++ b/lib/console.js @@ -86,7 +86,15 @@ function createWriteErrorHandler(stream) { }; } -function write(ignoreErrors, stream, string, errorhandler) { +function write(ignoreErrors, stream, string, errorhandler, groupIndent) { + if (groupIndent.length !== 0) { + if (string.indexOf('\n') !== -1) { + string = string.replace(/\n/g, `\n${groupIndent}`); + } + string = groupIndent + string; + } + string += '\n'; + if (!ignoreErrors) return stream.write(string); // There may be an error occurring synchronously (e.g. for files or TTYs @@ -115,8 +123,9 @@ function write(ignoreErrors, stream, string, errorhandler) { Console.prototype.log = function log(...args) { write(this._ignoreErrors, this._stdout, - `${this[kGroupIndent]}${util.format.apply(null, args)}\n`, - this._stdoutErrorHandler); + util.format.apply(null, args), + this._stdoutErrorHandler, + this[kGroupIndent]); }; @@ -126,8 +135,9 @@ Console.prototype.info = Console.prototype.log; Console.prototype.warn = function warn(...args) { write(this._ignoreErrors, this._stderr, - `${this[kGroupIndent]}${util.format.apply(null, args)}\n`, - this._stderrErrorHandler); + util.format.apply(null, args), + this._stderrErrorHandler, + this[kGroupIndent]); }; @@ -138,8 +148,9 @@ Console.prototype.dir = function dir(object, options) { options = Object.assign({ customInspect: false }, options); write(this._ignoreErrors, this._stdout, - `${this[kGroupIndent]}${util.inspect(object, options)}\n`, - this._stdoutErrorHandler); + util.inspect(object, options), + this._stdoutErrorHandler, + this[kGroupIndent]); }; diff --git a/test/parallel/test-console-group.js b/test/parallel/test-console-group.js index 8f7d84f2f17e99..ac965790423cf1 100644 --- a/test/parallel/test-console-group.js +++ b/test/parallel/test-console-group.js @@ -109,3 +109,30 @@ function teardown() { assert.strictEqual(stderr, expectedErr); teardown(); } + +// Check that multiline strings and object output are indented properly. +{ + setup(); + const expectedOut = 'not indented\n' + + ' indented\n' + + ' also indented\n' + + " { also: 'a',\n" + + " multiline: 'object',\n" + + " should: 'be',\n" + + " indented: 'properly',\n" + + " kthx: 'bai' }\n"; + const expectedErr = ''; + + c.log('not indented'); + c.group(); + c.log('indented\nalso indented'); + c.log({ also: 'a', + multiline: 'object', + should: 'be', + indented: 'properly', + kthx: 'bai' }); + + assert.strictEqual(stdout, expectedOut); + assert.strictEqual(stderr, expectedErr); + teardown(); +} From 9df027b6cc1c61ef75dcdc2f1918fec3f238bb44 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 23 Aug 2017 22:16:05 -0700 Subject: [PATCH 2/2] console: make groupIndent non-enumerable Hide the internal `groupIndent` key a bit by making it non-enumerable and non-configurable. --- lib/console.js | 2 ++ test/parallel/test-console-group.js | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/console.js b/lib/console.js index 2264d41b3b085d..54c8aba8299565 100644 --- a/lib/console.js +++ b/lib/console.js @@ -60,6 +60,8 @@ function Console(stdout, stderr, ignoreErrors = true) { Object.defineProperty(this, '_stderrErrorHandler', prop); this[kCounts] = new Map(); + + Object.defineProperty(this, kGroupIndent, { writable: true }); this[kGroupIndent] = ''; // bind the prototype functions to this Console instance diff --git a/test/parallel/test-console-group.js b/test/parallel/test-console-group.js index ac965790423cf1..8486d1a7ace367 100644 --- a/test/parallel/test-console-group.js +++ b/test/parallel/test-console-group.js @@ -136,3 +136,12 @@ function teardown() { assert.strictEqual(stderr, expectedErr); teardown(); } + +// Check that the kGroupIndent symbol property is not enumerable +{ + const keys = Reflect.ownKeys(console) + .filter((val) => console.propertyIsEnumerable(val)) + .map((val) => val.toString()); + assert(!keys.includes('Symbol(groupIndent)'), + 'groupIndent should not be enumerable'); +}