From 79cc8bb24117aa40e101971610d5813d927f9fbe Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 13 Jul 2019 18:08:19 -0400 Subject: 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 --- lib/readline.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'lib/readline.js') diff --git a/lib/readline.js b/lib/readline.js index d50b1d5a44..6a60df82c7 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); } /** -- cgit v1.2.3