Skip to content

Commit 17df75f

Browse files
cjihrigtargos
authored andcommitted
readline: expose stream API in clearScreenDown()
This commit adds an optional callback to clearScreenDown(), which is passed to the stream's write() method. It also exposes the return value of write(). PR-URL: #28641 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 3f65b91 commit 17df75f

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

doc/api/readline.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,20 @@ added: v0.7.7
360360
The `readline.clearLine()` method clears current line of given [TTY][] stream
361361
in a specified direction identified by `dir`.
362362

363-
## readline.clearScreenDown(stream)
363+
## readline.clearScreenDown(stream[, callback])
364364
<!-- YAML
365365
added: v0.7.7
366+
changes:
367+
- version: REPLACEME
368+
pr-url: https://github.com/nodejs/node/pull/28641
369+
description: The stream's write() callback and return value are exposed.
366370
-->
367371

368372
* `stream` {stream.Writable}
373+
* `callback` {Function} Invoked once the operation completes.
374+
* Returns: {boolean} `false` if `stream` wishes for the calling code to wait for
375+
the `'drain'` event to be emitted before continuing to write additional data;
376+
otherwise `true`.
369377

370378
The `readline.clearScreenDown()` method clears the given [TTY][] stream from
371379
the current position of the cursor down.

lib/readline.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
const { Math, Object } = primordials;
3131

3232
const {
33+
ERR_INVALID_CALLBACK,
3334
ERR_INVALID_CURSOR_POS,
3435
ERR_INVALID_OPT_VALUE
3536
} = require('internal/errors').codes;
@@ -1253,11 +1254,17 @@ function clearLine(stream, dir) {
12531254
* clears the screen from the current position of the cursor down
12541255
*/
12551256

1256-
function clearScreenDown(stream) {
1257-
if (stream === null || stream === undefined)
1258-
return;
1257+
function clearScreenDown(stream, callback) {
1258+
if (callback !== undefined && typeof callback !== 'function')
1259+
throw new ERR_INVALID_CALLBACK(callback);
1260+
1261+
if (stream === null || stream === undefined) {
1262+
if (typeof callback === 'function')
1263+
process.nextTick(callback);
1264+
return true;
1265+
}
12591266

1260-
stream.write(kClearScreenDown);
1267+
return stream.write(kClearScreenDown, callback);
12611268
}
12621269

12631270
module.exports = {

test/parallel/test-readline-csi.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ class TestWritable extends Writable {
2929

3030
const writable = new TestWritable();
3131

32-
readline.clearScreenDown(writable);
32+
assert.strictEqual(readline.clearScreenDown(writable), true);
3333
assert.deepStrictEqual(writable.data, CSI.kClearScreenDown);
34+
assert.strictEqual(readline.clearScreenDown(writable, common.mustCall()), true);
35+
36+
// Verify that clearScreenDown() throws on invalid callback.
37+
assert.throws(() => {
38+
readline.clearScreenDown(writable, null);
39+
}, /ERR_INVALID_CALLBACK/);
40+
41+
// Verify that clearScreenDown() does not throw on null or undefined stream.
42+
assert.strictEqual(readline.clearScreenDown(null, common.mustCall()), true);
43+
assert.strictEqual(readline.clearScreenDown(undefined, common.mustCall()),
44+
true);
3445

3546
writable.data = '';
3647
readline.clearLine(writable, -1);

0 commit comments

Comments
 (0)