Skip to content

Commit fb1ce38

Browse files
Raoofrefack
authored andcommitted
lib: rewrite the same code again :)
1 parent b0b8ba5 commit fb1ce38

File tree

3 files changed

+32
-56
lines changed

3 files changed

+32
-56
lines changed

doc/api/readline.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,9 @@ changes:
376376
to the history list duplicates an older one, this removes the older line
377377
from the list. **Default:** `false`.
378378
* `escapeCodeTimeout` {number} The duration `readline` will wait for a
379-
character when reading an ambiguous key sequence in milliseconds (one that
379+
character (when reading an ambiguous key sequence in milliseconds one that
380380
can both form a complete key sequence using the input read so far and can
381-
take additional input to complete a longer key sequence). The default
382-
value is used if `NaN`, a negative number, or a non-number is provided.
383-
**Default:** `500`.
381+
take additional input to complete a longer key sequence).
384382

385383
The `readline.createInterface()` method creates a new `readline.Interface`
386384
instance.

lib/readline.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ function Interface(input, output, completer, terminal) {
8282
this.isCompletionEnabled = true;
8383
this._sawKeyPress = false;
8484
this._previousKey = null;
85+
this.escapeCodeTimeout = ESCAPE_CODE_TIMEOUT;
8586

8687
EventEmitter.call(this);
8788
var historySize;
8889
var removeHistoryDuplicates = false;
8990
let crlfDelay;
9091
let prompt = '> ';
91-
let escapeCodeTimeout = ESCAPE_CODE_TIMEOUT;
9292

9393
if (input && input.input) {
9494
// an options object was given
@@ -100,15 +100,19 @@ function Interface(input, output, completer, terminal) {
100100
if (input.prompt !== undefined) {
101101
prompt = input.prompt;
102102
}
103-
if (typeof input.escapeCodeTimeout === 'number' &&
104-
!Number.isNaN(input.escapeCodeTimeout) &&
105-
input.escapeCodeTimeout > 0) {
106-
escapeCodeTimeout = input.escapeCodeTimeout;
103+
if (input.escapeCodeTimeout !== undefined) {
104+
this.escapeCodeTimeout = input.escapeCodeTimeout;
107105
}
108106
crlfDelay = input.crlfDelay;
109107
input = input.input;
110108
}
111109

110+
if (!Number.isFinite(this.escapeCodeTimeout)) {
111+
throw new ERR_INVALID_OPT_VALUE(
112+
'escapeCodeTimeout',
113+
this.escapeCodeTimeout
114+
);
115+
}
112116

113117
if (completer && typeof completer !== 'function') {
114118
throw new ERR_INVALID_OPT_VALUE('completer', completer);
@@ -138,7 +142,6 @@ function Interface(input, output, completer, terminal) {
138142
this.removeHistoryDuplicates = !!removeHistoryDuplicates;
139143
this.crlfDelay = crlfDelay ?
140144
Math.max(kMincrlfDelay, crlfDelay) : kMincrlfDelay;
141-
this.escapeCodeTimeout = escapeCodeTimeout;
142145
// Check arity, 2 - for async, 1 for sync
143146
if (typeof completer === 'function') {
144147
this.completer = completer.length === 2 ?
@@ -1005,7 +1008,7 @@ function emitKeypressEvents(stream, iface) {
10051008
stream[ESCAPE_DECODER] = emitKeys(stream);
10061009
stream[ESCAPE_DECODER].next();
10071010

1008-
const escapeCodeTimeoutFn = () => stream[ESCAPE_DECODER].next('');
1011+
const escapeCodeTimeout = () => stream[ESCAPE_DECODER].next('');
10091012
let timeoutId;
10101013

10111014
function onData(b) {
@@ -1028,7 +1031,7 @@ function emitKeypressEvents(stream, iface) {
10281031
// Escape letter at the tail position
10291032
if (r[i] === kEscape && i + 1 === r.length) {
10301033
timeoutId = setTimeout(
1031-
escapeCodeTimeoutFn,
1034+
escapeCodeTimeout,
10321035
iface ? iface.escapeCodeTimeout : ESCAPE_CODE_TIMEOUT
10331036
);
10341037
}
Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33

44
// This test ensures that the escapeCodeTimeout option set correctly
55

66
const assert = require('assert');
77
const readline = require('readline');
88
const EventEmitter = require('events').EventEmitter;
9-
const ESCAPE_CODE_TIMEOUT = 500;
109

1110
class FakeInput extends EventEmitter {
1211
resume() {}
@@ -26,46 +25,22 @@ class FakeInput extends EventEmitter {
2625
rli.close();
2726
}
2827

29-
{
30-
const fi = new FakeInput();
31-
const rli = new readline.Interface({
32-
input: fi,
33-
output: fi,
34-
escapeCodeTimeout: null
35-
});
36-
assert.strictEqual(rli.escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
37-
rli.close();
38-
}
39-
40-
{
41-
const fi = new FakeInput();
42-
const rli = new readline.Interface({
43-
input: fi,
44-
output: fi,
45-
escapeCodeTimeout: {}
46-
});
47-
assert.strictEqual(rli.escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
48-
rli.close();
49-
}
50-
51-
{
52-
const fi = new FakeInput();
53-
const rli = new readline.Interface({
54-
input: fi,
55-
output: fi,
56-
escapeCodeTimeout: NaN
28+
[
29+
null,
30+
{},
31+
NaN,
32+
'50'
33+
].forEach((invalidInput) => {
34+
common.expectsError(() => {
35+
const fi = new FakeInput();
36+
const rli = new readline.Interface({
37+
input: fi,
38+
output: fi,
39+
escapeCodeTimeout: invalidInput
40+
});
41+
rli.close();
42+
}, {
43+
type: TypeError,
44+
code: 'ERR_INVALID_OPT_VALUE'
5745
});
58-
assert.strictEqual(rli.escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
59-
rli.close();
60-
}
61-
62-
{
63-
const fi = new FakeInput();
64-
const rli = new readline.Interface({
65-
input: fi,
66-
output: fi,
67-
escapeCodeTimeout: '50'
68-
});
69-
assert.strictEqual(rli.escapeCodeTimeout, ESCAPE_CODE_TIMEOUT);
70-
rli.close();
71-
}
46+
});

0 commit comments

Comments
 (0)