Browse Source

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>
canary-base
Rich Trott 7 years ago
parent
commit
c40229a9b8
  1. 27
      doc/api/console.md
  2. 24
      lib/console.js
  3. 111
      test/parallel/test-console-group.js

27
doc/api/console.md

@ -286,6 +286,32 @@ If formatting elements (e.g. `%d`) are not found in the first string then
[`util.inspect()`][] is called on each argument and the resulting string [`util.inspect()`][] is called on each argument and the resulting string
values are concatenated. See [`util.format()`][] for more information. values are concatenated. See [`util.format()`][] for more information.
### console.group([...label])
<!-- YAML
added: REPLACEME
-->
* `label` {any}
Increases indentation of subsequent lines by two spaces.
If one or more `label`s are provided, those are printed first without the
additional indentation.
### console.groupCollapsed()
<!-- YAML
added: REPLACEME
-->
An alias for [`console.group()`][].
### console.groupEnd()
<!-- YAML
added: REPLACEME
-->
Decreases indentation of subsequent lines by two spaces.
### console.info([data][, ...args]) ### console.info([data][, ...args])
<!-- YAML <!-- YAML
added: v0.1.100 added: v0.1.100
@ -390,6 +416,7 @@ added: v0.1.100
The `console.warn()` function is an alias for [`console.error()`][]. The `console.warn()` function is an alias for [`console.error()`][].
[`console.error()`]: #console_console_error_data_args [`console.error()`]: #console_console_error_data_args
[`console.group()`]: #console_console_group_label
[`console.log()`]: #console_console_log_data_args [`console.log()`]: #console_console_log_data_args
[`console.time()`]: #console_console_time_label [`console.time()`]: #console_console_time_label
[`console.timeEnd()`]: #console_console_timeend_label [`console.timeEnd()`]: #console_console_timeend_label

24
lib/console.js

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

111
test/parallel/test-console-group.js

@ -0,0 +1,111 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const Console = require('console').Console;
let c, stdout, stderr;
function setup() {
stdout = '';
common.hijackStdout(function(data) {
stdout += data;
});
stderr = '';
common.hijackStderr(function(data) {
stderr += data;
});
c = new Console(process.stdout, process.stderr);
}
function teardown() {
common.restoreStdout();
common.restoreStderr();
}
// Basic group() functionality
{
setup();
const expectedOut = 'This is the outer level\n' +
' Level 2\n' +
' Level 3\n' +
' Back to level 2\n' +
'Back to the outer level\n' +
'Still at the outer level\n';
const expectedErr = ' More of level 3\n';
c.log('This is the outer level');
c.group();
c.log('Level 2');
c.group();
c.log('Level 3');
c.warn('More of level 3');
c.groupEnd();
c.log('Back to level 2');
c.groupEnd();
c.log('Back to the outer level');
c.groupEnd();
c.log('Still at the outer level');
assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}
// Group indentation is tracked per Console instance.
{
setup();
const expectedOut = 'No indentation\n' +
'None here either\n' +
' Now the first console is indenting\n' +
'But the second one does not\n';
const expectedErr = '';
const c2 = new Console(process.stdout, process.stderr);
c.log('No indentation');
c2.log('None here either');
c.group();
c.log('Now the first console is indenting');
c2.log('But the second one does not');
assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}
// Make sure labels work.
{
setup();
const expectedOut = 'This is a label\n' +
' And this is the data for that label\n';
const expectedErr = '';
c.group('This is a label');
c.log('And this is the data for that label');
assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}
// Check that console.groupCollapsed() is an alias of console.group()
{
setup();
const expectedOut = 'Label\n' +
' Level 2\n' +
' Level 3\n';
const expectedErr = '';
c.groupCollapsed('Label');
c.log('Level 2');
c.groupCollapsed();
c.log('Level 3');
assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}
Loading…
Cancel
Save