| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 17d16e8 commit 8ab26cf
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -370,6 +370,9 @@ changes: | |||
| 370 | 370 | `crlfDelay` milliseconds, both `\r` and `\n` will be treated as separate | |
| 371 | 371 | end-of-line input. Default to `100` milliseconds. | |
| 372 | 372 | `crlfDelay` will be coerced to `[100, 2000]` range. | |
| 373 | + * `deDupeHistory` {boolean} If `true`, when a new input line added to the | ||
| 374 | + history list duplicates an older one, this removes the older line from the | ||
| 375 | + list. Defaults to `false`. | ||
| 373 | 376 | ||
| 374 | 377 | The `readline.createInterface()` method creates a new `readline.Interface` | |
| 375 | 378 | instance. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,6 +39,7 @@ function Interface(input, output, completer, terminal) { | |||
| 39 | 39 | ||
| 40 | 40 | EventEmitter.call(this); | |
| 41 | 41 | var historySize; | |
| 42 | + var deDupeHistory = false; | ||
| 42 | 43 | let crlfDelay; | |
| 43 | 44 | let prompt = '> '; | |
| 44 | 45 | ||
@@ -48,6 +49,7 @@ function Interface(input, output, completer, terminal) { | |||
| 48 | 49 | completer = input.completer; | |
| 49 | 50 | terminal = input.terminal; | |
| 50 | 51 | historySize = input.historySize; | |
| 52 | + deDupeHistory = input.deDupeHistory; | ||
| 51 | 53 | if (input.prompt !== undefined) { | |
| 52 | 54 | prompt = input.prompt; | |
| 53 | 55 | } | |
@@ -80,6 +82,7 @@ function Interface(input, output, completer, terminal) { | |||
| 80 | 82 | this.output = output; | |
| 81 | 83 | this.input = input; | |
| 82 | 84 | this.historySize = historySize; | |
| 85 | + this.deDupeHistory = !!deDupeHistory; | ||
| 83 | 86 | this.crlfDelay = Math.max(kMincrlfDelay, | |
| 84 | 87 | Math.min(kMaxcrlfDelay, crlfDelay >>> 0)); | |
| 85 | 88 | ||
@@ -257,6 +260,12 @@ Interface.prototype._addHistory = function() { | |||
| 257 | 260 | if (this.line.trim().length === 0) return this.line; | |
| 258 | 261 | ||
| 259 | 262 | if (this.history.length === 0 || this.history[0] !== this.line) { | |
| 263 | + if (this.deDupeHistory) { | ||
| 264 | + // Remove older history line if identical to new one | ||
| 265 | + const dupIndex = this.history.indexOf(this.line); | ||
| 266 | + if (dupIndex !== -1) this.history.splice(dupIndex, 1); | ||
| 267 | + } | ||
| 268 | + | ||
| 260 | 269 | this.history.unshift(this.line); | |
| 261 | 270 | ||
| 262 | 271 | // Only store so many | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -305,6 +305,67 @@ function isWarned(emitter) { | |||
| 305 | 305 | return false; | |
| 306 | 306 | }); | |
| 307 | 307 | ||
| 308 | + // duplicate lines are removed from history when `options.deDupeHistory` | ||
| 309 | + // is `true` | ||
| 310 | + fi = new FakeInput(); | ||
| 311 | + rli = new readline.Interface({ | ||
| 312 | + input: fi, | ||
| 313 | + output: fi, | ||
| 314 | + terminal: true, | ||
| 315 | + deDupeHistory: true | ||
| 316 | + }); | ||
| 317 | + expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat']; | ||
| 318 | + callCount = 0; | ||
| 319 | + rli.on('line', function(line) { | ||
| 320 | + assert.strictEqual(line, expectedLines[callCount]); | ||
| 321 | + callCount++; | ||
| 322 | + }); | ||
| 323 | + fi.emit('data', expectedLines.join('\n') + '\n'); | ||
| 324 | + assert.strictEqual(callCount, expectedLines.length); | ||
| 325 | + fi.emit('keypress', '.', { name: 'up' }); // 'bat' | ||
| 326 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 327 | + fi.emit('keypress', '.', { name: 'up' }); // 'bar' | ||
| 328 | + assert.notStrictEqual(rli.line, expectedLines[--callCount]); | ||
| 329 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 330 | + fi.emit('keypress', '.', { name: 'up' }); // 'baz' | ||
| 331 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 332 | + fi.emit('keypress', '.', { name: 'up' }); // 'foo' | ||
| 333 | + assert.notStrictEqual(rli.line, expectedLines[--callCount]); | ||
| 334 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 335 | + assert.strictEqual(callCount, 0); | ||
| 336 | + rli.close(); | ||
| 337 | + | ||
| 338 | + // duplicate lines are not removed from history when `options.deDupeHistory` | ||
| 339 | + // is `false` | ||
| 340 | + fi = new FakeInput(); | ||
| 341 | + rli = new readline.Interface({ | ||
| 342 | + input: fi, | ||
| 343 | + output: fi, | ||
| 344 | + terminal: true, | ||
| 345 | + deDupeHistory: false | ||
| 346 | + }); | ||
| 347 | + expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat']; | ||
| 348 | + callCount = 0; | ||
| 349 | + rli.on('line', function(line) { | ||
| 350 | + assert.strictEqual(line, expectedLines[callCount]); | ||
| 351 | + callCount++; | ||
| 352 | + }); | ||
| 353 | + fi.emit('data', expectedLines.join('\n') + '\n'); | ||
| 354 | + assert.strictEqual(callCount, expectedLines.length); | ||
| 355 | + fi.emit('keypress', '.', { name: 'up' }); // 'bat' | ||
| 356 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 357 | + fi.emit('keypress', '.', { name: 'up' }); // 'bar' | ||
| 358 | + assert.notStrictEqual(rli.line, expectedLines[--callCount]); | ||
| 359 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 360 | + fi.emit('keypress', '.', { name: 'up' }); // 'baz' | ||
| 361 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 362 | + fi.emit('keypress', '.', { name: 'up' }); // 'bar' | ||
| 363 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 364 | + fi.emit('keypress', '.', { name: 'up' }); // 'foo' | ||
| 365 | + assert.strictEqual(rli.line, expectedLines[--callCount]); | ||
| 366 | + assert.strictEqual(callCount, 0); | ||
| 367 | + rli.close(); | ||
| 368 | + | ||
| 308 | 369 | // sending a multi-byte utf8 char over multiple writes | |
| 309 | 370 | const buf = Buffer.from('☮', 'utf8'); | |
| 310 | 371 | fi = new FakeInput(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments