Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
lib: rewrite the same code again :)
  • Loading branch information
Raoof authored and refack committed Oct 16, 2018
commit fb1ce389e1945b864e11c3baead54274f35c67e9
6 changes: 2 additions & 4 deletions doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,9 @@ changes:
to the history list duplicates an older one, this removes the older line
from the list. **Default:** `false`.
* `escapeCodeTimeout` {number} The duration `readline` will wait for a
character when reading an ambiguous key sequence in milliseconds (one that
character (when reading an ambiguous key sequence in milliseconds one that
can both form a complete key sequence using the input read so far and can
take additional input to complete a longer key sequence). The default
value is used if `NaN`, a negative number, or a non-number is provided.
**Default:** `500`.
take additional input to complete a longer key sequence).

The `readline.createInterface()` method creates a new `readline.Interface`
instance.
Expand Down
19 changes: 11 additions & 8 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ function Interface(input, output, completer, terminal) {
this.isCompletionEnabled = true;
this._sawKeyPress = false;
this._previousKey = null;
this.escapeCodeTimeout = ESCAPE_CODE_TIMEOUT;

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

if (input && input.input) {
// an options object was given
Expand All @@ -100,15 +100,19 @@ function Interface(input, output, completer, terminal) {
if (input.prompt !== undefined) {
prompt = input.prompt;
}
if (typeof input.escapeCodeTimeout === 'number' &&
!Number.isNaN(input.escapeCodeTimeout) &&
input.escapeCodeTimeout > 0) {
escapeCodeTimeout = input.escapeCodeTimeout;
if (input.escapeCodeTimeout !== undefined) {
this.escapeCodeTimeout = input.escapeCodeTimeout;
}
crlfDelay = input.crlfDelay;
input = input.input;
}

if (!Number.isFinite(this.escapeCodeTimeout)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should be moved into the added upper if statement, since it is only necessary to test to possible provided option. If non was passed through, we already know that the value is finite.

throw new ERR_INVALID_OPT_VALUE(
'escapeCodeTimeout',
this.escapeCodeTimeout
);
}

if (completer && typeof completer !== 'function') {
throw new ERR_INVALID_OPT_VALUE('completer', completer);
Expand Down Expand Up @@ -138,7 +142,6 @@ function Interface(input, output, completer, terminal) {
this.removeHistoryDuplicates = !!removeHistoryDuplicates;
this.crlfDelay = crlfDelay ?
Math.max(kMincrlfDelay, crlfDelay) : kMincrlfDelay;
this.escapeCodeTimeout = escapeCodeTimeout;
// Check arity, 2 - for async, 1 for sync
if (typeof completer === 'function') {
this.completer = completer.length === 2 ?
Expand Down Expand Up @@ -1005,7 +1008,7 @@ function emitKeypressEvents(stream, iface) {
stream[ESCAPE_DECODER] = emitKeys(stream);
stream[ESCAPE_DECODER].next();

const escapeCodeTimeoutFn = () => stream[ESCAPE_DECODER].next('');
const escapeCodeTimeout = () => stream[ESCAPE_DECODER].next('');
let timeoutId;

function onData(b) {
Expand All @@ -1028,7 +1031,7 @@ function emitKeypressEvents(stream, iface) {
// Escape letter at the tail position
if (r[i] === kEscape && i + 1 === r.length) {
timeoutId = setTimeout(
escapeCodeTimeoutFn,
escapeCodeTimeout,
iface ? iface.escapeCodeTimeout : ESCAPE_CODE_TIMEOUT
);
}
Expand Down
63 changes: 19 additions & 44 deletions test/parallel/test-readline-interface-escapecodetimeout.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
require('../common');
const common = require('../common');

// This test ensures that the escapeCodeTimeout option set correctly

const assert = require('assert');
const readline = require('readline');
const EventEmitter = require('events').EventEmitter;
const ESCAPE_CODE_TIMEOUT = 500;

class FakeInput extends EventEmitter {
resume() {}
Expand All @@ -26,46 +25,22 @@ class FakeInput extends EventEmitter {
rli.close();
}

{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
escapeCodeTimeout: null
});
assert.strictEqual(rli.escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
rli.close();
}

{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
escapeCodeTimeout: {}
});
assert.strictEqual(rli.escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
rli.close();
}

{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
escapeCodeTimeout: NaN
[
null,
{},
NaN,
'50'
].forEach((invalidInput) => {
common.expectsError(() => {
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
escapeCodeTimeout: invalidInput
});
rli.close();
}, {
type: TypeError,
code: 'ERR_INVALID_OPT_VALUE'
});
assert.strictEqual(rli.escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
rli.close();
}

{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
escapeCodeTimeout: '50'
});
assert.strictEqual(rli.escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
rli.close();
}
});