Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: expose stream API in clearScreenDown() #28641

Merged
merged 1 commit into from
Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,20 @@ added: v0.7.7
The `readline.clearLine()` method clears current line of given [TTY][] stream
in a specified direction identified by `dir`.

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

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

The `readline.clearScreenDown()` method clears the given [TTY][] stream from
the current position of the cursor down.
Expand Down
15 changes: 11 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
const { Math, Object } = primordials;

const {
ERR_INVALID_CALLBACK,
ERR_INVALID_CURSOR_POS,
ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes;
Expand Down Expand Up @@ -1253,11 +1254,17 @@ function clearLine(stream, dir) {
* clears the screen from the current position of the cursor down
*/

function clearScreenDown(stream) {
if (stream === null || stream === undefined)
return;
function clearScreenDown(stream, callback) {
if (callback !== undefined && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);

if (stream === null || stream === undefined) {
if (typeof callback === 'function')
process.nextTick(callback);
return true;
}

stream.write(kClearScreenDown);
return stream.write(kClearScreenDown, callback);
}

module.exports = {
Expand Down
13 changes: 12 additions & 1 deletion test/parallel/test-readline-csi.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ class TestWritable extends Writable {

const writable = new TestWritable();

readline.clearScreenDown(writable);
assert.strictEqual(readline.clearScreenDown(writable), true);
assert.deepStrictEqual(writable.data, CSI.kClearScreenDown);
assert.strictEqual(readline.clearScreenDown(writable, common.mustCall()), true);

// Verify that clearScreenDown() throws on invalid callback.
assert.throws(() => {
readline.clearScreenDown(writable, null);
}, /ERR_INVALID_CALLBACK/);

// Verify that clearScreenDown() does not throw on null or undefined stream.
assert.strictEqual(readline.clearScreenDown(null, common.mustCall()), true);
assert.strictEqual(readline.clearScreenDown(undefined, common.mustCall()),
true);

writable.data = '';
readline.clearLine(writable, -1);
Expand Down