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: add option to stop duplicates in history #2982

Closed
wants to merge 1 commit 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: 3 additions & 0 deletions doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ changes:
`crlfDelay` milliseconds, both `\r` and `\n` will be treated as separate
end-of-line input. Default to `100` milliseconds.
`crlfDelay` will be coerced to `[100, 2000]` range.
* `deDupeHistory` {boolean} If `true`, when a new input line added to the
history list duplicates an older one, this removes the older line from the
list. Defaults to `false`.

The `readline.createInterface()` method creates a new `readline.Interface`
instance.
Expand Down
9 changes: 9 additions & 0 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function Interface(input, output, completer, terminal) {

EventEmitter.call(this);
var historySize;
var deDupeHistory = false;
let crlfDelay;
let prompt = '> ';

Expand All @@ -48,6 +49,7 @@ function Interface(input, output, completer, terminal) {
completer = input.completer;
terminal = input.terminal;
historySize = input.historySize;
deDupeHistory = input.deDupeHistory;
if (input.prompt !== undefined) {
prompt = input.prompt;
}
Expand Down Expand Up @@ -80,6 +82,7 @@ function Interface(input, output, completer, terminal) {
this.output = output;
this.input = input;
this.historySize = historySize;
this.deDupeHistory = !!deDupeHistory;
this.crlfDelay = Math.max(kMincrlfDelay,
Math.min(kMaxcrlfDelay, crlfDelay >>> 0));

Expand Down Expand Up @@ -257,6 +260,12 @@ Interface.prototype._addHistory = function() {
if (this.line.trim().length === 0) return this.line;

if (this.history.length === 0 || this.history[0] !== this.line) {
if (this.deDupeHistory) {
// Remove older history line if identical to new one
const dupIndex = this.history.indexOf(this.line);
if (dupIndex !== -1) this.history.splice(dupIndex, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have preferred filter over this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't Array.prototype.filter() be slower? The method would always create a new array whether or not a duplicate exists, and would traverse the entire this.history array even after finding a match/duplicate, though there can never be more than one duplicate.

}

this.history.unshift(this.line);

// Only store so many
Expand Down
61 changes: 61 additions & 0 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,67 @@ function isWarned(emitter) {
return false;
});

// duplicate lines are removed from history when `options.deDupeHistory`
// is `true`
fi = new FakeInput();
rli = new readline.Interface({
input: fi,
output: fi,
terminal: true,
deDupeHistory: true
});
expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join('\n') + '\n');
assert.strictEqual(callCount, expectedLines.length);
fi.emit('keypress', '.', { name: 'up' }); // 'bat'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'bar'
assert.notStrictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'baz'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'foo'
assert.notStrictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(callCount, 0);
rli.close();

// duplicate lines are not removed from history when `options.deDupeHistory`
// is `false`
fi = new FakeInput();
rli = new readline.Interface({
input: fi,
output: fi,
terminal: true,
deDupeHistory: false
});
expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
callCount = 0;
rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', expectedLines.join('\n') + '\n');
assert.strictEqual(callCount, expectedLines.length);
fi.emit('keypress', '.', { name: 'up' }); // 'bat'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'bar'
assert.notStrictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'baz'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'bar'
assert.strictEqual(rli.line, expectedLines[--callCount]);
fi.emit('keypress', '.', { name: 'up' }); // 'foo'
assert.strictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(callCount, 0);
rli.close();

// sending a multi-byte utf8 char over multiple writes
const buf = Buffer.from('☮', 'utf8');
fi = new FakeInput();
Expand Down