Skip to content

Commit

Permalink
readline: establish y in cursorTo as optional
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
Flarna authored and targos committed Aug 19, 2019
1 parent e51f924 commit e474c67
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion doc/api/tty.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-readline-csi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down

0 comments on commit e474c67

Please sign in to comment.