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
Coerce to Min/max range
  • Loading branch information
princejwesley committed Aug 21, 2016
commit 515627c703c225d24550cf2489c34e1da539efdf
5 changes: 3 additions & 2 deletions doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,9 @@ added: v0.1.98
check, otherwise the history caching mechanism is not initialized at all.
* `prompt` - the prompt string to use. Default: `'> '`
* `crlfDelay` {number} If the delay between `\r` and `\n` exceeds
`crlfDelay`, both `\r` and `\n` will be treated as separate end-of-line
input. Default to `100` milliseconds.
`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.

The `readline.createInterface()` method creates a new `readline.Interface`
instance.
Expand Down
25 changes: 9 additions & 16 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'use strict';

const kHistorySize = 30;
const kcrlfDelayInMillis = 100;
const kMincrlfDelay = 100;
const kMaxcrlfDelay = 2000;

const util = require('util');
const debug = util.debuglog('readline');
Expand All @@ -21,20 +22,18 @@ const isFullWidthCodePoint = internalReadline.isFullWidthCodePoint;
const stripVTControlCharacters = internalReadline.stripVTControlCharacters;


exports.createInterface = function(input, output, completer, terminal,
crlfDelay = kcrlfDelayInMillis) {
exports.createInterface = function(input, output, completer, terminal) {
var rl;
if (arguments.length === 1) {
rl = new Interface(input);
} else {
rl = new Interface(input, output, completer, terminal, crlfDelay);
rl = new Interface(input, output, completer, terminal);
}
return rl;
};


function Interface(input, output, completer, terminal,
crlfDelay = kcrlfDelayInMillis) {
function Interface(input, output, completer, terminal) {
if (!(this instanceof Interface)) {
// call the constructor preserving original number of arguments
const self = Object.create(Interface.prototype);
Expand All @@ -48,6 +47,7 @@ function Interface(input, output, completer, terminal,

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

if (arguments.length === 1) {
Expand All @@ -59,9 +59,7 @@ function Interface(input, output, completer, terminal,
if (input.prompt !== undefined) {
prompt = input.prompt;
}
if (input.crlfDelay !== undefined) {
crlfDelay = input.crlfDelay;
}
crlfDelay = input.crlfDelay;
input = input.input;
}

Expand All @@ -79,12 +77,6 @@ function Interface(input, output, completer, terminal,
throw new TypeError('Argument "historySize" must be a positive number');
}

if (typeof crlfDelay !== 'number' ||
isNaN(crlfDelay) ||
crlfDelay <= 0) {
throw new TypeError('Argument "crlfDelay" must be a positive number > 0');
}

// backwards compat; check the isTTY prop of the output stream
// when `terminal` was not specified
if (terminal === undefined && !(output === null || output === undefined)) {
Expand All @@ -96,7 +88,8 @@ function Interface(input, output, completer, terminal,
this.output = output;
this.input = input;
this.historySize = historySize;
this.crlfDelay = crlfDelay;
this.crlfDelay = Math.max(kMincrlfDelay,
Math.min(kMaxcrlfDelay, crlfDelay >>> 0));

// Check arity, 2 - for async, 1 for sync
if (typeof completer === 'function') {
Expand Down
23 changes: 18 additions & 5 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ function isWarned(emitter) {
return false;
}

{
// Default crlfDelay is 100ms
const fi = new FakeInput();
let rli = new readline.Interface({ input: fi, output: fi });
assert.strictEqual(rli.crlfDelay, 100);
rli.close();

// Minimum crlfDelay is 100ms
rli = new readline.Interface({ input: fi, output: fi, crlfDelay: 0});
assert.strictEqual(rli.crlfDelay, 100);
rli.close();

// Maximum crlfDelay is 2000ms
rli = new readline.Interface({ input: fi, output: fi, crlfDelay: 1 << 30});
assert.strictEqual(rli.crlfDelay, 2000);
rli.close();
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To 3 separate scopes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@yorkie done


[ true, false ].forEach(function(terminal) {
var fi;
var rli;
Expand All @@ -46,11 +64,6 @@ function isWarned(emitter) {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal});
assert.strictEqual(rli.historySize, 30);

// default crlfDelay is 100ms
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal});
assert.strictEqual(rli.crlfDelay, 100);

fi.emit('data', 'asdf\n');
assert.deepStrictEqual(rli.history, terminal ? ['asdf'] : undefined);
rli.close();
Expand Down