Skip to content

Commit

Permalink
repl: fix preview cursor position
Browse files Browse the repository at this point in the history
The cusor position was off in case the preview was exactly as long
as the current terminal was wide.

PR-URL: nodejs#31293
Fixes: nodejs#31291
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Apr 25, 2020
1 parent 3eb21f9 commit 9b64025
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/internal/repl/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
const { cursorPos, displayPos } = getPreviewPos();
const rows = displayPos.rows - cursorPos.rows;
moveCursor(repl.output, 0, rows);
const { cols: resultCols } = repl._getDisplayPos(result);
repl.output.write(`\n${result}`);
moveCursor(repl.output, cursorPos.cols - resultCols, -rows - 1);
cursorTo(repl.output, cursorPos.cols);
moveCursor(repl.output, 0, -rows - 1);
});
};

Expand Down
20 changes: 10 additions & 10 deletions test/parallel/test-repl-history-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ const tests = [
// 360 % 250 + 2 === 112 (+1)
`${prompt}${'veryLongName'.repeat(30)}`, '\x1B[113G',
// "// 'I should be previewed'".length + 86 === 112 (+1)
"\n// 'I should be previewed'", '\x1B[86C\x1B[1A',
"\n// 'I should be previewed'", '\x1B[113G', '\x1B[1A',
// Preview cleanup
'\x1B[1B', '\x1B[2K', '\x1B[1A',
// 4. WORD LEFT
Expand All @@ -263,51 +263,51 @@ const tests = [
'\x1B[1A',
'\x1B[1G', '\x1B[0J',
`${prompt}${'veryLongName'.repeat(30)}`, '\x1B[3G', '\x1B[1A',
'\x1B[1B', "\n// 'I should be previewed'", '\x1B[24D\x1B[2A',
'\x1B[1B', "\n// 'I should be previewed'", '\x1B[3G', '\x1B[2A',
// Preview cleanup
'\x1B[2B', '\x1B[2K', '\x1B[2A',
// 5. UP
'\x1B[1G', '\x1B[0J',
`${prompt}e`, '\x1B[4G',
// '// RangeError: visible'.length - 19 === 3 (+1)
'\n// RangeError: visible', '\x1B[19D\x1B[1A',
'\n// RangeError: visible', '\x1B[4G', '\x1B[1A',
// Preview cleanup
'\x1B[1B', '\x1B[2K', '\x1B[1A',
// 6. Backspace
'\x1B[1G', '\x1B[0J',
'> ', '\x1B[3G', 'x', '1',
`\n// '${'あ'.repeat(124)}'`,
'\x1B[1C\x1B[1A',
'\x1B[5G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\x1B[1G', '\x1B[0J',
'> x', '\x1B[4G', '2',
`\n// '${'🐕'.repeat(124)}'`,
'\x1B[1C\x1B[1A',
'\x1B[5G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\x1B[1G', '\x1B[0J',
'> x', '\x1B[4G', '3',
`\n// '${'𐐷'.repeat(248)}'`,
'\x1B[1C\x1B[1A',
'\x1B[5G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\x1B[1G', '\x1B[0J',
'> x', '\x1B[4G', '4',
`\n// 'a${'\u0301'.repeat(1000)}'`,
'\x1B[2D\x1B[1A',
'\x1B[5G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\x1B[1G', '\x1B[0J',
'> ', '\x1B[3G', 'y', '1',
`\n// '${'あ'.repeat(121)}...`,
'\x1B[245D\x1B[1A',
'\x1B[5G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\x1B[1G', '\x1B[0J',
'> y', '\x1B[4G', '2',
`\n// '${'🐕'.repeat(121)}...`,
'\x1B[245D\x1B[1A',
'\x1B[5G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\x1B[1G', '\x1B[0J',
'> y', '\x1B[4G', '3',
`\n// '${'𐐷'.repeat(242)}...`,
'\x1B[245D\x1B[1A',
'\x1B[5G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\r\n',
'\x1B[1G', '\x1B[0J',
Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-repl-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ async function tests(options) {
const testCases = [
['foo', [2, 4], '[Function: foo]',
'foo',
'\x1B[90m[Function: foo]\x1B[39m\x1B[5D\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[90m[Function: foo]\x1B[39m\x1B[11G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[36m[Function: foo]\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
['koo', [2, 4], '[Function: koo]',
'k\x1B[90moo\x1B[39m\x1B[9G\x1B[0Ko\x1B[90mo\x1B[39m\x1B[10G\x1B[0Ko',
'\x1B[90m[Function: koo]\x1B[39m\x1B[5D\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[90m[Function: koo]\x1B[39m\x1B[11G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[36m[Function: koo]\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
['a', [1, 2], undefined],
Expand All @@ -83,19 +83,19 @@ async function tests(options) {
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
['1n + 2n', [2, 5], '\x1B[33m3n\x1B[39m',
'1n + 2',
'\x1B[90mType[39m\x1B[57D\x1B[1A\x1B[1B\x1B[2K\x1B[1An',
'\x1B[90m3n\x1B[39m\x1B[12C\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[90mType[39m\x1B[14G\x1B[1A\x1B[1B\x1B[2K\x1B[1An',
'\x1B[90m3n\x1B[39m\x1B[15G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[33m3n\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
['{ a: true };', [2, 4], '\x1B[33mtrue\x1B[39m',
'{ a: tru\x1B[90me\x1B[39m\x1B[16G\x1B[0Ke };',
'\x1B[90mtrue\x1B[39m\x1B[15C\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[90mtrue\x1B[39m\x1B[20G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[33mtrue\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
[' \t { a: true};', [2, 5], '\x1B[33mtrue\x1B[39m',
' \t { a: tru\x1B[90me\x1B[39m\x1B[26G\x1B[0Ke}',
'\x1B[90m{ a: true }\x1B[39m\x1B[16C\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
'\x1B[90mtrue\x1B[39m\x1B[24C\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[90m{ a: true }\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
'\x1B[90mtrue\x1B[39m\x1B[29G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
'\x1B[33mtrue\x1B[39m',
'\x1B[1G\x1B[0Jrepl > \x1B[8G']
];
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-repl-reverse-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ const tests = [
'\r\n',
'\x1B[1G', '\x1B[0J',
prompt, '\x1B[3G',
'1', '+', '1', '\n// 2', '\x1B[1C\x1B[1A',
'1', '+', '1', '\n// 2', '\x1B[6G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\r\n',
'2\n',
Expand All @@ -239,7 +239,7 @@ const tests = [
'2\n',
'\x1B[1G', '\x1B[0J',
prompt, '\x1B[3G',
'2', '\n// 2', '\x1B[1D\x1B[1A',
'2', '\n// 2', '\x1B[4G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\nbck-i-search: _', '\x1B[1A', '\x1B[4G',
'\x1B[3G', '\x1B[0J',
Expand All @@ -252,7 +252,7 @@ const tests = [
`${prompt}ab = "aaaa"`, '\x1B[14G',
'\x1B[1G', '\x1B[0J',
`${prompt}repl.repl.historyIndex`, '\x1B[25G', '\n// -1',
'\x1B[19C\x1B[1A',
'\x1B[25G', '\x1B[1A',
'\x1B[1B', '\x1B[2K', '\x1B[1A',
'\nfwd-i-search: _', '\x1B[1A', '\x1B[25G',
'\x1B[3G', '\x1B[0J',
Expand Down

0 comments on commit 9b64025

Please sign in to comment.