readline: establish y in cursorTo as optional

Parameter y in cursorTo() is optional and this is also verified by
tests but docs don't state this. Besides that if the newly added
parameter callback is used with no y, it's quite unhandy. This PR allows
to simply omit y.

PR-URL: https://github.com/nodejs/node/pull/29128
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Gerhard Stoebich 2019-08-14 22:35:41 +02:00 committed by Rich Trott
parent 4a2bd69db9
commit f25bbf1255
4 changed files with 15 additions and 2 deletions

View File

@ -487,7 +487,7 @@ function completer(linePartial, callback) {
}
```
## readline.cursorTo(stream, x, y[, callback])
## readline.cursorTo(stream, x[, y][, callback])
<!-- YAML
added: v0.7.7
changes:

View File

@ -145,7 +145,7 @@ added: v0.7.7
A `number` specifying the number of columns the TTY currently has. This property
is updated whenever the `'resize'` event is emitted.
### writeStream.cursorTo(x, y[, callback])
### writeStream.cursorTo(x[, y][, callback])
<!-- YAML
added: v0.7.7
changes:

View File

@ -1193,6 +1193,11 @@ function cursorTo(stream, x, y, callback) {
if (callback !== undefined && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);
if (typeof y === 'function') {
callback = y;
y = undefined;
}
if (stream == null || (typeof x !== 'number' && typeof y !== 'number')) {
if (typeof callback === 'function')
process.nextTick(callback);

View File

@ -133,6 +133,10 @@ writable.data = '';
assert.strictEqual(readline.cursorTo(writable, 1, 'a'), true);
assert.strictEqual(writable.data, '\x1b[2G');
writable.data = '';
assert.strictEqual(readline.cursorTo(writable, 1), true);
assert.strictEqual(writable.data, '\x1b[2G');
writable.data = '';
assert.strictEqual(readline.cursorTo(writable, 1, 2), true);
assert.strictEqual(writable.data, '\x1b[3;2H');
@ -141,6 +145,10 @@ writable.data = '';
assert.strictEqual(readline.cursorTo(writable, 1, 2, common.mustCall()), true);
assert.strictEqual(writable.data, '\x1b[3;2H');
writable.data = '';
assert.strictEqual(readline.cursorTo(writable, 1, common.mustCall()), true);
assert.strictEqual(writable.data, '\x1b[2G');
// Verify that cursorTo() throws on invalid callback.
assert.throws(() => {
readline.cursorTo(writable, 1, 1, null);