1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | "use strict" ;
|
28 |
|
29 |
|
30 |
|
31 | const fromOutputSequence = require( './fromOutputSequence.js' ) ;
|
32 | const string = require( 'string-kit' ) ;
|
33 | const NextGenEvents = require( 'nextgen-events' ) ;
|
34 | const Promise = require( 'seventh' ) ;
|
35 |
|
36 |
|
37 |
|
38 | function SequencesReader( options = {} ) {
|
39 | }
|
40 |
|
41 | module.exports = SequencesReader ;
|
42 |
|
43 | SequencesReader.prototype = Object.create( NextGenEvents.prototype ) ;
|
44 | SequencesReader.prototype.constructor = SequencesReader ;
|
45 |
|
46 |
|
47 |
|
48 | async function readStream( stream , size = 1 ) {
|
49 | var data ;
|
50 |
|
51 |
|
52 | data = stream.read( size ) ;
|
53 |
|
54 |
|
55 | while ( data === null ) {
|
56 | await Promise.onceEventOrError( stream , 'readable' , [ 'close' , 'end' ] ) ;
|
57 | data = stream.read( size ) ;
|
58 |
|
59 | }
|
60 |
|
61 | return data ;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 | function charCodeIsAlpha( charCode ) {
|
67 | return ( charCode >= 0x41 && charCode <= 0x5a ) || ( charCode >= 0x61 && charCode <= 0x7a ) ;
|
68 | }
|
69 |
|
70 | function toCharCodeStr( charCode ) {
|
71 | var charCodeStr = charCode.toString( 16 ) ;
|
72 | charCodeStr = charCodeStr.length > 1 ? '\\x' + charCodeStr : '\\x0' + charCodeStr ;
|
73 | return charCodeStr ;
|
74 | }
|
75 |
|
76 | function toCharOrCharCodeStr( charCode ) {
|
77 | if ( charCode <= 0x1f || charCode === 0x7f ) { return toCharCodeStr( charCode ) ; }
|
78 | return String.fromCharCode( charCode ) ;
|
79 | }
|
80 |
|
81 | function defaultArgs( args , defaultArgs_ ) {
|
82 | if ( ! defaultArgs_ || ! defaultArgs_.length ) { return args ; }
|
83 |
|
84 | args = Array.from( args ) ;
|
85 |
|
86 | defaultArgs_.forEach( ( e , index ) => {
|
87 | if ( e !== undefined && args[ index ] === undefined ) {
|
88 | args[ index ] = e ;
|
89 | }
|
90 | } ) ;
|
91 |
|
92 | return args ;
|
93 | }
|
94 |
|
95 |
|
96 | const ESC_3_BYTES = new Set(
|
97 |
|
98 | '(' , ')' , '*' , '+' , '-' , '.' , '/' ,
|
99 |
|
100 | ' ' , '#' , '%'
|
101 | ) ;
|
102 |
|
103 |
|
104 | const CSI_TYPE_EXTRA_CHAR_BEFORE = new Set(
|
105 | '?' , '>' , '<'
|
106 | ) ;
|
107 |
|
108 |
|
109 | const CSI_TYPE_EXTRA_CHAR_AFTER = new Set(
|
110 | ' ' , '$' , '#' , '"' , "'" , '*'
|
111 | ) ;
|
112 |
|
113 |
|
114 | const OSC_TYPE_EXTRA_PARAM_AFTER = new Set(
|
115 | '?'
|
116 | ) ;
|
117 |
|
118 |
|
119 |
|
120 | SequencesReader.prototype.streamToEvent = async function( stream ) {
|
121 | var charCode , charCodeStr , char , bytes , codepoint ,
|
122 | type , args , argIndex , emitParsedSequence , event , subTree , subSubTree , basePtr ,
|
123 | charBuffer = Buffer.alloc( 6 ) ;
|
124 |
|
125 | for ( ;; ) {
|
126 | charCode = ( await readStream( stream ) )[ 0 ] ;
|
127 |
|
128 |
|
129 | if ( charCode <= 0x1f || charCode === 0x7f ) {
|
130 |
|
131 | charCodeStr = toCharCodeStr( charCode ) ;
|
132 |
|
133 |
|
134 | if ( charCode === 0x1b ) {
|
135 | charCode = ( await readStream( stream ) )[ 0 ] ;
|
136 |
|
137 | if ( charCode === 0x5b ) {
|
138 | args = '' ;
|
139 |
|
140 | while ( ! charCodeIsAlpha( charCode = ( await readStream( stream ) )[ 0 ] ) ) {
|
141 | args += String.fromCharCode( charCode ) ;
|
142 | }
|
143 |
|
144 | type = String.fromCharCode( charCode ) ;
|
145 | args = args ? args.split( ';' ) : [] ;
|
146 | emitParsedSequence = true ;
|
147 |
|
148 | if ( args.length && CSI_TYPE_EXTRA_CHAR_BEFORE.has( args[ 0 ][ 0 ] ) ) {
|
149 | type = args[ 0 ][ 0 ] + type ;
|
150 | args[ 0 ] = args[ 0 ].slice( 1 ) ;
|
151 | }
|
152 |
|
153 | if ( args.length && CSI_TYPE_EXTRA_CHAR_AFTER.has( args[ args.length - 1 ][ args[ args.length - 1 ] - 1 ] ) ) {
|
154 | type = args[ args.length - 1 ][ args[ args.length - 1 ] - 1 ] + type ;
|
155 | args[ args.length - 1 ] = args[ args.length - 1 ].slice( 0 , -1 ) ;
|
156 | }
|
157 |
|
158 | subTree = fromOutputSequence.CSI[ type ] ;
|
159 | console.error( ">>>>>>>>>> CSI parsing:" , type , args ) ;
|
160 |
|
161 | if ( subTree ) {
|
162 | if ( subTree.subTree && args.length ) {
|
163 | basePtr = subTree ;
|
164 |
|
165 | for ( argIndex = 0 ; argIndex < args.length ; argIndex ++ ) {
|
166 | subSubTree = basePtr.subTree[ args[ argIndex ] ] ;
|
167 |
|
168 | if ( subSubTree ) {
|
169 | if ( subSubTree.subTree ) {
|
170 | basePtr = subSubTree ;
|
171 | continue ;
|
172 | }
|
173 |
|
174 | event = subSubTree.event || basePtr.event || subTree.event ;
|
175 |
|
176 | if ( event !== 'none' ) {
|
177 | this.emit(
|
178 | event ,
|
179 | subSubTree.subType || basePtr.subType || subTree.subType ,
|
180 | subSubTree.arg !== undefined ? subSubTree.arg : ( basePtr.arg !== undefined ? basePtr.arg : subTree.arg ) ,
|
181 | subSubTree.extraArgs || basePtr.extraArgs || subTree.extraArgs || defaultArgs( args.slice( argIndex + 1 ) , subSubTree.defaultExtraArgs )
|
182 | ) ;
|
183 | }
|
184 |
|
185 | emitParsedSequence = false ;
|
186 |
|
187 | if ( subSubTree.continue ) {
|
188 | basePtr = subTree ;
|
189 | continue ;
|
190 | }
|
191 | }
|
192 | else {
|
193 | emitParsedSequence = true ;
|
194 | }
|
195 | break ;
|
196 | }
|
197 | }
|
198 | else if ( subTree.event ) {
|
199 | if ( subTree.event !== 'none' ) {
|
200 | this.emit(
|
201 | subTree.event ,
|
202 | subTree.subType ,
|
203 | subTree.arg ,
|
204 | subTree.extraArgs || defaultArgs( args , subTree.defaultExtraArgs )
|
205 | ) ;
|
206 | }
|
207 |
|
208 | emitParsedSequence = false ;
|
209 | }
|
210 | }
|
211 |
|
212 | if ( emitParsedSequence ) {
|
213 | this.emit( 'CSI' , type , args ) ;
|
214 | }
|
215 |
|
216 | continue ;
|
217 | }
|
218 | else if ( charCode === 0x5d ) {
|
219 | args = '' ;
|
220 |
|
221 | for ( ;; ) {
|
222 | charCode = ( await readStream( stream ) )[ 0 ] ;
|
223 |
|
224 | if ( charCode === 0x07 ) { break ; }
|
225 |
|
226 | if ( charCode === 0x1b ) {
|
227 | charCode = ( await readStream( stream ) )[ 0 ] ;
|
228 | if ( charCode === 0x5c ) { break ; }
|
229 |
|
230 | args += String.fromCharCode( 0x1b ) ;
|
231 | }
|
232 |
|
233 | args += String.fromCharCode( charCode ) ;
|
234 | }
|
235 |
|
236 | args = args ? args.split( ';' ) : [] ;
|
237 | type = args.shift() ;
|
238 |
|
239 | if ( OSC_TYPE_EXTRA_PARAM_AFTER.has( args[ args.length - 1 ] ) ) {
|
240 | type += args.pop() ;
|
241 | }
|
242 |
|
243 | emitParsedSequence = true ;
|
244 |
|
245 | subTree = fromOutputSequence.OSC[ type ] ;
|
246 |
|
247 | if ( subTree ) {
|
248 | if ( subTree.subTree ) {
|
249 | for ( argIndex = 0 ; argIndex < args.length ; argIndex ++ ) {
|
250 | subSubTree = subTree.subTree[ args[ argIndex ] ] ;
|
251 | if ( subSubTree ) {
|
252 | event = subSubTree.event || subTree.event ;
|
253 |
|
254 | if ( event !== 'none' ) {
|
255 | this.emit( event , subSubTree.subType || subTree.subType , args.slice( argIndex + 1 ) ) ;
|
256 | }
|
257 |
|
258 | emitParsedSequence = false ;
|
259 | }
|
260 | else {
|
261 | emitParsedSequence = true ;
|
262 | }
|
263 | break ;
|
264 | }
|
265 | }
|
266 | else if ( subTree.event ) {
|
267 | if ( subTree.event !== 'none' ) {
|
268 | this.emit( subTree.event , subTree.subType , args ) ;
|
269 | }
|
270 |
|
271 | emitParsedSequence = false ;
|
272 | }
|
273 | }
|
274 |
|
275 | if ( emitParsedSequence ) {
|
276 | this.emit( 'OSC' , type , args ) ;
|
277 | }
|
278 |
|
279 | continue ;
|
280 | }
|
281 | else {
|
282 |
|
283 | type = toCharOrCharCodeStr( charCode ) ;
|
284 | args = null ;
|
285 |
|
286 | if ( ESC_3_BYTES.has( type ) ) {
|
287 |
|
288 | args = [ toCharOrCharCodeStr( ( await readStream( stream ) )[ 0 ] ) ] ;
|
289 | }
|
290 |
|
291 | emitParsedSequence = true ;
|
292 | subTree = fromOutputSequence.ESC[ type ] ;
|
293 |
|
294 | if ( subTree ) {
|
295 | if ( subTree.event !== 'none' ) {
|
296 | this.emit( subTree.event , subTree.subType , subTree.arg , args || subTree.extraArgs ) ;
|
297 | }
|
298 |
|
299 | emitParsedSequence = false ;
|
300 | }
|
301 |
|
302 | if ( emitParsedSequence ) {
|
303 | this.emit( 'ESC' , type , args ) ;
|
304 | }
|
305 |
|
306 | continue ;
|
307 | }
|
308 | }
|
309 | else if ( fromOutputSequence.control[ charCodeStr ] ) {
|
310 | subTree = fromOutputSequence.control[ charCodeStr ] ;
|
311 |
|
312 | if ( subTree.event !== 'none' ) {
|
313 | this.emit( subTree.event , subTree.subType , subTree.arg , subTree.extraArgs ) ;
|
314 | }
|
315 |
|
316 | continue ;
|
317 | }
|
318 | else {
|
319 | this.emit( 'control' , charCodeStr ) ;
|
320 | continue ;
|
321 | }
|
322 | }
|
323 |
|
324 | if ( charCode >= 0x80 ) {
|
325 |
|
326 | if ( charCode < 0xc0 ) { continue ; }
|
327 | else if ( charCode < 0xe0 ) { bytes = 2 ; }
|
328 | else if ( charCode < 0xf0 ) { bytes = 3 ; }
|
329 | else if ( charCode < 0xf8 ) { bytes = 4 ; }
|
330 | else if ( charCode < 0xfc ) { bytes = 5 ; }
|
331 | else { bytes = 6 ; }
|
332 |
|
333 | charBuffer[ 0 ] = charCode ;
|
334 | charBuffer[ 1 ] = charBuffer[ 2 ] = charBuffer[ 3 ] = charBuffer[ 4 ] = charBuffer[ 5 ] = 0 ;
|
335 | ( await readStream( stream , bytes - 1 ) ).copy( charBuffer , 1 ) ;
|
336 |
|
337 | char = charBuffer.toString( 'utf8' ) ;
|
338 | codepoint = string.unicode.firstCodePoint( char ) ;
|
339 |
|
340 |
|
341 | this.emit( 'char' , char , codepoint ) ;
|
342 | }
|
343 | else {
|
344 |
|
345 | char = String.fromCharCode( charCode ) ;
|
346 |
|
347 | this.emit( 'char' , char , charCode ) ;
|
348 | }
|
349 | }
|
350 | } ;
|
351 |
|