Skip to content

Commit 5ac572c

Browse files
committed
readline: allow history to be disabled
fix #6336 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.
1 parent 7940ecf commit 5ac572c

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

doc/api/readline.md

Lines changed: 2 additions & 1 deletion
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ 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+
rli.close();
36+
37+
// default history size 30
38+
fi = new FakeInput();
39+
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal});
40+
assert.strictEqual(rli.historySize, 30);
41+
rli.close();
42+
3043
// sending a full line
3144
fi = new FakeInput();
3245
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });

0 commit comments

Comments
 (0)