Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: Allow history to be disabled #6352

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ the following values:
treated like a TTY, and have ANSI/VT100 escape codes written to it.
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
is supposed to return an Array with 2 entries:
Expand Down
8 changes: 7 additions & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ function Interface(input, output, completer, terminal) {
historySize = input.historySize;
input = input.input;
}
historySize = historySize || kHistorySize;

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

if (historySize === undefined) {
historySize = kHistorySize;
}

if (typeof historySize !== 'number' ||
isNaN(historySize) ||
historySize < 0) {
Expand Down Expand Up @@ -228,6 +231,9 @@ Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
Interface.prototype._addHistory = function() {
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) {
this.history.unshift(this.line);

Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ function isWarned(emitter) {
var rli;
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
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
Expand Down