@@ -126,9 +126,11 @@ function Interface(input, output, completer, terminal) {
126126
127127 // Check arity, 2 - for async, 1 for sync
128128 if ( typeof completer === 'function' ) {
129- this . completer = completer . length === 2 ? completer : function ( v , cb ) {
130- cb ( null , completer ( v ) ) ;
131- } ;
129+ this . completer = completer . length === 2 ?
130+ completer :
131+ function completerWrapper ( v , cb ) {
132+ cb ( null , completer ( v ) ) ;
133+ } ;
132134 }
133135
134136 this . setPrompt ( prompt ) ;
@@ -171,15 +173,23 @@ function Interface(input, output, completer, terminal) {
171173 }
172174
173175 if ( ! this . terminal ) {
174- input . on ( 'data' , ondata ) ;
175- input . on ( 'end' , onend ) ;
176- self . once ( 'close' , function ( ) {
176+ function onSelfCloseWithoutTerminal ( ) {
177177 input . removeListener ( 'data' , ondata ) ;
178178 input . removeListener ( 'end' , onend ) ;
179- } ) ;
180- this . _decoder = new StringDecoder ( 'utf8' ) ;
179+ }
181180
181+ input . on ( 'data' , ondata ) ;
182+ input . on ( 'end' , onend ) ;
183+ self . once ( 'close' , onSelfCloseWithoutTerminal ) ;
184+ this . _decoder = new StringDecoder ( 'utf8' ) ;
182185 } else {
186+ function onSelfCloseWithTerminal ( ) {
187+ input . removeListener ( 'keypress' , onkeypress ) ;
188+ input . removeListener ( 'end' , ontermend ) ;
189+ if ( output !== null && output !== undefined ) {
190+ output . removeListener ( 'resize' , onresize ) ;
191+ }
192+ }
183193
184194 emitKeypressEvents ( input , this ) ;
185195
@@ -202,13 +212,7 @@ function Interface(input, output, completer, terminal) {
202212 if ( output !== null && output !== undefined )
203213 output . on ( 'resize' , onresize ) ;
204214
205- self . once ( 'close' , function ( ) {
206- input . removeListener ( 'keypress' , onkeypress ) ;
207- input . removeListener ( 'end' , ontermend ) ;
208- if ( output !== null && output !== undefined ) {
209- output . removeListener ( 'resize' , onresize ) ;
210- }
211- } ) ;
215+ self . once ( 'close' , onSelfCloseWithTerminal ) ;
212216 }
213217
214218 input . resume ( ) ;
@@ -450,7 +454,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
450454 var self = this ;
451455
452456 self . pause ( ) ;
453- self . completer ( self . line . slice ( 0 , self . cursor ) , function ( err , rv ) {
457+ self . completer ( self . line . slice ( 0 , self . cursor ) , function onComplete ( err , rv ) {
454458 self . resume ( ) ;
455459
456460 if ( err ) {
@@ -464,7 +468,7 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
464468 // Apply/show completions.
465469 if ( lastKeypressWasTab ) {
466470 self . _writeToOutput ( '\r\n' ) ;
467- var width = completions . reduce ( function ( a , b ) {
471+ var width = completions . reduce ( function completionReducer ( a , b ) {
468472 return a . length > b . length ? a : b ;
469473 } ) . length + 2 ; // 2 space padding
470474 var maxColumns = Math . floor ( self . columns / width ) ;
@@ -485,7 +489,9 @@ Interface.prototype._tabComplete = function(lastKeypressWasTab) {
485489 }
486490
487491 // If there is a common prefix to all matches, then apply that portion.
488- var f = completions . filter ( function ( e ) { if ( e ) return e ; } ) ;
492+ var f = completions . filter ( function completionFilter ( e ) {
493+ if ( e ) return e ;
494+ } ) ;
489495 var prefix = commonPrefix ( f ) ;
490496 if ( prefix . length > completeOn . length ) {
491497 self . _insertString ( prefix . slice ( completeOn . length ) ) ;
0 commit comments