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
Next Next commit
readline: rename input field and remove excess params
Removed excess parameters from the createInterface method,
renamed input field to options, in accordance with the docs,
and made corresponding changes in tests.

Refs: #31603 (comment)
  • Loading branch information
rexagod committed Feb 27, 2020
commit da67338fad163d9488148eea9cd790be77130620
108 changes: 64 additions & 44 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ const ESCAPE_DECODER = Symbol('escape-decoder');
// GNU readline library - keyseq-timeout is 500ms (default)
const ESCAPE_CODE_TIMEOUT = 500;

function createInterface(input, output, completer, terminal) {
return new Interface(input, output, completer, terminal);
function createInterface(options) {
return new Interface(options);
}


function Interface(input, output, completer, terminal) {
function Interface(options) {
if (!(this instanceof Interface)) {
return new Interface(input, output, completer, terminal);
return new Interface(options);
}

this._sawReturnAt = 0;
Expand All @@ -111,37 +111,41 @@ function Interface(input, output, completer, terminal) {
this.tabSize = 8;

EventEmitter.call(this);
let input;
let output;
let completer;
let terminal;
let historySize;
let removeHistoryDuplicates = false;
let crlfDelay;
let prompt = '> ';

if (input && input.input) {
if (options && options.input) {
// An options object was given
output = input.output;
completer = input.completer;
terminal = input.terminal;
historySize = input.historySize;
if (input.tabSize !== undefined) {
validateUint32(input.tabSize, 'tabSize', true);
this.tabSize = input.tabSize;
output = options.output;
completer = options.completer;
terminal = options.terminal;
historySize = options.historySize;
if (options.tabSize !== undefined) {
validateUint32(options.tabSize, 'tabSize', true);
this.tabSize = options.tabSize;
}
removeHistoryDuplicates = input.removeHistoryDuplicates;
if (input.prompt !== undefined) {
prompt = input.prompt;
removeHistoryDuplicates = options.removeHistoryDuplicates;
if (options.prompt !== undefined) {
prompt = options.prompt;
}
if (input.escapeCodeTimeout !== undefined) {
if (NumberIsFinite(input.escapeCodeTimeout)) {
this.escapeCodeTimeout = input.escapeCodeTimeout;
if (options.escapeCodeTimeout !== undefined) {
if (NumberIsFinite(options.escapeCodeTimeout)) {
this.escapeCodeTimeout = options.escapeCodeTimeout;
} else {
throw new ERR_INVALID_OPT_VALUE(
'escapeCodeTimeout',
this.escapeCodeTimeout
);
}
}
crlfDelay = input.crlfDelay;
input = input.input;
crlfDelay = options.crlfDelay;
input = options.input;
}

if (completer !== undefined && typeof completer !== 'function') {
Expand Down Expand Up @@ -229,28 +233,37 @@ function Interface(input, output, completer, terminal) {

if (!this.terminal) {
function onSelfCloseWithoutTerminal() {
input.removeListener('data', ondata);
input.removeListener('end', onend);
if (input !== null && input !== undefined) {
input.removeListener('data', ondata);
input.removeListener('end', onend);
}
}

if (input !== null && input !== undefined) {
input.on('data', ondata);
input.on('end', onend);
}

input.on('data', ondata);
input.on('end', onend);
self.once('close', onSelfCloseWithoutTerminal);
this._decoder = new StringDecoder('utf8');
} else {
function onSelfCloseWithTerminal() {
input.removeListener('keypress', onkeypress);
input.removeListener('end', ontermend);
if (input !== null && input !== undefined) {
input.removeListener('keypress', onkeypress);
input.removeListener('end', ontermend);
}

if (output !== null && output !== undefined) {
output.removeListener('resize', onresize);
}
}

emitKeypressEvents(input, this);

// `input` usually refers to stdin
input.on('keypress', onkeypress);
input.on('end', ontermend);
if (input !== null && input !== undefined) {
emitKeypressEvents(input, this);
// `input` usually refers to stdin
input.on('keypress', onkeypress);
input.on('end', ontermend);
}

// Current line
this.line = '';
Expand All @@ -270,7 +283,9 @@ function Interface(input, output, completer, terminal) {
self.once('close', onSelfCloseWithTerminal);
}

input.resume();
if (input !== null && input !== undefined) {
input.resume();
}
}

ObjectSetPrototypeOf(Interface.prototype, EventEmitter.prototype);
Expand All @@ -292,9 +307,12 @@ Interface.prototype.setPrompt = function(prompt) {


Interface.prototype._setRawMode = function(mode) {
const wasInRawMode = this.input.isRaw;
let wasInRawMode;
if (this.input) {
wasInRawMode = this.input.isRaw;
}

if (typeof this.input.setRawMode === 'function') {
if (this.input && typeof this.input.setRawMode === 'function') {
this.input.setRawMode(mode);
}

Expand Down Expand Up @@ -427,22 +445,24 @@ Interface.prototype.close = function() {

Interface.prototype.pause = function() {
if (this.paused) return;
this.input.pause();
this.paused = true;
this.emit('pause');
return this;
if (this.input !== null && this.input !== undefined) {
this.input.pause();
this.paused = true;
this.emit('pause');
return this;
}
};


Interface.prototype.resume = function() {
if (!this.paused) return;
this.input.resume();
this.paused = false;
this.emit('resume');
return this;
if (this.input !== null && this.input !== undefined) {
this.input.resume();
this.paused = false;
this.emit('resume');
return this;
}
};


Interface.prototype.write = function(d, key) {
if (this.paused) this.resume();
if (this.terminal) {
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-tty-stdin-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ require('../common');
const assert = require('assert');
const readline = require('readline');

const rl = readline.createInterface(process.stdin, process.stdout);
const rl = readline.createInterface(
{ input: process.stdin, output: process.stdout }
);
rl.resume();

let hasPaused = false;
Expand Down