Skip to content

Commit 4a7e20f

Browse files
cjihrigtargos
authored andcommitted
readline: expose stream API in moveCursor()
This commit adds an optional callback to moveCursor(), which is passed to the stream's write() method. It also exposes the return value of write(). PR-URL: #28674 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 0f5af44 commit 4a7e20f

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

doc/api/readline.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,22 @@ if (process.stdin.isTTY)
525525
process.stdin.setRawMode(true);
526526
```
527527

528-
## readline.moveCursor(stream, dx, dy)
528+
## readline.moveCursor(stream, dx, dy[, callback])
529529
<!-- YAML
530530
added: v0.7.7
531+
changes:
532+
- version: REPLACEME
533+
pr-url: https://github.com/nodejs/node/pull/28674
534+
description: The stream's write() callback and return value are exposed.
531535
-->
532536

533537
* `stream` {stream.Writable}
534538
* `dx` {number}
535539
* `dy` {number}
540+
* `callback` {Function} Invoked once the operation completes.
541+
* Returns: {boolean} `false` if `stream` wishes for the calling code to wait for
542+
the `'drain'` event to be emitted before continuing to write additional data;
543+
otherwise `true`.
536544

537545
The `readline.moveCursor()` method moves the cursor *relative* to its current
538546
position in a given [TTY][] `stream`.

lib/readline.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -1210,21 +1210,31 @@ function cursorTo(stream, x, y) {
12101210
* moves the cursor relative to its current location
12111211
*/
12121212

1213-
function moveCursor(stream, dx, dy) {
1214-
if (stream === null || stream === undefined)
1215-
return;
1213+
function moveCursor(stream, dx, dy, callback) {
1214+
if (callback !== undefined && typeof callback !== 'function')
1215+
throw new ERR_INVALID_CALLBACK(callback);
1216+
1217+
if (stream == null || !(dx || dy)) {
1218+
if (typeof callback === 'function')
1219+
process.nextTick(callback);
1220+
return true;
1221+
}
1222+
1223+
let data = '';
12161224

12171225
if (dx < 0) {
1218-
stream.write(CSI`${-dx}D`);
1226+
data += CSI`${-dx}D`;
12191227
} else if (dx > 0) {
1220-
stream.write(CSI`${dx}C`);
1228+
data += CSI`${dx}C`;
12211229
}
12221230

12231231
if (dy < 0) {
1224-
stream.write(CSI`${-dy}A`);
1232+
data += CSI`${-dy}A`;
12251233
} else if (dy > 0) {
1226-
stream.write(CSI`${dy}B`);
1234+
data += CSI`${dy}B`;
12271235
}
1236+
1237+
return stream.write(data, callback);
12281238
}
12291239

12301240
/**

test/parallel/test-readline-csi.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,28 @@ assert.strictEqual(readline.clearLine(undefined, 0, common.mustCall()), true);
8383
[1, -1, '\x1b[1C\x1b[1A'],
8484
].forEach((set) => {
8585
writable.data = '';
86-
readline.moveCursor(writable, set[0], set[1]);
86+
assert.strictEqual(readline.moveCursor(writable, set[0], set[1]), true);
87+
assert.deepStrictEqual(writable.data, set[2]);
88+
writable.data = '';
89+
assert.strictEqual(
90+
readline.moveCursor(writable, set[0], set[1], common.mustCall()),
91+
true
92+
);
8793
assert.deepStrictEqual(writable.data, set[2]);
8894
});
8995

96+
// Verify that moveCursor() throws on invalid callback.
97+
assert.throws(() => {
98+
readline.moveCursor(writable, 1, 1, null);
99+
}, /ERR_INVALID_CALLBACK/);
100+
101+
// Verify that moveCursor() does not throw on null or undefined stream.
102+
assert.strictEqual(readline.moveCursor(null, 1, 1), true);
103+
assert.strictEqual(readline.moveCursor(undefined, 1, 1), true);
104+
assert.strictEqual(readline.moveCursor(null, 1, 1, common.mustCall()), true);
105+
assert.strictEqual(readline.moveCursor(undefined, 1, 1, common.mustCall()),
106+
true);
107+
90108
// Undefined or null as stream should not throw.
91109
readline.cursorTo(null);
92110
readline.cursorTo();

0 commit comments

Comments
 (0)