Skip to content

Commit 487a417

Browse files
cjihrigtargos
authored andcommitted
tty: expose stream API from readline methods
This commit exposes the return value and callback of the underlying readline APIs from the tty module. PR-URL: #28721 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 88809a4 commit 487a417

3 files changed

Lines changed: 70 additions & 18 deletions

File tree

‎doc/api/tty.md‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,41 @@ process.stdout.on('resize', () => {
9999
});
100100
```
101101

102-
### writeStream.clearLine(dir)
102+
### writeStream.clearLine(dir[, callback])
103103
<!-- YAML
104104
added: v0.7.7
105+
changes:
106+
- version: REPLACEME
107+
pr-url: https://github.com/nodejs/node/pull/28721
108+
description: The stream's write() callback and return value are exposed.
105109
-->
106110

107111
*`dir` {number}
108112
*`-1` - to the left from cursor
109113
*`1` - to the right from cursor
110114
*`0` - the entire line
115+
*`callback` {Function} Invoked once the operation completes.
116+
* Returns: {boolean} `false` if the stream wishes for the calling code to wait
117+
for the `'drain'` event to be emitted before continuing to write additional
118+
data; otherwise `true`.
111119

112120
`writeStream.clearLine()` clears the current line of this `WriteStream` in a
113121
direction identified by `dir`.
114122

115-
### writeStream.clearScreenDown()
123+
### writeStream.clearScreenDown([callback])
116124
<!-- YAML
117125
added: v0.7.7
126+
changes:
127+
- version: REPLACEME
128+
pr-url: https://github.com/nodejs/node/pull/28721
129+
description: The stream's write() callback and return value are exposed.
118130
-->
119131

132+
*`callback` {Function} Invoked once the operation completes.
133+
* Returns: {boolean} `false` if the stream wishes for the calling code to wait
134+
for the `'drain'` event to be emitted before continuing to write additional
135+
data; otherwise `true`.
136+
120137
`writeStream.clearScreenDown()` clears this `WriteStream` from the current
121138
cursor down.
122139

@@ -128,13 +145,21 @@ added: v0.7.7
128145
A `number` specifying the number of columns the TTY currently has. This property
129146
is updated whenever the `'resize'` event is emitted.
130147

131-
### writeStream.cursorTo(x, y)
148+
### writeStream.cursorTo(x, y[, callback])
132149
<!-- YAML
133150
added: v0.7.7
151+
changes:
152+
- version: REPLACEME
153+
pr-url: https://github.com/nodejs/node/pull/28721
154+
description: The stream's write() callback and return value are exposed.
134155
-->
135156

136157
*`x` {number}
137158
*`y` {number}
159+
*`callback` {Function} Invoked once the operation completes.
160+
* Returns: {boolean} `false` if the stream wishes for the calling code to wait
161+
for the `'drain'` event to be emitted before continuing to write additional
162+
data; otherwise `true`.
138163

139164
`writeStream.cursorTo()` moves this `WriteStream`'s cursor to the specified
140165
position.
@@ -220,13 +245,21 @@ added: v0.5.8
220245

221246
A `boolean` that is always `true`.
222247

223-
### writeStream.moveCursor(dx, dy)
248+
### writeStream.moveCursor(dx, dy[, callback])
224249
<!-- YAML
225250
added: v0.7.7
251+
changes:
252+
- version: REPLACEME
253+
pr-url: https://github.com/nodejs/node/pull/28721
254+
description: The stream's write() callback and return value are exposed.
226255
-->
227256

228257
*`dx` {number}
229258
*`dy` {number}
259+
*`callback` {Function} Invoked once the operation completes.
260+
* Returns: {boolean} `false` if the stream wishes for the calling code to wait
261+
for the `'drain'` event to be emitted before continuing to write additional
262+
data; otherwise `true`.
230263

231264
`writeStream.moveCursor()` moves this `WriteStream`'s cursor *relative* to its
232265
current position.

‎lib/tty.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,21 @@ WriteStream.prototype._refreshSize = function() {
137137
};
138138

139139
// Backwards-compat
140-
WriteStream.prototype.cursorTo=function(x,y){
140+
WriteStream.prototype.cursorTo=function(x,y,callback){
141141
if(readline===undefined)readline=require('readline');
142-
readline.cursorTo(this,x,y);
142+
returnreadline.cursorTo(this,x,y,callback);
143143
};
144-
WriteStream.prototype.moveCursor=function(dx,dy){
144+
WriteStream.prototype.moveCursor=function(dx,dy,callback){
145145
if(readline===undefined)readline=require('readline');
146-
readline.moveCursor(this,dx,dy);
146+
returnreadline.moveCursor(this,dx,dy,callback);
147147
};
148-
WriteStream.prototype.clearLine=function(dir){
148+
WriteStream.prototype.clearLine=function(dir,callback){
149149
if(readline===undefined)readline=require('readline');
150-
readline.clearLine(this,dir);
150+
returnreadline.clearLine(this,dir,callback);
151151
};
152-
WriteStream.prototype.clearScreenDown=function(){
152+
WriteStream.prototype.clearScreenDown=function(callback){
153153
if(readline===undefined)readline=require('readline');
154-
readline.clearScreenDown(this);
154+
returnreadline.clearScreenDown(this,callback);
155155
};
156156
WriteStream.prototype.getWindowSize=function(){
157157
return[this.columns,this.rows];
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Flags: --expose-internals
22
'use strict';
33
constcommon=require('../common');
4+
constassert=require('assert');
5+
constreadline=require('readline');
46

57
constnoop=()=>{};
68
const{ internalBinding }=require('internal/test/binding');
@@ -13,15 +15,32 @@ TTY.prototype = {
1315

1416
const{ WriteStream }=require('tty');
1517

16-
constmethods=[
18+
[
1719
'cursorTo',
1820
'moveCursor',
1921
'clearLine',
2022
'clearScreenDown'
21-
];
23+
].forEach((method)=>{
24+
readline[method]=common.mustCall(function(){
25+
constlastArg=arguments[arguments.length-1];
2226

23-
methods.forEach((method)=>{
24-
require('readline')[method]=common.mustCall();
25-
constwriteStream=newWriteStream(1);
26-
writeStream[method](1,2);
27+
if(typeoflastArg==='function'){
28+
process.nextTick(lastArg);
29+
}
30+
31+
returntrue;
32+
},2);
2733
});
34+
35+
constwriteStream=newWriteStream(1);
36+
37+
// Verify that the corresponding readline methods are called, that the return
38+
// values are propagated, and any callbacks are invoked.
39+
assert.strictEqual(writeStream.cursorTo(1,2),true);
40+
assert.strictEqual(writeStream.cursorTo(1,2,common.mustCall()),true);
41+
assert.strictEqual(writeStream.moveCursor(1,2),true);
42+
assert.strictEqual(writeStream.moveCursor(1,2,common.mustCall()),true);
43+
assert.strictEqual(writeStream.clearLine(1),true);
44+
assert.strictEqual(writeStream.clearLine(1,common.mustCall()),true);
45+
assert.strictEqual(writeStream.clearScreenDown(),true);
46+
assert.strictEqual(writeStream.clearScreenDown(common.mustCall()),true);

0 commit comments

Comments
 (0)