Skip to content

Commit

Permalink
readline,repl: skip history entries identical to the current line
Browse files Browse the repository at this point in the history
Skip history entries that are identical to the currently visible line
to improve the user experience.

PR-URL: nodejs#31112
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Apr 25, 2020
1 parent bf62a83 commit cb0a321
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,8 @@ Interface.prototype._historyNext = function() {
const search = this[kSubstringSearch] || '';
let index = this.historyIndex - 1;
while (index >= 0 &&
!this.history[index].startsWith(search)) {
(!this.history[index].startsWith(search) ||
this.line === this.history[index])) {
index--;
}
if (index === -1) {
Expand All @@ -721,10 +722,13 @@ Interface.prototype._historyPrev = function() {
const search = this[kSubstringSearch] || '';
let index = this.historyIndex + 1;
while (index < this.history.length &&
!this.history[index].startsWith(search)) {
(!this.history[index].startsWith(search) ||
this.line === this.history[index])) {
index++;
}
if (index === this.history.length) {
// TODO(BridgeAR): Change this to:
// this.line = search;
return;
} else {
this.line = this.history[index];
Expand Down
4 changes: 0 additions & 4 deletions test/parallel/test-repl-history-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ const tests = [
// UP - skipping const foo = true
'\x1B[1G', '\x1B[0J',
'> 555 + 909', '\x1B[12G',
// UP - matching the identical history entry again.
'\x1B[1G', '\x1B[0J',
'> 555 + 909',
// UP, UP, ENTER. UPs at the end of the history have no effect.
'\x1B[12G',
'\r\n',
'1464\n',
'\x1B[1G', '\x1B[0J',
Expand Down

0 comments on commit cb0a321

Please sign in to comment.