@ -95,18 +95,13 @@ function Interface(input, output, completer) {
this . history = [ ] ;
this . history = [ ] ;
this . historyIndex = - 1 ;
this . historyIndex = - 1 ;
// a number of lines used by current command
this . usedLines = 1 ;
var winSize = output . getWindowSize ( ) ;
var winSize = output . getWindowSize ( ) ;
exports . columns = winSize [ 0 ] ;
exports . columns = winSize [ 0 ] ;
exports . rows = winSize [ 1 ] ;
if ( process . listeners ( 'SIGWINCH' ) . length === 0 ) {
if ( process . listeners ( 'SIGWINCH' ) . length === 0 ) {
process . on ( 'SIGWINCH' , function ( ) {
process . on ( 'SIGWINCH' , function ( ) {
var winSize = output . getWindowSize ( ) ;
var winSize = output . getWindowSize ( ) ;
exports . columns = winSize [ 0 ] ;
exports . columns = winSize [ 0 ] ;
exports . rows = winSize [ 1 ] ;
} ) ;
} ) ;
}
}
}
}
@ -118,10 +113,6 @@ Interface.prototype.__defineGetter__('columns', function() {
return exports . columns ;
return exports . columns ;
} ) ;
} ) ;
Interface . prototype . __ defineGetter__ ( 'rows' , function ( ) {
return exports . rows ;
} ) ;
Interface . prototype . setPrompt = function ( prompt , length ) {
Interface . prototype . setPrompt = function ( prompt , length ) {
this . _ prompt = prompt ;
this . _ prompt = prompt ;
if ( length ) {
if ( length ) {
@ -187,53 +178,19 @@ Interface.prototype._addHistory = function() {
} ;
} ;
Interface . prototype . _ recalcUsedLines = function ( ) {
var line = this . _ prompt + this . line ;
var newcount = Math . ceil ( line . length / this . columns ) ;
if ( newcount > this . usedLines ) this . usedLines = newcount ;
} ;
Interface . prototype . _ refreshLine = function ( ) {
Interface . prototype . _ refreshLine = function ( ) {
var columns = this . columns ;
if ( ! columns ) columns = Infinity ;
// See if a number of used lines has changed
var oldLines = this . usedLines ;
this . _ recalcUsedLines ( ) ;
if ( oldLines != this . usedLines ) {
this . output . cursorTo ( 0 , this . rows - 1 ) ;
for ( var i = oldLines ; i < this . usedLines ; i ++ ) {
this . output . write ( '\r\n' ) ;
}
}
// Cursor to left edge.
// Cursor to left edge.
if ( this . usedLines === 1 ) {
this . output . cursorTo ( 0 ) ;
this . output . cursorTo ( 0 ) ;
} else {
this . output . cursorTo ( 0 , this . rows - this . usedLines ) ;
}
// Write the prompt and the current buffer content.
// Write the prompt and the current buffer content.
var buffer = this . _ prompt + this . line ;
this . output . write ( this . _ prompt ) ;
this . output . write ( buffer ) ;
this . output . write ( this . line ) ;
// Erase to right.
// Erase to right.
this . output . clearLine ( 1 ) ;
this . output . clearLine ( 1 ) ;
var clearLinesCnt = this . usedLines - Math . floor ( buffer . length / columns ) - 1 ;
for ( var i = this . rows - clearLinesCnt ; i < this . rows ; i ++ ) {
this . output . cursorTo ( 0 , i ) ;
this . output . clearLine ( 0 ) ;
}
// Move cursor to original position.
// Move cursor to original position.
var curPos = this . _ getCursorPos ( ) ;
this . output . cursorTo ( this . _ promptLength + this . cursor ) ;
if ( this . usedLines === 1 ) {
this . output . cursorTo ( curPos [ 0 ] ) ;
} else {
this . output . cursorTo ( curPos [ 0 ] , this . rows - this . usedLines + curPos [ 1 ] ) ;
}
} ;
} ;
@ -285,7 +242,6 @@ Interface.prototype._insertString = function(c) {
this . line += c ;
this . line += c ;
this . cursor += c . length ;
this . cursor += c . length ;
this . output . write ( c ) ;
this . output . write ( c ) ;
this . _ recalcUsedLines ( ) ;
}
}
} ;
} ;
@ -459,7 +415,6 @@ Interface.prototype._deleteLineRight = function() {
Interface . prototype . _ line = function ( ) {
Interface . prototype . _ line = function ( ) {
var line = this . _ addHistory ( ) ;
var line = this . _ addHistory ( ) ;
this . output . write ( '\r\n' ) ;
this . output . write ( '\r\n' ) ;
this . usedLines = 1 ;
this . _ onLine ( line ) ;
this . _ onLine ( line ) ;
} ;
} ;
@ -491,33 +446,6 @@ Interface.prototype._historyPrev = function() {
} ;
} ;
// Returns current cursor's position and line
Interface . prototype . _ getCursorPos = function ( ) {
var columns = this . columns ;
var pos = this . cursor + this . _ promptLength ;
if ( ! columns ) return [ pos , 0 ] ;
var lineNum = Math . floor ( pos / columns ) ;
var colNum = pos - lineNum * columns ;
return [ colNum , lineNum ] ;
} ;
Interface . prototype . _ moveCursor = function ( dx ) {
var oldcursor = this . cursor ;
var oldLine = this . _ getCursorPos ( ) [ 1 ] ;
this . cursor += dx ;
var newLine = this . _ getCursorPos ( ) [ 1 ] ;
// check if cursors are in the same line
if ( oldLine == newLine ) {
this . output . moveCursor ( dx , 0 ) ;
} else {
this . _ refreshLine ( ) ;
}
} ;
// handle a write from the tty
// handle a write from the tty
Interface . prototype . _ ttyWrite = function ( s , key ) {
Interface . prototype . _ ttyWrite = function ( s , key ) {
var next_word , next_non_word , previous_word , previous_non_word ;
var next_word , next_non_word , previous_word , previous_non_word ;
@ -691,13 +619,15 @@ Interface.prototype._ttyWrite = function(s, key) {
case 'left' :
case 'left' :
if ( this . cursor > 0 ) {
if ( this . cursor > 0 ) {
this . _ moveCursor ( - 1 ) ;
this . cursor -- ;
this . output . moveCursor ( - 1 , 0 ) ;
}
}
break ;
break ;
case 'right' :
case 'right' :
if ( this . cursor != this . line . length ) {
if ( this . cursor != this . line . length ) {
this . _ moveCursor ( 1 ) ;
this . cursor ++ ;
this . output . moveCursor ( 1 , 0 ) ;
}
}
break ;
break ;