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: https://github.com/nodejs/node/pull/28641 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
a013aa0f5e
commit
c31f7e595a
@ -360,12 +360,20 @@ added: v0.7.7
|
|||||||
The `readline.clearLine()` method clears current line of given [TTY][] stream
|
The `readline.clearLine()` method clears current line of given [TTY][] stream
|
||||||
in a specified direction identified by `dir`.
|
in a specified direction identified by `dir`.
|
||||||
|
|
||||||
## readline.clearScreenDown(stream)
|
## readline.clearScreenDown(stream[, callback])
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v0.7.7
|
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}
|
* `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 `readline.clearScreenDown()` method clears the given [TTY][] stream from
|
||||||
the current position of the cursor down.
|
the current position of the cursor down.
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
const { Math, Object } = primordials;
|
const { Math, Object } = primordials;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
ERR_INVALID_CALLBACK,
|
||||||
ERR_INVALID_CURSOR_POS,
|
ERR_INVALID_CURSOR_POS,
|
||||||
ERR_INVALID_OPT_VALUE
|
ERR_INVALID_OPT_VALUE
|
||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
@ -1253,11 +1254,17 @@ function clearLine(stream, dir) {
|
|||||||
* clears the screen from the current position of the cursor down
|
* clears the screen from the current position of the cursor down
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function clearScreenDown(stream) {
|
function clearScreenDown(stream, callback) {
|
||||||
if (stream === null || stream === undefined)
|
if (callback !== undefined && typeof callback !== 'function')
|
||||||
return;
|
throw new ERR_INVALID_CALLBACK(callback);
|
||||||
|
|
||||||
stream.write(kClearScreenDown);
|
if (stream === null || stream === undefined) {
|
||||||
|
if (typeof callback === 'function')
|
||||||
|
process.nextTick(callback);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return stream.write(kClearScreenDown, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -29,8 +29,19 @@ class TestWritable extends Writable {
|
|||||||
|
|
||||||
const writable = new TestWritable();
|
const writable = new TestWritable();
|
||||||
|
|
||||||
readline.clearScreenDown(writable);
|
assert.strictEqual(readline.clearScreenDown(writable), true);
|
||||||
assert.deepStrictEqual(writable.data, CSI.kClearScreenDown);
|
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 = '';
|
writable.data = '';
|
||||||
readline.clearLine(writable, -1);
|
readline.clearLine(writable, -1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user