7
7
} = primordials ;
8
8
9
9
const { CSI } = require ( 'internal/readline/utils' ) ;
10
- const { validateInteger } = require ( 'internal/validators' ) ;
10
+ const { validateBoolean , validateInteger } = require ( 'internal/validators' ) ;
11
11
const { isWritable } = require ( 'internal/streams/utils' ) ;
12
12
const { codes : { ERR_INVALID_ARG_TYPE } } = require ( 'internal/errors' ) ;
13
13
@@ -19,13 +19,18 @@ const {
19
19
} = CSI ;
20
20
21
21
class Readline {
22
+ #autoCommit = false ;
22
23
#stream;
23
24
#todo = [ ] ;
24
25
25
- constructor ( stream ) {
26
+ constructor ( stream , options = undefined ) {
26
27
if ( ! isWritable ( stream ) )
27
28
throw new ERR_INVALID_ARG_TYPE ( 'stream' , 'Writable' , stream ) ;
28
29
this . #stream = stream ;
30
+ if ( options ?. autoCommit != null ) {
31
+ validateBoolean ( options . autoCommit , 'options.autoCommit' ) ;
32
+ this . #autoCommit = options . autoCommit ;
33
+ }
29
34
}
30
35
31
36
/**
@@ -38,10 +43,9 @@ class Readline {
38
43
validateInteger ( x , 'x' ) ;
39
44
if ( y != null ) validateInteger ( y , 'y' ) ;
40
45
41
- ArrayPrototypePush (
42
- this . #todo,
43
- y == null ? CSI `${ x + 1 } G` : CSI `${ y + 1 } ;${ x + 1 } H`
44
- ) ;
46
+ const data = y == null ? CSI `${ x + 1 } G` : CSI `${ y + 1 } ;${ x + 1 } H` ;
47
+ if ( this . #autoCommit) process . nextTick ( ( ) => this . #stream. write ( data ) ) ;
48
+ else ArrayPrototypePush ( this . #todo, data ) ;
45
49
46
50
return this ;
47
51
}
@@ -70,7 +74,8 @@ class Readline {
70
74
} else if ( dy > 0 ) {
71
75
data += CSI `${ dy } B` ;
72
76
}
73
- ArrayPrototypePush ( this . #todo, data ) ;
77
+ if ( this . #autoCommit) process . nextTick ( ( ) => this . #stream. write ( data ) ) ;
78
+ else ArrayPrototypePush ( this . #todo, data ) ;
74
79
}
75
80
return this ;
76
81
}
@@ -86,10 +91,12 @@ class Readline {
86
91
clearLine ( dir ) {
87
92
validateInteger ( dir , 'dir' , - 1 , 1 ) ;
88
93
89
- ArrayPrototypePush (
90
- this . #todo,
91
- dir < 0 ? kClearToLineBeginning : dir > 0 ? kClearToLineEnd : kClearLine
92
- ) ;
94
+ const data =
95
+ dir < 0 ? kClearToLineBeginning :
96
+ dir > 0 ? kClearToLineEnd :
97
+ kClearLine ;
98
+ if ( this . #autoCommit) process . nextTick ( ( ) => this . #stream. write ( data ) ) ;
99
+ else ArrayPrototypePush ( this . #todo, data ) ;
93
100
return this ;
94
101
}
95
102
@@ -98,7 +105,11 @@ class Readline {
98
105
* @returns {Readline } this
99
106
*/
100
107
clearScreenDown ( ) {
101
- ArrayPrototypePush ( this . #todo, kClearScreenDown ) ;
108
+ if ( this . #autoCommit) {
109
+ process . nextTick ( ( ) => this . #stream. write ( kClearScreenDown ) ) ;
110
+ } else {
111
+ ArrayPrototypePush ( this . #todo, kClearScreenDown ) ;
112
+ }
102
113
return this ;
103
114
}
104
115
0 commit comments