From 3867f2095ea9c433ff8bd6fb347dba0a85ed7439 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 10 Jan 2020 13:53:47 +0100 Subject: [PATCH] repl: fix preview cursor position The cusor position was off in case the preview was exactly as long as the current terminal was wide. PR-URL: https://github.com/nodejs/node/pull/31293 Fixes: https://github.com/nodejs/node/issues/31291 Reviewed-By: Yongsheng Zhang Reviewed-By: Anto Aravinth Reviewed-By: Rich Trott --- lib/internal/repl/utils.js | 4 ++-- test/parallel/test-repl-history-navigation.js | 20 +++++++++---------- test/parallel/test-repl-preview.js | 14 ++++++------- test/parallel/test-repl-reverse-search.js | 6 +++--- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index 826a3364c82ca4..6082fe7908ba73 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -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); }); }; diff --git a/test/parallel/test-repl-history-navigation.js b/test/parallel/test-repl-history-navigation.js index 3591f0f336b6e6..69ed7e062c9357 100644 --- a/test/parallel/test-repl-history-navigation.js +++ b/test/parallel/test-repl-history-navigation.js @@ -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 @@ -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', diff --git a/test/parallel/test-repl-preview.js b/test/parallel/test-repl-preview.js index 1c6dfd76b89bc1..179f4f78e2e473 100644 --- a/test/parallel/test-repl-preview.js +++ b/test/parallel/test-repl-preview.js @@ -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], @@ -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'] ]; diff --git a/test/parallel/test-repl-reverse-search.js b/test/parallel/test-repl-reverse-search.js index 1a2454b180ee95..e507dc285fa3a3 100644 --- a/test/parallel/test-repl-reverse-search.js +++ b/test/parallel/test-repl-reverse-search.js @@ -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', @@ -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', @@ -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',