readline: expose stream API in cursorTo() · nodejs/node@caee910 · GitHub
Skip to content

Commit caee910

Browse files
cjihrigtargos
authored andcommitted
readline: expose stream API in cursorTo()
This commit adds an optional callback to cursorTo(), which is passed to the stream's write() method. It also exposes the return value of write(). PR-URL: #28674 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 4a7e20f commit caee910

3 files changed

Lines changed: 36 additions & 17 deletions

File tree

doc/api/readline.md

Lines changed: 9 additions & 1 deletion

lib/readline.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,21 +1189,21 @@ function emitKeypressEvents(stream, iface) {
11891189
* moves the cursor to the x and y coordinate on the given stream
11901190
*/
11911191

1192-
function cursorTo(stream, x, y) {
1193-
if (stream === null || stream === undefined)
1194-
return;
1192+
function cursorTo(stream, x, y, callback) {
1193+
if (callback !== undefined && typeof callback !== 'function')
1194+
throw new ERR_INVALID_CALLBACK(callback);
11951195

1196-
if (typeof x !== 'number' && typeof y !== 'number')
1197-
return;
1196+
if (stream == null || (typeof x !== 'number' && typeof y !== 'number')) {
1197+
if (typeof callback === 'function')
1198+
process.nextTick(callback);
1199+
return true;
1200+
}
11981201

11991202
if (typeof x !== 'number')
12001203
throw new ERR_INVALID_CURSOR_POS();
12011204

1202-
if (typeof y !== 'number') {
1203-
stream.write(CSI`${x + 1}G`);
1204-
} else {
1205-
stream.write(CSI`${y + 1};${x + 1}H`);
1206-
}
1205+
const data = typeof y !== 'number' ? CSI`${x + 1}G` : CSI`${y + 1};${x + 1}H`;
1206+
return stream.write(data, callback);
12071207
}
12081208

12091209
/**

test/parallel/test-readline-csi.js

Lines changed: 17 additions & 6 deletions

0 commit comments

Comments
 (0)