@ -7,6 +7,8 @@
'use strict' ;
'use strict' ;
const kHistorySize = 30 ;
const kHistorySize = 30 ;
const kMincrlfDelay = 100 ;
const kMaxcrlfDelay = 2000 ;
const util = require ( 'util' ) ;
const util = require ( 'util' ) ;
const debug = util . debuglog ( 'readline' ) ;
const debug = util . debuglog ( 'readline' ) ;
@ -39,13 +41,14 @@ function Interface(input, output, completer, terminal) {
return self ;
return self ;
}
}
this . _ sawReturn = false ;
this . _ sawReturnAt = 0 ;
this . isCompletionEnabled = true ;
this . isCompletionEnabled = true ;
this . _ sawKeyPress = false ;
this . _ sawKeyPress = false ;
this . _ previousKey = null ;
this . _ previousKey = null ;
EventEmitter . call ( this ) ;
EventEmitter . call ( this ) ;
var historySize ;
var historySize ;
let crlfDelay ;
let prompt = '> ' ;
let prompt = '> ' ;
if ( arguments . length === 1 ) {
if ( arguments . length === 1 ) {
@ -57,6 +60,7 @@ function Interface(input, output, completer, terminal) {
if ( input . prompt !== undefined ) {
if ( input . prompt !== undefined ) {
prompt = input . prompt ;
prompt = input . prompt ;
}
}
crlfDelay = input . crlfDelay ;
input = input . input ;
input = input . input ;
}
}
@ -85,6 +89,8 @@ function Interface(input, output, completer, terminal) {
this . output = output ;
this . output = output ;
this . input = input ;
this . input = input ;
this . historySize = historySize ;
this . historySize = historySize ;
this . crlfDelay = Math . max ( kMincrlfDelay ,
Math . min ( kMaxcrlfDelay , crlfDelay >>> 0 ) ) ;
// Check arity, 2 - for async, 1 for sync
// Check arity, 2 - for async, 1 for sync
if ( typeof completer === 'function' ) {
if ( typeof completer === 'function' ) {
@ -345,9 +351,10 @@ Interface.prototype._normalWrite = function(b) {
return ;
return ;
}
}
var string = this . _ decoder . write ( b ) ;
var string = this . _ decoder . write ( b ) ;
if ( this . _ sawReturn ) {
if ( this . _ sawReturnAt &&
Date . now ( ) - this . _ sawReturnAt <= this . crlfDelay ) {
string = string . replace ( /^\n/ , '' ) ;
string = string . replace ( /^\n/ , '' ) ;
this . _ sawReturn = false ;
this . _ sawReturnAt = 0 ;
}
}
// Run test() on the new string chunk, not on the entire line buffer.
// Run test() on the new string chunk, not on the entire line buffer.
@ -358,7 +365,7 @@ Interface.prototype._normalWrite = function(b) {
this . _ line_buffer = null ;
this . _ line_buffer = null ;
}
}
if ( newPartContainsEnding ) {
if ( newPartContainsEnding ) {
this . _ sawReturn = string . endsWith ( '\r' ) ;
this . _ sawReturnAt = string . endsWith ( '\r' ) ? Date . now ( ) : 0 ;
// got one or more newlines; process into "line" events
// got one or more newlines; process into "line" events
var lines = string . split ( lineEnding ) ;
var lines = string . split ( lineEnding ) ;
@ -846,20 +853,22 @@ Interface.prototype._ttyWrite = function(s, key) {
/* No modifier keys used */
/* No modifier keys used */
// \r bookkeeping is only relevant if a \n comes right after.
// \r bookkeeping is only relevant if a \n comes right after.
if ( this . _ sawReturn && key . name !== 'enter' )
if ( this . _ sawReturnAt && key . name !== 'enter' )
this . _ sawReturn = false ;
this . _ sawReturnAt = 0 ;
switch ( key . name ) {
switch ( key . name ) {
case 'return' : // carriage return, i.e. \r
case 'return' : // carriage return, i.e. \r
this . _ sawReturn = true ;
this . _ sawReturnAt = Date . now ( ) ;
this . _ line ( ) ;
this . _ line ( ) ;
break ;
break ;
case 'enter' :
case 'enter' :
if ( this . _ sawReturn )
// When key interval > crlfDelay
this . _ sawReturn = false ;
if ( this . _ sawReturnAt === 0 ||
els e
Date . now ( ) - this . _ sawR eturnAt > this . crlfD elay ) {
this . _ line ( ) ;
this . _ line ( ) ;
}
this . _ sawReturnAt = 0 ;
break ;
break ;
case 'backspace' :
case 'backspace' :