Skip to content

Commit 9c6ef5b

Browse files
suryaghjasnell
authored andcommitted
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: #6336 PR-URL: #6352 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 66f048a commit 9c6ef5b

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

doc/api/readline.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ the following values:
300300
treated like a TTY, and have ANSI/VT100 escape codes written to it.
301301
Defaults to checking `isTTY` on the `output` stream upon instantiation.
302302

303-
- `historySize` - maximum number of history lines retained. Defaults to `30`.
303+
- `historySize` - maximum number of history lines retained. To disable the
304+
history set this value to `0`. Defaults to `30`.
304305

305306
The `completer` function is given the current line entered by the user, and
306307
is supposed to return an Array with 2 entries:

lib/readline.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ function Interface(input, output, completer, terminal) {
5353
historySize = input.historySize;
5454
input = input.input;
5555
}
56-
historySize = historySize || kHistorySize;
5756

5857
if (completer && typeof completer !== 'function') {
5958
throw new TypeError('Argument "completer" must be a function');
6059
}
6160

61+
if (historySize === undefined) {
62+
historySize = kHistorySize;
63+
}
64+
6265
if (typeof historySize !== 'number' ||
6366
isNaN(historySize) ||
6467
historySize < 0) {
@@ -228,6 +231,9 @@ Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
228231
Interface.prototype._addHistory = function() {
229232
if (this.line.length === 0) return '';
230233

234+
// if the history is disabled then return the line
235+
if (this.historySize === 0) return this.line;
236+
231237
if (this.history.length === 0 || this.history[0] !== this.line) {
232238
this.history.unshift(this.line);
233239

test/parallel/test-readline-interface.js

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ function isWarned(emitter) {
2727
var rli;
2828
var called;
2929

30+
// disable history
31+
fi = new FakeInput();
32+
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal,
33+
historySize: 0 });
34+
assert.strictEqual(rli.historySize, 0);
35+
36+
fi.emit('data', 'asdf\n');
37+
assert.deepStrictEqual(rli.history, terminal ? [] : undefined);
38+
rli.close();
39+
40+
// default history size 30
41+
fi = new FakeInput();
42+
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal});
43+
assert.strictEqual(rli.historySize, 30);
44+
45+
fi.emit('data', 'asdf\n');
46+
assert.deepStrictEqual(rli.history, terminal ? ['asdf'] : undefined);
47+
rli.close();
48+
3049
// sending a full line
3150
fi = new FakeInput();
3251
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });

0 commit comments

Comments
 (0)