Skip to content

Commit

Permalink
readline: name some anonymous functions
Browse files Browse the repository at this point in the history
PR-URL: #14297
Refs: #8913
Refs: #14297 (review)
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Amunu authored and jasnell committed Sep 25, 2017
1 parent e014983 commit 8eeaba6
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ function Interface(input, output, completer, terminal) {

// Check arity, 2 - for async, 1 for sync
if (typeof completer === 'function') {
this.completer = completer.length === 2 ? completer : function(v, cb) {
cb(null, completer(v));
};
this.completer = completer.length === 2 ?
completer :
function completerWrapper(v, cb) {
cb(null, completer(v));
};
}

this.setPrompt(prompt);
Expand Down Expand Up @@ -171,15 +173,23 @@ function Interface(input, output, completer, terminal) {
}

if (!this.terminal) {
input.on('data', ondata);
input.on('end', onend);
self.once('close', function() {
function onSelfCloseWithoutTerminal() {
input.removeListener('data', ondata);
input.removeListener('end', onend);
});
this._decoder = new StringDecoder('utf8');
}

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 (output !== null && output !== undefined) {
output.removeListener('resize', onresize);
}
}

emitKeypressEvents(input, this);

Expand All @@ -202,13 +212,7 @@ function Interface(input, output, completer, terminal) {
if (output !== null && output !== undefined)
output.on('resize', onresize);

self.once('close', function() {
input.removeListener('keypress', onkeypress);
input.removeListener('end', ontermend);
if (output !== null && output !== undefined) {
output.removeListener('resize', onresize);
}
});
self.once('close', onSelfCloseWithTerminal);
}

input.resume();
Expand Down Expand Up @@ -450,7 +454,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
var self = this;

self.pause();
self.completer(self.line.slice(0, self.cursor), function(err, rv) {
self.completer(self.line.slice(0, self.cursor), function onComplete(err, rv) {
self.resume();

if (err) {
Expand All @@ -464,7 +468,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
// Apply/show completions.
if (lastKeypressWasTab) {
self._writeToOutput('\r\n');
var width = completions.reduce(function(a, b) {
var width = completions.reduce(function completionReducer(a, b) {
return a.length > b.length ? a : b;
}).length + 2; // 2 space padding
var maxColumns = Math.floor(self.columns / width);
Expand All @@ -485,7 +489,9 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
}

// If there is a common prefix to all matches, then apply that portion.
var f = completions.filter(function(e) { if (e) return e; });
var f = completions.filter(function completionFilter(e) {
if (e) return e;
});
var prefix = commonPrefix(f);
if (prefix.length > completeOn.length) {
self._insertString(prefix.slice(completeOn.length));
Expand Down

0 comments on commit 8eeaba6

Please sign in to comment.