Skip to content

Commit 0f5af44

Browse files
cjihrigtargos
authored andcommitted
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: #28674 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 2308c74 commit 0f5af44

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

doc/api/readline.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,24 @@ async function processLineByLine() {
346346
}
347347
```
348348

349-
## readline.clearLine(stream, dir)
349+
## readline.clearLine(stream, dir[, callback])
350350
<!-- YAML
351351
added: v0.7.7
352+
changes:
353+
- version: REPLACEME
354+
pr-url: https://github.com/nodejs/node/pull/28674
355+
description: The stream's write() callback and return value are exposed.
352356
-->
353357

354358
* `stream` {stream.Writable}
355359
* `dir` {number}
356360
* `-1` - to the left from cursor
357361
* `1` - to the right from cursor
358362
* `0` - the entire line
363+
* `callback` {Function} Invoked once the operation completes.
364+
* Returns: {boolean} `false` if `stream` wishes for the calling code to wait for
365+
the `'drain'` event to be emitted before continuing to write additional data;
366+
otherwise `true`.
359367

360368
The `readline.clearLine()` method clears current line of given [TTY][] stream
361369
in a specified direction identified by `dir`.

lib/readline.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -1234,20 +1234,18 @@ function moveCursor(stream, dx, dy) {
12341234
* 0 for the entire line
12351235
*/
12361236

1237-
function clearLine(stream, dir) {
1238-
if (stream === null || stream === undefined)
1239-
return;
1237+
function clearLine(stream, dir, callback) {
1238+
if (callback !== undefined && typeof callback !== 'function')
1239+
throw new ERR_INVALID_CALLBACK(callback);
12401240

1241-
if (dir < 0) {
1242-
// to the beginning
1243-
stream.write(kClearToBeginning);
1244-
} else if (dir > 0) {
1245-
// to the end
1246-
stream.write(kClearToEnd);
1247-
} else {
1248-
// entire line
1249-
stream.write(kClearLine);
1241+
if (stream === null || stream === undefined) {
1242+
if (typeof callback === 'function')
1243+
process.nextTick(callback);
1244+
return true;
12501245
}
1246+
1247+
const type = dir < 0 ? kClearToBeginning : dir > 0 ? kClearToEnd : kClearLine;
1248+
return stream.write(type, callback);
12511249
}
12521250

12531251
/**

test/parallel/test-readline-csi.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,32 @@ assert.strictEqual(readline.clearScreenDown(undefined, common.mustCall()),
4444
true);
4545

4646
writable.data = '';
47-
readline.clearLine(writable, -1);
47+
assert.strictEqual(readline.clearLine(writable, -1), true);
4848
assert.deepStrictEqual(writable.data, CSI.kClearToBeginning);
4949

5050
writable.data = '';
51-
readline.clearLine(writable, 1);
51+
assert.strictEqual(readline.clearLine(writable, 1), true);
5252
assert.deepStrictEqual(writable.data, CSI.kClearToEnd);
5353

5454
writable.data = '';
55-
readline.clearLine(writable, 0);
55+
assert.strictEqual(readline.clearLine(writable, 0), true);
5656
assert.deepStrictEqual(writable.data, CSI.kClearLine);
5757

58+
writable.data = '';
59+
assert.strictEqual(readline.clearLine(writable, -1, common.mustCall()), true);
60+
assert.deepStrictEqual(writable.data, CSI.kClearToBeginning);
61+
62+
// Verify that clearLine() throws on invalid callback.
63+
assert.throws(() => {
64+
readline.clearLine(writable, 0, null);
65+
}, /ERR_INVALID_CALLBACK/);
66+
67+
// Verify that clearLine() does not throw on null or undefined stream.
68+
assert.strictEqual(readline.clearLine(null, 0), true);
69+
assert.strictEqual(readline.clearLine(undefined, 0), true);
70+
assert.strictEqual(readline.clearLine(null, 0, common.mustCall()), true);
71+
assert.strictEqual(readline.clearLine(undefined, 0, common.mustCall()), true);
72+
5873
// Nothing is written when moveCursor 0, 0
5974
[
6075
[0, 0, ''],

0 commit comments

Comments
 (0)