Skip to content

Commit 3a68b0b

Browse files
TrottMylesBorins
authored andcommitted
console: improve console.group()
Preserve indentation for multiline strings, objects that span multiple lines, etc. also make groupIndent non-enumerable Hide the internal `groupIndent` key a bit by making it non-enumerable and non-configurable. PR-URL: #14999 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent a46e59d commit 3a68b0b

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

lib/console.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function Console(stdout, stderr, ignoreErrors = true) {
5959
Object.defineProperty(this, '_stderrErrorHandler', prop);
6060

6161
this[kCounts] = new Map();
62+
63+
Object.defineProperty(this, kGroupIndent, { writable: true });
6264
this[kGroupIndent] = '';
6365

6466
// bind the prototype functions to this Console instance
@@ -85,7 +87,15 @@ function createWriteErrorHandler(stream) {
8587
};
8688
}
8789

88-
function write(ignoreErrors, stream, string, errorhandler) {
90+
function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
91+
if (groupIndent.length !== 0) {
92+
if (string.indexOf('\n') !== -1) {
93+
string = string.replace(/\n/g, `\n${groupIndent}`);
94+
}
95+
string = groupIndent + string;
96+
}
97+
string += '\n';
98+
8999
if (!ignoreErrors) return stream.write(string);
90100

91101
// There may be an error occurring synchronously (e.g. for files or TTYs
@@ -114,8 +124,9 @@ function write(ignoreErrors, stream, string, errorhandler) {
114124
Console.prototype.log = function log(...args) {
115125
write(this._ignoreErrors,
116126
this._stdout,
117-
`${this[kGroupIndent]}${util.format.apply(null, args)}\n`,
118-
this._stdoutErrorHandler);
127+
util.format.apply(null, args),
128+
this._stdoutErrorHandler,
129+
this[kGroupIndent]);
119130
};
120131

121132

@@ -125,8 +136,9 @@ Console.prototype.info = Console.prototype.log;
125136
Console.prototype.warn = function warn(...args) {
126137
write(this._ignoreErrors,
127138
this._stderr,
128-
`${this[kGroupIndent]}${util.format.apply(null, args)}\n`,
129-
this._stderrErrorHandler);
139+
util.format.apply(null, args),
140+
this._stderrErrorHandler,
141+
this[kGroupIndent]);
130142
};
131143

132144

@@ -137,8 +149,9 @@ Console.prototype.dir = function dir(object, options) {
137149
options = Object.assign({customInspect: false}, options);
138150
write(this._ignoreErrors,
139151
this._stdout,
140-
`${this[kGroupIndent]}${util.inspect(object, options)}\n`,
141-
this._stdoutErrorHandler);
152+
util.inspect(object, options),
153+
this._stdoutErrorHandler,
154+
this[kGroupIndent]);
142155
};
143156

144157

test/parallel/test-console-group.js

+36
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,39 @@ function teardown() {
109109
assert.strictEqual(stderr, expectedErr);
110110
teardown();
111111
}
112+
113+
// Check that multiline strings and object output are indented properly.
114+
{
115+
setup();
116+
const expectedOut = 'not indented\n' +
117+
' indented\n' +
118+
' also indented\n' +
119+
" { also: 'a',\n" +
120+
" multiline: 'object',\n" +
121+
" should: 'be',\n" +
122+
" indented: 'properly',\n" +
123+
" kthx: 'bai' }\n";
124+
const expectedErr = '';
125+
126+
c.log('not indented');
127+
c.group();
128+
c.log('indented\nalso indented');
129+
c.log({ also: 'a',
130+
multiline: 'object',
131+
should: 'be',
132+
indented: 'properly',
133+
kthx: 'bai' });
134+
135+
assert.strictEqual(stdout, expectedOut);
136+
assert.strictEqual(stderr, expectedErr);
137+
teardown();
138+
}
139+
140+
// Check that the kGroupIndent symbol property is not enumerable
141+
{
142+
const keys = Reflect.ownKeys(console)
143+
.filter((val) => console.propertyIsEnumerable(val))
144+
.map((val) => val.toString());
145+
assert(!keys.includes('Symbol(groupIndent)'),
146+
'groupIndent should not be enumerable');
147+
}

0 commit comments

Comments
 (0)