console: implement minimal console.group()

Node.js exposes `console.group()` and `console.groupEnd()` via the
inspector. These functions have no apparent effect when called from
Node.js without the inspector. We cannot easily hide them when Node.js
is started without the inspector because we support opening the
inspector during runtime via `inspector.port()`.

Implement a minimal `console.group()`/`console.groupEnd()`. More
sophisticated implementations are possible, but they can be done in
userland and/or features can be added to this at a later time.

`console.groupCollapsed()` is implemented as an alias for
`console.group()`.

PR-URL: https://github.com/nodejs/node/pull/14910
Fixes: https://github.com/nodejs/node/issues/1716
Ref: https://github.com/nodejs/node/issues/12675
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Rich Trott 2017-08-17 22:41:14 -07:00
parent c6da5c8cdf
commit c40229a9b8
3 changed files with 159 additions and 3 deletions

View file

@ -25,6 +25,9 @@ const errors = require('internal/errors');
const util = require('util');
const kCounts = Symbol('counts');
// Track amount of indentation required via `console.group()`.
const kGroupIndent = Symbol('groupIndent');
function Console(stdout, stderr, ignoreErrors = true) {
if (!(this instanceof Console)) {
return new Console(stdout, stderr, ignoreErrors);
@ -57,6 +60,7 @@ function Console(stdout, stderr, ignoreErrors = true) {
Object.defineProperty(this, '_stderrErrorHandler', prop);
this[kCounts] = new Map();
this[kGroupIndent] = '';
// bind the prototype functions to this Console instance
var keys = Object.keys(Console.prototype);
@ -111,7 +115,7 @@ function write(ignoreErrors, stream, string, errorhandler) {
Console.prototype.log = function log(...args) {
write(this._ignoreErrors,
this._stdout,
`${util.format.apply(null, args)}\n`,
`${this[kGroupIndent]}${util.format.apply(null, args)}\n`,
this._stdoutErrorHandler);
};
@ -122,7 +126,7 @@ Console.prototype.info = Console.prototype.log;
Console.prototype.warn = function warn(...args) {
write(this._ignoreErrors,
this._stderr,
`${util.format.apply(null, args)}\n`,
`${this[kGroupIndent]}${util.format.apply(null, args)}\n`,
this._stderrErrorHandler);
};
@ -134,7 +138,7 @@ Console.prototype.dir = function dir(object, options) {
options = Object.assign({ customInspect: false }, options);
write(this._ignoreErrors,
this._stdout,
`${util.inspect(object, options)}\n`,
`${this[kGroupIndent]}${util.inspect(object, options)}\n`,
this._stdoutErrorHandler);
};
@ -214,6 +218,20 @@ Console.prototype.countReset = function countReset(label = 'default') {
counts.delete(`${label}`);
};
Console.prototype.group = function group(...data) {
if (data.length > 0) {
this.log(...data);
}
this[kGroupIndent] += ' ';
};
Console.prototype.groupCollapsed = Console.prototype.group;
Console.prototype.groupEnd = function groupEnd() {
this[kGroupIndent] =
this[kGroupIndent].slice(0, this[kGroupIndent].length - 2);
};
module.exports = new Console(process.stdout, process.stderr);
module.exports.Console = Console;