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 <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 4a7e20f commit caee910

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

doc/api/readline.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,22 @@ function completer(linePartial, callback) {
487487
}
488488
```
489489

490-
## readline.cursorTo(stream, x, y)
490+
## readline.cursorTo(stream, x, y[, callback])
491491
<!-- YAML
492492
added: v0.7.7
493+
changes:
494+
- version: REPLACEME
495+
pr-url: https://github.com/nodejs/node/pull/28674
496+
description: The stream's write() callback and return value are exposed.
493497
-->
494498

495499
* `stream` {stream.Writable}
496500
* `x` {number}
497501
* `y` {number}
502+
* `callback` {Function} Invoked once the operation completes.
503+
* Returns: {boolean} `false` if `stream` wishes for the calling code to wait for
504+
the `'drain'` event to be emitted before continuing to write additional data;
505+
otherwise `true`.
498506

499507
The `readline.cursorTo()` method moves cursor to the specified position in a
500508
given [TTY][] `stream`.

lib/readline.js

+10-10
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

+17-6
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,17 @@ assert.strictEqual(readline.moveCursor(undefined, 1, 1, common.mustCall()),
106106
true);
107107

108108
// Undefined or null as stream should not throw.
109-
readline.cursorTo(null);
110-
readline.cursorTo();
109+
assert.strictEqual(readline.cursorTo(null), true);
110+
assert.strictEqual(readline.cursorTo(), true);
111+
assert.strictEqual(readline.cursorTo(null, 1, 1, common.mustCall()), true);
112+
assert.strictEqual(readline.cursorTo(undefined, 1, 1, common.mustCall()), true);
111113

112114
writable.data = '';
113-
readline.cursorTo(writable, 'a');
115+
assert.strictEqual(readline.cursorTo(writable, 'a'), true);
114116
assert.strictEqual(writable.data, '');
115117

116118
writable.data = '';
117-
readline.cursorTo(writable, 'a', 'b');
119+
assert.strictEqual(readline.cursorTo(writable, 'a', 'b'), true);
118120
assert.strictEqual(writable.data, '');
119121

120122
writable.data = '';
@@ -128,9 +130,18 @@ common.expectsError(
128130
assert.strictEqual(writable.data, '');
129131

130132
writable.data = '';
131-
readline.cursorTo(writable, 1, 'a');
133+
assert.strictEqual(readline.cursorTo(writable, 1, 'a'), true);
132134
assert.strictEqual(writable.data, '\x1b[2G');
133135

134136
writable.data = '';
135-
readline.cursorTo(writable, 1, 2);
137+
assert.strictEqual(readline.cursorTo(writable, 1, 2), true);
136138
assert.strictEqual(writable.data, '\x1b[3;2H');
139+
140+
writable.data = '';
141+
assert.strictEqual(readline.cursorTo(writable, 1, 2, common.mustCall()), true);
142+
assert.strictEqual(writable.data, '\x1b[3;2H');
143+
144+
// Verify that cursorTo() throws on invalid callback.
145+
assert.throws(() => {
146+
readline.cursorTo(writable, 1, 1, null);
147+
}, /ERR_INVALID_CALLBACK/);

0 commit comments

Comments
 (0)