mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
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: https://github.com/nodejs/node/pull/14999 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
This commit is contained in:
1 parent
a1d34b3f49
commit
af11867b41
2 files changed
+56
-7
No files matched your search
+20
-7
@@ -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
|
||||
@@ -86,7 +88,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 +125,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 +137,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 +150,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]);
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user