-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
1111 lines (961 loc) · 28.9 KB
/
Copy pathindex.js
File metadata and controls
1111 lines (961 loc) · 28.9 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module tty-events
*/
const EventEmitter = require("events"),
{ StringDecoder } = require('string_decoder');
/**
*/
class KeyboardEvent {
/**
* @classdesc Represents a keyboard event (key or key combination).
* @hideconstructor
* @alias module:tty-events.KeyboardEvent
*/
constructor(
{
name,
sequence,
isSpecial = false,
ctrl = false,
alt = false,
shift = false
}
) {
if (typeof name !== "string")
throw new TypeError();
/**
* The key name (for special keys) or character produced by the key.
* @type {string}
*/
this.name = name;
/**
* The sequence produced by the key / key combination.
* @type {string}
*/
this.sequence = sequence;
/**
* Determines if the key is a special key. Special keys have names like `f2` or `backspace` or are a combination of Ctrl+symbol / Ctrl+letter.
* @type {boolean}
*/
this.isSpecial = isSpecial;
/**
* Determines if the Ctrl modifier was being pressed with the key. If the key is not a special key, this is always `false`.
* @type {boolean}
*/
this.ctrl = ctrl;
/**
* Determines if the Alt modifier was being pressed with the key.
* @type {boolean}
*/
this.alt = alt;
/**
* Determines if the Shift modifier was being pressed with the key. If the key is not a special key, this is always `false`.
* @type {boolean}
*/
this.shift = shift;
}
/**
* Determines if the Alt modifier was being pressed with the key. Present for compatibility with the `readline` module.
* @type {boolean}
*/
get meta() {
return this.alt;
}
set meta(value) {
this.alt = value;
}
/**
* Represents the key combination with a string in the format `["Ctrl+"]["Alt+"]["Shift+"]key.name`. For example: `"b"`, `"B"`, `"Ctrl+e"`, `"Ctrl+Shift+home"`, `"+"`.
*/
toString() {
return (this.ctrl? "Ctrl+":"") +
(this.alt? "Alt+":"") +
(this.shift? "Shift+":"") +
this.name
}
}
class MouseEvent {
/**
* @classdesc Represents a mouse event (click, wheel, etc.).
* @hideconstructor
* @alias module:tty-events.MouseEvent
*/
constructor(opts) {
Object.assign(this, opts)
}
}
/**
* The x coordinate of where the mouse event happened (1 = leftmost column).
*
* @name module:tty-events.MouseEvent#x
* @type {number}
*/
/**
* The y coordinate of where the mouse event happened (1 = topmost row).
*
* @name module:tty-events.MouseEvent#y
* @type {number}
*/
/**
* The button number, in the range 1-11. This property might be `undefined` for `mouseup` and `mousemove` events. If `undefined` in a `mousemove` event, no button was pressed when the cursor moved.
*
* List of mouse buttons (from {@link http://xahlee.info/linux/linux_x11_mouse_button_number.html}):
*
* - `1`: Left button
* - `2`: Middle (wheel) button
* - `3`: Right button
* - `4`: Rotate wheel up
* - `5`: Rotate wheel down
* - `6`: Push wheel left
* - `7`: Push wheel right
* - `8`: 4th button or XButton1 (browser back)
* - `9`: 5th button or XButton2 (browser forward)
*
* @name module:tty-events.MouseEvent#button
* @type {number?}
*/
/**
* Determines if the Ctrl modifier was being pressed when the mouse event occurred.
*
* @name module:tty-events.MouseEvent#ctrl
* @type {boolean}
*/
/**
* Determines if the Alt modifier was being pressed when the mouse event occurred.
*
* @name module:tty-events.MouseEvent#alt
* @type {boolean}
*/
/**
* Determines if the Shift modifier was being pressed when the mouse event occurred.
*
* @name module:tty-events.MouseEvent#shift
* @type {boolean}
*/
/**
* Type of mouse event (`mousedown`, `mouseup`, `mousemove` or `wheel`).
*
* @name module:tty-events.MouseEvent#type
* @type {string}
*/
/**
* Only for `wheel` events. Direction of the wheel turn (1 = down; -1 = up).
*
* @name module:tty-events.MouseEvent#direction
* @type {number?}
*/
/**
*/
class HighlightEvent {
/**
* @classdesc Represents a highlight selection.
* @hideconstructor
* @alias module:tty-events.HighlightEvent
*/
constructor(startX, startY ,endX, endY, mouseX, mouseY) {
/**
* The x coordinate of the first character of the selection.
* @type {number}
*/
this.startX = startX;
/**
* The y coordinate of the first character of the selection.
* @type {number}
*/
this.startY = startY;
/**
* The x coordinate of the first character after the selection.
* @type {number}
*/
this.endX = endX;
/**
* The y coordinate of the first character after the selection.
* @type {number}
*/
this.endY = endY;
/**
* The x coordinate of the mouse position.
* @type {number}
*/
this.mouseX = mouseX;
/**
* The y coordinate of the mouse position.
* @type {number}
*/
this.mouseY = mouseY;
}
}
/*******************************************************************************\
| Below is modified code from the node repository. |
| <https://github.com/nodejs/node/blob/master/lib/internal/readline/utils.js> |
| |
\*******************************************************************************/
/*
Some patterns seen in terminal key escape codes, derived from combos seen
at http://www.midnight-commander.org/browser/lib/tty/key.c
ESC letter
ESC [ letter
ESC [ modifier letter
ESC [ 1 ; modifier letter
ESC [ num char
ESC [ num ; modifier char
ESC O letter
ESC O modifier letter
ESC O 1 ; modifier letter
ESC N letter
ESC [ [ num ; modifier char
ESC [ [ 1 ; modifier letter
ESC ESC [ num char
ESC ESC O letter
- char is usually ~ but $ and ^ also happen with rxvt
- modifier is 1 +
(shift * 1) +
(left_alt * 2) +
(ctrl * 4) +
(right_alt * 8)
- two leading ESCs (~~apparently mean the same as one leading ESC~~) mean that the Alt key was being pressed.
*/
/**
* A generator function that accepts characters from the input stream as inputs to `iterator.next()` and emits the events.
*
* @param {module:tty-input} term
* @private
*/
function* emitKeys(term) {
/**
* @param {number} b The encoded button value
* @param {number} x The x coordinate of the event
* @param {number} y The y coordinate of the event
* @param {boolean} mouseUp If it is a mouseup event (used by SGR).
*/
function parseAndEmitMouse(b, x, y, mouseUp = false) {
if (b<0 || x<1 || y<1 || isNaN(b) || isNaN(x) || isNaN(y)) // Sometimes, rxvt sends mouse sequences with negative coordinates.
return;
/* Quotes (indicated by "//>") are from <https://invisible-island.net/xterm/ctlseqs/ctlseqs.pdf> */
let button, evType, evOpts = {x, y};
//> The low two bits of Cb encode button information: 0=MB1 pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release.
button = b & 3;
//> Additional buttons are encoded like the wheel mice,
//> • by adding 64 (for buttons 4 through 7), or
//> • by adding 128 (for buttons 8 through 11).
if (b & 64 || b & 128) {
if (b & 64)
button |= 4;
if (b & 128)
button |= 8;
} else if (button === 3)
button = undefined;
else
button += 1; // Only increment button if in the range 0-2.
evOpts.button = button;
//> On button-motion events, xterm adds 32 to the event code (the third character, Cb).
if (b & 32) {
evType = "mousemove";
} else if (button === undefined || mouseUp) {
evType = "mouseup";
} else if (button === 4) {
evType = "wheel";
evOpts.direction = -1;
} else if (button === 5) {
evType = "wheel";
evOpts.direction = 1;
} else {
evType = "mousedown";
}
//> The next three bits encode the modifiers which were down when the button was pressed
//> and are added together: 4=Shift, 8=Alt, 16=Control.
//> Note however that the shift and control bits are normally unavailable because xterm uses the control modifier
//> with mouse for popup menus, and the shift modifier is used in the default translations for button events.
evOpts.shift = Boolean(b & 4);
evOpts.alt = Boolean(b & 8);
evOpts.ctrl = Boolean(b & 16);
evOpts.type = evType;
term.emit(evType, new MouseEvent(evOpts));
term.emit("mouse", new MouseEvent(evOpts));
}
function parseAndEmitHighlight(args) {
var startX, startY ,endX, endY, mouseX, mouseY;
if (args.length === 6) // ESC [ T ... *or* ESC [ < ... T
// Apparently this happens if either the start position changed (backwards selection) or
// if the mouse position is not the same as the end position (new line).
[startX, startY ,endX, endY, mouseX, mouseY] = args;
else { // ESC [ t ... *or* ESC [ < ... t
endX = mouseX = args[0];
endY = mouseY = args[1];
}
term.emit("highlight", new HighlightEvent(startX, startY ,endX, endY, mouseX, mouseY))
}
function emitEsc() {
term.emit("keypress", new KeyboardEvent({name: "escape", sequence: "\x1b", isSpecial: true}))
}
/**
* Was the last char a carriage return?
*/
var lastCharWasCR = false,
/**
* The last character to be read.
*/
ch = yield,
/**
* The sequence.
*/
s;
main: while (true) {
if (ch === "") {
ch = yield;
continue;
}
s = ch;
const key = {
name: undefined,
sequence: undefined,
isSpecial: true,
ctrl: false,
alt: false,
shift: false
};
let escaped = 0;
if (ch === "\x1b") { // ESC
escaped = 1;
s += (ch = yield);
if (ch === "\x1b") { // ESC ESC
escaped = 2;
s += (ch = yield);
}
if (ch === "" || (s.length > 1 && ch === "\x1b")) { // ESC [ESC] timeout *or* ESC ESC ESC
term.emit("keypress", new KeyboardEvent({
name: "escape",
sequence: s,
isSpecial: true
}))
ch = yield;
if (ch === "") { // ESC ESC ESC timeout
ch = yield;
}
continue;
}
}
if (escaped && (ch === 'O' || ch === '[')) {
// Escape sequence
/**
* The processed code (without modifier).
*/
let code = ch;
let modifier = 0;
if (ch === 'O') {
// SS3 sequences:
//
// ESC O letter
// ESC O modifier letter
s += (ch = yield);
if (ch >= '0' && ch <= '9') {
modifier = (ch >> 0) - 1;
s += (ch = yield);
} else if (ch === "") { // Alt+O
term.emit("keypress", new KeyboardEvent({
name: "O",
sequence: s,
alt: true
}));
ch = yield;
continue main;
}
code += ch;
} else if (ch === '[') {
// CSI sequences:
//
// ESC [ letter
// ESC [ modifier letter
// ESC [ [ modifier letter
// ESC [ [ num char
const seqStart = s.length;
s += (ch = yield);
if (ch === "") { // Alt+[
term.emit("keypress", new KeyboardEvent({
name: "[",
sequence: s,
alt: true
}));
ch = yield;
continue main;
}
if (ch === "M") { // MOUSE
const args = [];
for (let i=0; i<3; i++) {
let byte = yield true;
if (byte === "") {
term.emit("unknownSequence", s);
ch = yield;
continue main;
}
args.push(byte - 0x20)
}
parseAndEmitMouse(...args);
ch = yield;
continue;
} else if (ch === "T" || ch === "t") { // MOUSE HIGHLIGHT TRACKING
const mode = ch, argsLength = mode==="T"? 6:2;
const args = [];
for (let i=0; i<argsLength; i++) {
let byte = yield true;
if (byte === "") {
term.emit("unknownSequence", s);
ch = yield;
continue main;
}
args.push(byte - 0x20)
}
parseAndEmitHighlight(args);
ch = yield;
continue;
}
if (ch === '[') {
// \x1b[[A
// ^--- escape codes might have a second bracket
code += ch;
s += (ch = yield);
}
/*
* Here and later we try to buffer just enough data to get
* a complete ascii sequence.
*
* We have basically two classes of ascii characters to process:
*
*
* 1. `\x1b[24;5~` should be parsed as { code: '[24~', modifier: 5 }
*
* This particular example is featuring Ctrl+F12 in xterm.
*
* - `;5` part is optional, e.g. it could be `\x1b[24~`
* - first part can contain one or two digits
*
* So the generic regexp is like /^\d\d?(;\d)?[~^$]$/
*
*
* 2. `\x1b[1;5H` should be parsed as { code: '[H', modifier: 5 }
*
* This particular example is featuring Ctrl+Home in xterm.
*
* - `1;5` part is optional, e.g. it could be `\x1b[H`
* - `1;` part is optional, e.g. it could be `\x1b[5H`
*
* So the generic regexp is like /^((\d;)?\d)?[A-Za-z]$/
*
*/
const cmdStart = s.length - 1;
/*
* Here we buffer the sequence into `s`.
*
* CSI sequences can only contain bytes in the range 0x20-0x3f,
* and end with a byte in the range 0x40-0x7e.
*/
while (true) {
const charCode = ch.charCodeAt(0);
if ((charCode >= 0x40 && charCode <= 0x7e) || charCode === 0x24) { // Not in the spec, but $ can end a sequence.
// End of sequence
break;
} else if (charCode >= 0x20 && charCode <= 0x3f) {
s += (ch = yield);
} else {
// The sequence is broken.
term.emit("unknownSequence", s);
continue main;
}
}
// We buffered enough data.
const seq = s.slice(seqStart);
// CSI sequences might not represent a key.
if (
(seq[0] === "<" && (ch==="M" || ch==="m")) ||
(seq[0] === "<" && (ch==="T" || ch==="t")) ||
ch === "I" ||
ch === "O" ||
seq === "200~"
) {
/* These sequences should not be preceded with an extra ESC.
* If that happens, it's probably because the Esc key was pressed.
*/
if (escaped >= 2)
emitEsc();
if (ch==="M" || ch==="m" || ch==="T" || ch==="t") {
/**
* - `true`: SGR extended mouse;
* - `false`: Highlight tracking.
*/
const mouse = ch==="M" || ch==="m",
args = seq.slice(1, seq.length-1).split(";");
let err = args.length !== (mouse? 3:(ch === "t"? 2:6));
if (!err) {
for (let i=0; i<args.length; i++) {
const n = parseInt(args[i]);
if (isNaN(n)) {
err = true;
break;
}
args[i] = n;
};
}
if (err) {
term.emit("unknownSequence", s);
} else {
if (mouse) { // SGR MOUSE
parseAndEmitMouse(
...args,
ch === "m"
);
} else /* if (ch==="T" || ch==="t") */ { // HIGHLIGHT TRACKING
parseAndEmitHighlight(args);
}
}
} else if (ch === "I") { // FOCUS IN
term.emit("focusin");
} else if (ch === "O") { // FOCUS OUT
term.emit("focusout");
} else if (seq === "200~") { // BRACKETED PASTE MODE
// Loop until an ESC [ 2 0 1 ~ is received.
const endSeq = "\x1b[201~";
let str = "", endSeqIndex = 0;
while (true) {
str += (ch = yield);
if (ch === endSeq[endSeqIndex])
endSeqIndex++;
else
endSeqIndex = 0;
if (endSeqIndex >= endSeq.length) {
// An ESC [ 2 0 1 ~ was received.
term.emit("paste", str.slice(0, str.length - endSeq.length));
break;
}
}
}
ch = yield;
continue;
}
// Now trying to extract code and modifier from it.
const cmd = s.slice(cmdStart);
let match;
if ((match = cmd.match(/^(\d\d?)(;(\d))?([~^$])$/))) {
code += match[1] + match[4];
modifier = (match[3] || 1) - 1;
} else if ((match = cmd.match(/^((\d;)?(\d))?([A-Za-z])$/))) {
code += match[4];
modifier = (match[3] || 1) - 1;
} else {
code += cmd;
}
/* The ending char of the code indicates modifiers in some terminals.
* Here, the ending char is parsed and replaced by `~`.
*/
switch (code[code.length - 1]) {
case "$":
key.shift = true;
code = code.slice(0, code.length-1)+"~";
break;
case "^":
key.ctrl = true;
code = code.slice(0, code.length-1)+"~";
break;
case "@":
key.shift = key.ctrl = true;
code = code.slice(0, code.length-1)+"~";
break;
}
}
// Parse the key modifier
key.ctrl = key.ctrl || Boolean(modifier & 4);
key.alt = Boolean(modifier & 10);
key.shift = key.shift || Boolean(modifier & 1);
if (escaped >= 2) {
// Double-escaped sequences usually mean that the Alt key was being pressed.
key.alt = true;
}
// Parse the key code
if (code.match(/^(O|\[)[A-Z]$/)) {
// ESC O letter and ESC [ letter are equivalent.
switch (code[1]) {
/* xterm/gnome */
case 'A': key.name = 'up'; break;
case 'B': key.name = 'down'; break;
case 'C': key.name = 'right'; break;
case 'D': key.name = 'left'; break;
case 'E': key.name = 'clear'; break;
case 'F': key.name = 'end'; break;
case 'H': key.name = 'home'; break;
case 'P': key.name = 'f1'; break;
case 'Q': key.name = 'f2'; break;
case 'R': key.name = 'f3'; break;
case 'S': key.name = 'f4'; break;
}
} else {
switch (code) {
/* xterm/rxvt ESC [ number ~ */
case '[11~': key.name = 'f1'; break;
case '[12~': key.name = 'f2'; break;
case '[13~': key.name = 'f3'; break;
case '[14~': key.name = 'f4'; break;
/* from Cygwin and used in libuv */
case '[[A': key.name = 'f1'; break;
case '[[B': key.name = 'f2'; break;
case '[[C': key.name = 'f3'; break;
case '[[D': key.name = 'f4'; break;
case '[[E': key.name = 'f5'; break;
/* common */
case '[15~': key.name = 'f5'; break;
case '[17~': key.name = 'f6'; break;
case '[18~': key.name = 'f7'; break;
case '[19~': key.name = 'f8'; break;
case '[20~': key.name = 'f9'; break;
case '[21~': key.name = 'f10'; break;
case '[23~': key.name = 'f11'; break;
case '[24~': key.name = 'f12'; break;
/* Extended function keys */
case '[25~': key.name = 'f3'; key.shift = true; break;
case '[26~': key.name = 'f4'; key.shift = true; break;
case '[28~': key.name = 'f5'; key.shift = true; break;
case '[29~': key.name = 'f6'; key.shift = true; break;
case '[31~': key.name = 'f7'; key.shift = true; break;
case '[32~': key.name = 'f8'; key.shift = true; break;
case '[33~': key.name = 'f9'; key.shift = true; break;
case '[34~': key.name = 'f10'; key.shift = true; break;
/* xterm/rxvt ESC [ number ~ */
case '[1~': key.name = 'home'; break;
case '[2~': key.name = 'insert'; break;
case '[3~': key.name = 'delete'; break;
case '[4~': key.name = 'end'; break;
case '[5~': key.name = 'pageup'; break;
case '[6~': key.name = 'pagedown'; break;
/* putty */
case '[[5~': key.name = 'pageup'; break;
case '[[6~': key.name = 'pagedown'; break;
/* rxvt */
case '[7~': key.name = 'home'; break;
case '[8~': key.name = 'end'; break;
/* rxvt keys with modifiers */
case '[a': key.name = 'up'; key.shift = true; break;
case '[b': key.name = 'down'; key.shift = true; break;
case '[c': key.name = 'right'; key.shift = true; break;
case '[d': key.name = 'left'; key.shift = true; break;
case '[e': key.name = 'clear'; key.shift = true; break;
case 'Oa': key.name = 'up'; key.ctrl = true; break;
case 'Ob': key.name = 'down'; key.ctrl = true; break;
case 'Oc': key.name = 'right'; key.ctrl = true; break;
case 'Od': key.name = 'left'; key.ctrl = true; break;
case 'Oe': key.name = 'clear'; key.ctrl = true; break;
/* misc. */
case '[Z': key.name = 'tab'; key.shift = true; break;
// default: key.name = undefined; break;
}
}
key.sequence = s;
if (key.name !== undefined) {
term.emit("keypress", new KeyboardEvent(key))
} else {
/* Unrecognized escape sequence */
term.emit("unknownSequence", s)
}
ch = yield;
continue;
} else {
// Key is not an escape sequence, but might be escaped.
if (escaped >= 2)
emitEsc();
key.alt = Boolean(escaped); // In most terminals, typing a character while holding Alt precedes the char with an ESC.
key.sequence = s;
if (ch === '\r') {
// Enter
key.name = 'enter';
} else if (ch === '\n') {
if (lastCharWasCR) { // Ignore \n in \r\n.
ch = yield;
continue;
}
// \n without \r
key.name = 'enter';
} else if (ch === '\t') {
// Tab
key.name = 'tab';
} else if (ch === '\b' || ch === '\x7f') {
// Backspace or Ctrl+h
key.name = 'backspace';
} else if (ch === ' ') {
key.name = 'space';
} else if (ch <= '\x1e') {
// ^@, ^A-Z, ^\, ^], ^^
// Note: ^@ can also be obtained using Ctrl+␣ in most terminals.
key.name = String.fromCharCode(ch.charCodeAt(0) + 0x40).toLowerCase();
key.ctrl = true;
} else if (ch === '\x1f') {
// In most terminals ^_ is obtained by Ctrl+-.
key.name = "-";
key.ctrl = true;
} else {
// Character (letter, number, symbol, etc...).
key.isSpecial = false;
key.name = ch;
key.sequence = (escaped? "\x1b":"") + key.name;
}
}
term.emit("keypress", new KeyboardEvent(key))
lastCharWasCR = ch === "\r";
ch = yield;
}
}
/**************************************************\
| End of modified code from the node repository. |
| |
\**************************************************/
/**
* @typedef TermOptions
* @property {number} timeout=500 The escape sequence timeout, in milliseconds. `tty-events` will stop waiting for the rest of an escape sequence when the timeout fires. `Infinity` = no timeout.
* @property {string} encoding=utf-8 The encoding of the input stream.
*/
/**
* Emits terminal-related events.
*
* @alias module:tty-events
*/
class Terminal extends EventEmitter {
/**
* @param {ReadableStream} input The input stream. (Normally stdin.)
* @param {WritableStream} output The output stream for activating mouse support and bracketed paste mode. (Normally stdout.) Optional.
* @param {TermOptions} options
*/
constructor(input = process.stdin, output, {
escKeyTimeout = 500,
timeout = escKeyTimeout,
encoding
} = {}) {
super()
const stringDecoder = new StringDecoder(encoding),
iterator = emitKeys(this);
/**
* The value returned by `iterator.next()`. (`false`: iterator expects a char; `true`: iterator expects a byte.)
*/
var sendByte = iterator.next().value
this._dataListener = (buf) => {
for (let i=0; i<buf.length; i++) {
if (sendByte)
sendByte = iterator.next(buf[i]).value;
else {
// Decode a character
let str = stringDecoder.write(Buffer.from([buf[i]]));
// Send the character(s) to the generator function.
if (str) for (let char of str) {
if (iterator.next(char).value) { // Break when a byte is expected, discarding the rest of the string (this should never happen).
sendByte = true;
break;
}
}
}
}
// Escape Timeout
//
// Because the escape key sends ESC, there's no way to distinguish it form
// the start of an escape sequence.
if (this._timeoutID) {
clearTimeout(this._timeoutID)
}
if (this.timeout !== Infinity) {
this._timeoutID = setTimeout(()=>{
this._timeoutID = undefined;
sendByte = iterator.next("").value; // When the timeout fires, `undefined` is sent to the generator function.
}, this.timeout);
}
}
// When the stream closes, clear the string decoder's internal buffer and process any incomplete code points.
this._endListener = ()=>{
if (!sendByte) {
let str = stringDecoder.end();
// Send the character(s) to the generator function.
if (str) for (let char of str) {
if (iterator.next(char).value) {
sendByte = true;
break;
}
}
}
}
this._input = input;
this.output = output || (input === process.stdin? process.stdout:null);
this._encoding = encoding;
this.timeout = timeout;
this._timeoutID = undefined;
this._isPaused = true;
this.resume();
}
/**
* Removes the `data` listener from the input stream.
* @param {boolean} pauseStream Whether to pause the input stream. This will allow Node.js to exit.
*/
pause(pauseStream = true) {
if (!this._isPaused) {
this._input.removeListener("data", this._dataListener);
this._input.removeListener("end", this._endListener);
if (pauseStream && this._input.pause)
this._input.pause();
this._isPaused = true;
return true;
} else
return false;
}
/**
* Attaches the `data` listener to the input stream.
* @param {boolean} resumeStream Determines if the underlying input stream is also resumed.
*/
resume(resumeStream = true) {
if (this._isPaused) {
this._input.on("data", this._dataListener);
this._input.on("end", this._endListener);
if (resumeStream && this._input.resume)
this._input.resume();
this._isPaused = false;
return true;
} else
return false;
}
/**
* Enables mouse events.
* @param {number} mode The mouse mode (one of the constants)
* @param {boolean} sgr Whether to try to activate SGR extended mode
*/
enableMouse(mode = 0, sgr = true) {
// If a terminal doesn't support a certain mode, try to enable the most complete one.
this.output.write(`\x1b[?1000h`);
if (mode === 1)
this.output.write(`\x1b[?1001h`);
else {
if (mode >= 2)
this.output.write(`\x1b[?1002h`);
if (mode === 3)
this.output.write(`\x1b[?1003h`);
}
if (sgr)
this.output.write(`\x1b[?1006h`);
}
/**
* Disables mouse events.
*/
disableMouse() {
this.output.write(`\x1b[?1000l\x1b[?1006l`);
}
/**
* Enables bracketed paste mode.
*/
enableBPM() {
this.output.write("\x1b[?2004h")
}
/**
* Disables bracketed paste mode.
*/
disableBPM() {
this.output.write("\x1b[?2004l")
}
/**