From: Yazhong Liu Date: Sun, 9 Mar 2014 06:46:54 +0000 (+0800) Subject: readline: consider newlines for cursor position X-Git-Tag: v0.11.13~79 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=93c3674ff7115fb2a3dbb5b4ffd22f4d5ed9a472;p=platform%2Fupstream%2Fnodejs.git readline: consider newlines for cursor position Fixes #7266. Closes #7279. --- diff --git a/lib/readline.js b/lib/readline.js index c5d04b9..1fa217c 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -563,6 +563,7 @@ Interface.prototype._historyPrev = function() { Interface.prototype._getDisplayPos = function(str) { var offset = 0; var col = this.columns; + var row = 0; var code; str = stripVTControlCharacters(str); for (var i = 0, len = str.length; i < len; i++) { @@ -570,6 +571,11 @@ Interface.prototype._getDisplayPos = function(str) { if (code >= 0x10000) { // surrogates i++; } + if (code === 0x0a) { // new line \n + offset = 0; + row += 1; + continue; + } if (isFullWidthCodePoint(code)) { if ((offset + 1) % col === 0) { offset++; @@ -580,7 +586,7 @@ Interface.prototype._getDisplayPos = function(str) { } } var cols = offset % col; - var rows = (offset - cols) / col; + var rows = row + (offset - cols) / col; return {cols: cols, rows: rows}; }; diff --git a/test/simple/test-readline-interface.js b/test/simple/test-readline-interface.js index d88f64a..930cf22 100644 --- a/test/simple/test-readline-interface.js +++ b/test/simple/test-readline-interface.js @@ -206,6 +206,32 @@ FakeInput.prototype.end = function() {}; assert.equal(callCount, 1); rli.close(); + if (terminal) { + // question + fi = new FakeInput(); + rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); + expectedLines = ['foo']; + rli.question(expectedLines[0], function() { + rli.close(); + }); + var cursorPos = rli._getCursorPos(); + assert.equal(cursorPos.rows, 0); + assert.equal(cursorPos.cols, expectedLines[0].length); + rli.close(); + + // sending a multi-line question + fi = new FakeInput(); + rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); + expectedLines = ['foo', 'bar']; + rli.question(expectedLines.join('\n'), function() { + rli.close(); + }); + var cursorPos = rli._getCursorPos(); + assert.equal(cursorPos.rows, expectedLines.length - 1); + assert.equal(cursorPos.cols, expectedLines.slice(-1)[0].length); + rli.close(); + } + // wide characters should be treated as two columns. assert.equal(readline.isFullWidthCodePoint('a'.charCodeAt(0)), false); assert.equal(readline.isFullWidthCodePoint('あ'.charCodeAt(0)), true);