@@ -557,6 +557,7 @@ Interface.prototype._getDisplayPos = function(str) {
557
557
var offset = 0 ;
558
558
var col = this . columns ;
559
559
var code ;
560
+ str = stripVTControlCharacters ( str ) ;
560
561
for ( var i = 0 , len = str . length ; i < len ; i ++ ) {
561
562
code = codePointAt ( str , i ) ;
562
563
if ( code >= 0x10000 ) { // surrogates
@@ -581,7 +582,7 @@ Interface.prototype._getDisplayPos = function(str) {
581
582
Interface . prototype . _getCursorPos = function ( ) {
582
583
var columns = this . columns ;
583
584
var strBeforeCursor = this . _prompt + this . line . substring ( 0 , this . cursor ) ;
584
- var dispPos = this . _getDisplayPos ( strBeforeCursor ) ;
585
+ var dispPos = this . _getDisplayPos ( stripVTControlCharacters ( strBeforeCursor ) ) ;
585
586
var cols = dispPos . cols ;
586
587
var rows = dispPos . rows ;
587
588
// If the cursor is on a full-width character which steps over the line,
@@ -921,9 +922,11 @@ exports.emitKeypressEvents = emitKeypressEvents;
921
922
*/
922
923
923
924
// Regexes used for ansi escape code splitting
924
- var metaKeyCodeRe = / ^ (?: \x1b ) ( [ a - z A - Z 0 - 9 ] ) $ / ;
925
- var functionKeyCodeRe =
926
- / ^ (?: \x1b + ) ( O | N | \[ | \[ \[ ) (?: ( \d + ) (?: ; ( \d + ) ) ? ( [ ~ ^ $ ] ) | (?: 1 ; ) ? ( \d + ) ? ( [ a - z A - Z ] ) ) / ;
925
+ var metaKeyCodeReAnywhere = / (?: \x1b ) ( [ a - z A - Z 0 - 9 ] ) / ;
926
+ var metaKeyCodeRe = new RegExp ( '^' + metaKeyCodeReAnywhere . source + '$' ) ;
927
+ var functionKeyCodeReAnywhere =
928
+ / (?: \x1b + ) ( O | N | \[ | \[ \[ ) (?: ( \d + ) (?: ; ( \d + ) ) ? ( [ ~ ^ $ ] ) | (?: 1 ; ) ? ( \d + ) ? ( [ a - z A - Z ] ) ) / ;
929
+ var functionKeyCodeRe = new RegExp ( '^' + functionKeyCodeReAnywhere . source ) ;
927
930
928
931
function emitKey ( stream , s ) {
929
932
var ch ,
@@ -1207,6 +1210,7 @@ exports.clearScreenDown = clearScreenDown;
1207
1210
1208
1211
function getStringWidth ( str ) {
1209
1212
var width = 0 ;
1213
+ str = stripVTControlCharacters ( str ) ;
1210
1214
for ( var i = 0 , len = str . length ; i < len ; i ++ ) {
1211
1215
var code = codePointAt ( str , i ) ;
1212
1216
if ( code >= 0x10000 ) { // surrogates
@@ -1289,3 +1293,14 @@ function codePointAt(str, index) {
1289
1293
return code ;
1290
1294
}
1291
1295
exports . codePointAt = codePointAt ;
1296
+
1297
+
1298
+ /**
1299
+ * Tries to remove all VT control characters. Use to estimate displayed
1300
+ * string width. May be buggy due to not running a real state machine
1301
+ */
1302
+ function stripVTControlCharacters ( str ) {
1303
+ str = str . replace ( new RegExp ( functionKeyCodeReAnywhere . source , 'g' ) , '' ) ;
1304
+ return str . replace ( new RegExp ( metaKeyCodeReAnywhere . source , 'g' ) , '' ) ;
1305
+ }
1306
+ exports . stripVTControlCharacters = stripVTControlCharacters ;
0 commit comments