Skip to content

Commit 9b64025

Browse files
BridgeARtargos
authored andcommitted
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: nodejs#31293 Fixes: nodejs#31291 Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Anto Aravinth <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 3eb21f9 commit 9b64025

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

lib/internal/repl/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,9 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
382382
const { cursorPos, displayPos } = getPreviewPos();
383383
const rows = displayPos.rows - cursorPos.rows;
384384
moveCursor(repl.output, 0, rows);
385-
const { cols: resultCols } = repl._getDisplayPos(result);
386385
repl.output.write(`\n${result}`);
387-
moveCursor(repl.output, cursorPos.cols - resultCols, -rows - 1);
386+
cursorTo(repl.output, cursorPos.cols);
387+
moveCursor(repl.output, 0, -rows - 1);
388388
});
389389
};
390390

test/parallel/test-repl-history-navigation.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ const tests = [
254254
// 360 % 250 + 2 === 112 (+1)
255255
`${prompt}${'veryLongName'.repeat(30)}`, '\x1B[113G',
256256
// "// 'I should be previewed'".length + 86 === 112 (+1)
257-
"\n// 'I should be previewed'", '\x1B[86C\x1B[1A',
257+
"\n// 'I should be previewed'", '\x1B[113G', '\x1B[1A',
258258
// Preview cleanup
259259
'\x1B[1B', '\x1B[2K', '\x1B[1A',
260260
// 4. WORD LEFT
@@ -263,51 +263,51 @@ const tests = [
263263
'\x1B[1A',
264264
'\x1B[1G', '\x1B[0J',
265265
`${prompt}${'veryLongName'.repeat(30)}`, '\x1B[3G', '\x1B[1A',
266-
'\x1B[1B', "\n// 'I should be previewed'", '\x1B[24D\x1B[2A',
266+
'\x1B[1B', "\n// 'I should be previewed'", '\x1B[3G', '\x1B[2A',
267267
// Preview cleanup
268268
'\x1B[2B', '\x1B[2K', '\x1B[2A',
269269
// 5. UP
270270
'\x1B[1G', '\x1B[0J',
271271
`${prompt}e`, '\x1B[4G',
272272
// '// RangeError: visible'.length - 19 === 3 (+1)
273-
'\n// RangeError: visible', '\x1B[19D\x1B[1A',
273+
'\n// RangeError: visible', '\x1B[4G', '\x1B[1A',
274274
// Preview cleanup
275275
'\x1B[1B', '\x1B[2K', '\x1B[1A',
276276
// 6. Backspace
277277
'\x1B[1G', '\x1B[0J',
278278
'> ', '\x1B[3G', 'x', '1',
279279
`\n// '${'あ'.repeat(124)}'`,
280-
'\x1B[1C\x1B[1A',
280+
'\x1B[5G', '\x1B[1A',
281281
'\x1B[1B', '\x1B[2K', '\x1B[1A',
282282
'\x1B[1G', '\x1B[0J',
283283
'> x', '\x1B[4G', '2',
284284
`\n// '${'🐕'.repeat(124)}'`,
285-
'\x1B[1C\x1B[1A',
285+
'\x1B[5G', '\x1B[1A',
286286
'\x1B[1B', '\x1B[2K', '\x1B[1A',
287287
'\x1B[1G', '\x1B[0J',
288288
'> x', '\x1B[4G', '3',
289289
`\n// '${'𐐷'.repeat(248)}'`,
290-
'\x1B[1C\x1B[1A',
290+
'\x1B[5G', '\x1B[1A',
291291
'\x1B[1B', '\x1B[2K', '\x1B[1A',
292292
'\x1B[1G', '\x1B[0J',
293293
'> x', '\x1B[4G', '4',
294294
`\n// 'a${'\u0301'.repeat(1000)}'`,
295-
'\x1B[2D\x1B[1A',
295+
'\x1B[5G', '\x1B[1A',
296296
'\x1B[1B', '\x1B[2K', '\x1B[1A',
297297
'\x1B[1G', '\x1B[0J',
298298
'> ', '\x1B[3G', 'y', '1',
299299
`\n// '${'あ'.repeat(121)}...`,
300-
'\x1B[245D\x1B[1A',
300+
'\x1B[5G', '\x1B[1A',
301301
'\x1B[1B', '\x1B[2K', '\x1B[1A',
302302
'\x1B[1G', '\x1B[0J',
303303
'> y', '\x1B[4G', '2',
304304
`\n// '${'🐕'.repeat(121)}...`,
305-
'\x1B[245D\x1B[1A',
305+
'\x1B[5G', '\x1B[1A',
306306
'\x1B[1B', '\x1B[2K', '\x1B[1A',
307307
'\x1B[1G', '\x1B[0J',
308308
'> y', '\x1B[4G', '3',
309309
`\n// '${'𐐷'.repeat(242)}...`,
310-
'\x1B[245D\x1B[1A',
310+
'\x1B[5G', '\x1B[1A',
311311
'\x1B[1B', '\x1B[2K', '\x1B[1A',
312312
'\r\n',
313313
'\x1B[1G', '\x1B[0J',

test/parallel/test-repl-preview.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ async function tests(options) {
6868
const testCases = [
6969
['foo', [2, 4], '[Function: foo]',
7070
'foo',
71-
'\x1B[90m[Function: foo]\x1B[39m\x1B[5D\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
71+
'\x1B[90m[Function: foo]\x1B[39m\x1B[11G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
7272
'\x1B[36m[Function: foo]\x1B[39m',
7373
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
7474
['koo', [2, 4], '[Function: koo]',
7575
'k\x1B[90moo\x1B[39m\x1B[9G\x1B[0Ko\x1B[90mo\x1B[39m\x1B[10G\x1B[0Ko',
76-
'\x1B[90m[Function: koo]\x1B[39m\x1B[5D\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
76+
'\x1B[90m[Function: koo]\x1B[39m\x1B[11G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
7777
'\x1B[36m[Function: koo]\x1B[39m',
7878
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
7979
['a', [1, 2], undefined],
@@ -83,19 +83,19 @@ async function tests(options) {
8383
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
8484
['1n + 2n', [2, 5], '\x1B[33m3n\x1B[39m',
8585
'1n + 2',
86-
'\x1B[90mType[39m\x1B[57D\x1B[1A\x1B[1B\x1B[2K\x1B[1An',
87-
'\x1B[90m3n\x1B[39m\x1B[12C\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
86+
'\x1B[90mType[39m\x1B[14G\x1B[1A\x1B[1B\x1B[2K\x1B[1An',
87+
'\x1B[90m3n\x1B[39m\x1B[15G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
8888
'\x1B[33m3n\x1B[39m',
8989
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
9090
['{ a: true };', [2, 4], '\x1B[33mtrue\x1B[39m',
9191
'{ a: tru\x1B[90me\x1B[39m\x1B[16G\x1B[0Ke };',
92-
'\x1B[90mtrue\x1B[39m\x1B[15C\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
92+
'\x1B[90mtrue\x1B[39m\x1B[20G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
9393
'\x1B[33mtrue\x1B[39m',
9494
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
9595
[' \t { a: true};', [2, 5], '\x1B[33mtrue\x1B[39m',
9696
' \t { a: tru\x1B[90me\x1B[39m\x1B[26G\x1B[0Ke}',
97-
'\x1B[90m{ a: true }\x1B[39m\x1B[16C\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
98-
'\x1B[90mtrue\x1B[39m\x1B[24C\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
97+
'\x1B[90m{ a: true }\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
98+
'\x1B[90mtrue\x1B[39m\x1B[29G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
9999
'\x1B[33mtrue\x1B[39m',
100100
'\x1B[1G\x1B[0Jrepl > \x1B[8G']
101101
];

test/parallel/test-repl-reverse-search.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ const tests = [
223223
'\r\n',
224224
'\x1B[1G', '\x1B[0J',
225225
prompt, '\x1B[3G',
226-
'1', '+', '1', '\n// 2', '\x1B[1C\x1B[1A',
226+
'1', '+', '1', '\n// 2', '\x1B[6G', '\x1B[1A',
227227
'\x1B[1B', '\x1B[2K', '\x1B[1A',
228228
'\r\n',
229229
'2\n',
@@ -239,7 +239,7 @@ const tests = [
239239
'2\n',
240240
'\x1B[1G', '\x1B[0J',
241241
prompt, '\x1B[3G',
242-
'2', '\n// 2', '\x1B[1D\x1B[1A',
242+
'2', '\n// 2', '\x1B[4G', '\x1B[1A',
243243
'\x1B[1B', '\x1B[2K', '\x1B[1A',
244244
'\nbck-i-search: _', '\x1B[1A', '\x1B[4G',
245245
'\x1B[3G', '\x1B[0J',
@@ -252,7 +252,7 @@ const tests = [
252252
`${prompt}ab = "aaaa"`, '\x1B[14G',
253253
'\x1B[1G', '\x1B[0J',
254254
`${prompt}repl.repl.historyIndex`, '\x1B[25G', '\n// -1',
255-
'\x1B[19C\x1B[1A',
255+
'\x1B[25G', '\x1B[1A',
256256
'\x1B[1B', '\x1B[2K', '\x1B[1A',
257257
'\nfwd-i-search: _', '\x1B[1A', '\x1B[25G',
258258
'\x1B[3G', '\x1B[0J',

0 commit comments

Comments
 (0)