Browse Source

readline: allow history to be disabled

1.  The `historySize` to default to `30` only if `undefined`.
2.  If `historySize` is set to 0, then disable caching the line.
3.  Added unit tests.
4.  Updated documentation.

Fixes: https://github.com/nodejs/node/issues/6336
PR-URL: https://github.com/nodejs/node/pull/6352
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
process-exit-stdio-flushing
surya panikkal 9 years ago
committed by Anna Henningsen
parent
commit
0303a2552e
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 3
      doc/api/readline.md
  2. 8
      lib/readline.js
  3. 19
      test/parallel/test-readline-interface.js

3
doc/api/readline.md

@ -300,7 +300,8 @@ the following values:
treated like a TTY, and have ANSI/VT100 escape codes written to it. treated like a TTY, and have ANSI/VT100 escape codes written to it.
Defaults to checking `isTTY` on the `output` stream upon instantiation. Defaults to checking `isTTY` on the `output` stream upon instantiation.
- `historySize` - maximum number of history lines retained. Defaults to `30`. - `historySize` - maximum number of history lines retained. To disable the
history set this value to `0`. Defaults to `30`.
The `completer` function is given the current line entered by the user, and The `completer` function is given the current line entered by the user, and
is supposed to return an Array with 2 entries: is supposed to return an Array with 2 entries:

8
lib/readline.js

@ -53,12 +53,15 @@ function Interface(input, output, completer, terminal) {
historySize = input.historySize; historySize = input.historySize;
input = input.input; input = input.input;
} }
historySize = historySize || kHistorySize;
if (completer && typeof completer !== 'function') { if (completer && typeof completer !== 'function') {
throw new TypeError('Argument "completer" must be a function'); throw new TypeError('Argument "completer" must be a function');
} }
if (historySize === undefined) {
historySize = kHistorySize;
}
if (typeof historySize !== 'number' || if (typeof historySize !== 'number' ||
isNaN(historySize) || isNaN(historySize) ||
historySize < 0) { historySize < 0) {
@ -228,6 +231,9 @@ Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
Interface.prototype._addHistory = function() { Interface.prototype._addHistory = function() {
if (this.line.length === 0) return ''; if (this.line.length === 0) return '';
// if the history is disabled then return the line
if (this.historySize === 0) return this.line;
if (this.history.length === 0 || this.history[0] !== this.line) { if (this.history.length === 0 || this.history[0] !== this.line) {
this.history.unshift(this.line); this.history.unshift(this.line);

19
test/parallel/test-readline-interface.js

@ -27,6 +27,25 @@ function isWarned(emitter) {
var rli; var rli;
var called; var called;
// disable history
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal,
historySize: 0 });
assert.strictEqual(rli.historySize, 0);
fi.emit('data', 'asdf\n');
assert.deepStrictEqual(rli.history, terminal ? [] : undefined);
rli.close();
// default history size 30
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal});
assert.strictEqual(rli.historySize, 30);
fi.emit('data', 'asdf\n');
assert.deepStrictEqual(rli.history, terminal ? ['asdf'] : undefined);
rli.close();
// sending a full line // sending a full line
fi = new FakeInput(); fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });

Loading…
Cancel
Save