From 0f5af4430471ff157e8db23536f2c7fda7f449b2 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 13 Jul 2019 18:08:19 -0400 Subject: [PATCH] readline: expose stream API in clearLine() This commit adds an optional callback to clearLine(), 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/28674 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat --- doc/api/readline.md | 10 +++++++++- lib/readline.js | 22 ++++++++++------------ test/parallel/test-readline-csi.js | 21 ++++++++++++++++++--- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/doc/api/readline.md b/doc/api/readline.md index b295ace5b3f00b..936ffb5d842788 100644 --- a/doc/api/readline.md +++ b/doc/api/readline.md @@ -346,9 +346,13 @@ async function processLineByLine() { } ``` -## readline.clearLine(stream, dir) +## readline.clearLine(stream, dir[, callback]) * `stream` {stream.Writable} @@ -356,6 +360,10 @@ added: v0.7.7 * `-1` - to the left from cursor * `1` - to the right from cursor * `0` - the entire line +* `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.clearLine()` method clears current line of given [TTY][] stream in a specified direction identified by `dir`. diff --git a/lib/readline.js b/lib/readline.js index fe0c093a5624a7..06e8581e7b0b35 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -1234,20 +1234,18 @@ function moveCursor(stream, dx, dy) { * 0 for the entire line */ -function clearLine(stream, dir) { - if (stream === null || stream === undefined) - return; +function clearLine(stream, dir, callback) { + if (callback !== undefined && typeof callback !== 'function') + throw new ERR_INVALID_CALLBACK(callback); - if (dir < 0) { - // to the beginning - stream.write(kClearToBeginning); - } else if (dir > 0) { - // to the end - stream.write(kClearToEnd); - } else { - // entire line - stream.write(kClearLine); + if (stream === null || stream === undefined) { + if (typeof callback === 'function') + process.nextTick(callback); + return true; } + + const type = dir < 0 ? kClearToBeginning : dir > 0 ? kClearToEnd : kClearLine; + return stream.write(type, callback); } /** diff --git a/test/parallel/test-readline-csi.js b/test/parallel/test-readline-csi.js index c753c5d93c26ec..becd4cd8d4dc05 100644 --- a/test/parallel/test-readline-csi.js +++ b/test/parallel/test-readline-csi.js @@ -44,17 +44,32 @@ assert.strictEqual(readline.clearScreenDown(undefined, common.mustCall()), true); writable.data = ''; -readline.clearLine(writable, -1); +assert.strictEqual(readline.clearLine(writable, -1), true); assert.deepStrictEqual(writable.data, CSI.kClearToBeginning); writable.data = ''; -readline.clearLine(writable, 1); +assert.strictEqual(readline.clearLine(writable, 1), true); assert.deepStrictEqual(writable.data, CSI.kClearToEnd); writable.data = ''; -readline.clearLine(writable, 0); +assert.strictEqual(readline.clearLine(writable, 0), true); assert.deepStrictEqual(writable.data, CSI.kClearLine); +writable.data = ''; +assert.strictEqual(readline.clearLine(writable, -1, common.mustCall()), true); +assert.deepStrictEqual(writable.data, CSI.kClearToBeginning); + +// Verify that clearLine() throws on invalid callback. +assert.throws(() => { + readline.clearLine(writable, 0, null); +}, /ERR_INVALID_CALLBACK/); + +// Verify that clearLine() does not throw on null or undefined stream. +assert.strictEqual(readline.clearLine(null, 0), true); +assert.strictEqual(readline.clearLine(undefined, 0), true); +assert.strictEqual(readline.clearLine(null, 0, common.mustCall()), true); +assert.strictEqual(readline.clearLine(undefined, 0, common.mustCall()), true); + // Nothing is written when moveCursor 0, 0 [ [0, 0, ''],