@@ -9,30 +9,71 @@ const fakeTTYs = new Set();
99const { isatty} = tty ;
1010tty . isatty = fd => fakeTTYs . has ( fd ) || isatty ( fd ) ;
1111
12- const simulateTTY = ( stream , { colorDepth, hasColors, columns, rows} ) => {
13- Object . assign ( stream , { isTTY : true , columns, rows} ) ;
12+ const takesCallbackAndReturnWriteResult = tty . WriteStream . prototype . clearLine . length === 1 ;
13+
14+ const assertCallback = callback => {
15+ // FIXME: Better replicate Node.js' internal errors.
16+ if ( callback !== undefined && typeof callback !== 'function' ) {
17+ const error = new TypeError ( 'Callback must be a function' ) ;
18+ error . code = 'ERR_INVALID_CALLBACK' ;
19+ throw error ;
20+ }
21+ } ;
22+
23+ const fakeWriters = {
24+ clearLine ( dir , callback ) {
25+ assertCallback ( callback ) ;
1426
15- stream . clearLine = dir => {
1627 switch ( dir ) {
1728 case - 1 :
18- stream . write ( ansiEscapes . eraseStartLine ) ;
19- break ;
29+ return this . write ( ansiEscapes . eraseStartLine , callback ) ;
2030 case 1 :
21- stream . write ( ansiEscapes . eraseEndLine ) ;
22- break ;
31+ return this . write ( ansiEscapes . eraseEndLine , callback ) ;
2332 default :
24- stream . write ( ansiEscapes . eraseLine ) ;
33+ return this . write ( ansiEscapes . eraseLine , callback ) ;
2534 }
26- } ;
35+ } ,
2736
28- stream . clearScreenDown = ( ) => stream . write ( ansiEscapes . eraseDown ) ;
37+ clearScreenDown ( callback ) {
38+ assertCallback ( callback ) ;
2939
30- stream . cursorTo = ( x , y ) => stream . write ( ansiEscapes . cursorTo ( x , y ) ) ;
40+ return this . write ( ansiEscapes . eraseDown , callback ) ;
41+ } ,
3142
32- stream . getWindowSize = ( ) => [ 80 , 24 ] ;
43+ cursorTo ( x , y , callback ) {
44+ assertCallback ( callback ) ;
45+ return this . write ( ansiEscapes . cursorTo ( x , y ) , callback ) ;
46+ } ,
3347
34- stream . moveCursor = ( x , y ) => stream . write ( ansiEscapes . cursorMove ( x , y ) ) ;
48+ moveCursor ( x , y , callback ) {
49+ assertCallback ( callback ) ;
50+ return this . write ( ansiEscapes . cursorMove ( x , y ) , callback ) ;
51+ }
52+ } ;
53+
54+ const simulateTTY = ( stream , { colorDepth, hasColors, columns, rows} ) => {
55+ Object . assign ( stream , { isTTY : true , columns, rows} ) ;
3556
57+ if ( takesCallbackAndReturnWriteResult ) {
58+ Object . assign ( stream , fakeWriters ) ;
59+ } else {
60+ Object . assign ( stream , {
61+ clearLine ( dir ) {
62+ fakeWriters . clearLine . call ( this , dir ) ;
63+ } ,
64+ clearScreenDown ( ) {
65+ fakeWriters . clearScreenDown . call ( this ) ;
66+ } ,
67+ cursorTo ( x , y ) {
68+ fakeWriters . cursorTo . call ( this , x , y ) ;
69+ } ,
70+ moveCursor ( x , y ) {
71+ fakeWriters . moveCursor . call ( this , x , y ) ;
72+ }
73+ } ) ;
74+ }
75+
76+ stream . getWindowSize = ( ) => [ 80 , 24 ] ;
3677 if ( colorDepth !== undefined ) {
3778 stream . getColorDepth = ( ) => colorDepth ;
3879 }
0 commit comments