-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathghostty-web.js
More file actions
3508 lines (3507 loc) · 136 KB
/
Copy pathghostty-web.js
File metadata and controls
3508 lines (3507 loc) · 136 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
var N = /* @__PURE__ */ ((i) => (i[i.CURSOR_KEY_APPLICATION = 0] = "CURSOR_KEY_APPLICATION", i[i.KEYPAD_KEY_APPLICATION = 1] = "KEYPAD_KEY_APPLICATION", i[i.IGNORE_KEYPAD_WITH_NUMLOCK = 2] = "IGNORE_KEYPAD_WITH_NUMLOCK", i[i.ALT_ESC_PREFIX = 3] = "ALT_ESC_PREFIX", i[i.MODIFY_OTHER_KEYS_STATE_2 = 4] = "MODIFY_OTHER_KEYS_STATE_2", i[i.KITTY_KEYBOARD_FLAGS = 5] = "KITTY_KEYBOARD_FLAGS", i))(N || {}), G = /* @__PURE__ */ ((i) => (i[i.RELEASE = 0] = "RELEASE", i[i.PRESS = 1] = "PRESS", i[i.REPEAT = 2] = "REPEAT", i))(G || {}), h = /* @__PURE__ */ ((i) => (i[i.UNIDENTIFIED = 0] = "UNIDENTIFIED", i[i.GRAVE = 1] = "GRAVE", i[i.BACKSLASH = 2] = "BACKSLASH", i[i.BRACKET_LEFT = 3] = "BRACKET_LEFT", i[i.BRACKET_RIGHT = 4] = "BRACKET_RIGHT", i[i.COMMA = 5] = "COMMA", i[i.ZERO = 6] = "ZERO", i[i.ONE = 7] = "ONE", i[i.TWO = 8] = "TWO", i[i.THREE = 9] = "THREE", i[i.FOUR = 10] = "FOUR", i[i.FIVE = 11] = "FIVE", i[i.SIX = 12] = "SIX", i[i.SEVEN = 13] = "SEVEN", i[i.EIGHT = 14] = "EIGHT", i[i.NINE = 15] = "NINE", i[i.EQUAL = 16] = "EQUAL", i[i.INTL_BACKSLASH = 17] = "INTL_BACKSLASH", i[i.INTL_RO = 18] = "INTL_RO", i[i.INTL_YEN = 19] = "INTL_YEN", i[i.A = 20] = "A", i[i.B = 21] = "B", i[i.C = 22] = "C", i[i.D = 23] = "D", i[i.E = 24] = "E", i[i.F = 25] = "F", i[i.G = 26] = "G", i[i.H = 27] = "H", i[i.I = 28] = "I", i[i.J = 29] = "J", i[i.K = 30] = "K", i[i.L = 31] = "L", i[i.M = 32] = "M", i[i.N = 33] = "N", i[i.O = 34] = "O", i[i.P = 35] = "P", i[i.Q = 36] = "Q", i[i.R = 37] = "R", i[i.S = 38] = "S", i[i.T = 39] = "T", i[i.U = 40] = "U", i[i.V = 41] = "V", i[i.W = 42] = "W", i[i.X = 43] = "X", i[i.Y = 44] = "Y", i[i.Z = 45] = "Z", i[i.MINUS = 46] = "MINUS", i[i.PERIOD = 47] = "PERIOD", i[i.QUOTE = 48] = "QUOTE", i[i.SEMICOLON = 49] = "SEMICOLON", i[i.SLASH = 50] = "SLASH", i[i.ALT_LEFT = 51] = "ALT_LEFT", i[i.ALT_RIGHT = 52] = "ALT_RIGHT", i[i.BACKSPACE = 53] = "BACKSPACE", i[i.CAPS_LOCK = 54] = "CAPS_LOCK", i[i.CONTEXT_MENU = 55] = "CONTEXT_MENU", i[i.CONTROL_LEFT = 56] = "CONTROL_LEFT", i[i.CONTROL_RIGHT = 57] = "CONTROL_RIGHT", i[i.ENTER = 58] = "ENTER", i[i.META_LEFT = 59] = "META_LEFT", i[i.META_RIGHT = 60] = "META_RIGHT", i[i.SHIFT_LEFT = 61] = "SHIFT_LEFT", i[i.SHIFT_RIGHT = 62] = "SHIFT_RIGHT", i[i.SPACE = 63] = "SPACE", i[i.TAB = 64] = "TAB", i[i.CONVERT = 65] = "CONVERT", i[i.KANA_MODE = 66] = "KANA_MODE", i[i.NON_CONVERT = 67] = "NON_CONVERT", i[i.DELETE = 68] = "DELETE", i[i.END = 69] = "END", i[i.HELP = 70] = "HELP", i[i.HOME = 71] = "HOME", i[i.INSERT = 72] = "INSERT", i[i.PAGE_DOWN = 73] = "PAGE_DOWN", i[i.PAGE_UP = 74] = "PAGE_UP", i[i.DOWN = 75] = "DOWN", i[i.LEFT = 76] = "LEFT", i[i.RIGHT = 77] = "RIGHT", i[i.UP = 78] = "UP", i[i.NUM_LOCK = 79] = "NUM_LOCK", i[i.KP_0 = 80] = "KP_0", i[i.KP_1 = 81] = "KP_1", i[i.KP_2 = 82] = "KP_2", i[i.KP_3 = 83] = "KP_3", i[i.KP_4 = 84] = "KP_4", i[i.KP_5 = 85] = "KP_5", i[i.KP_6 = 86] = "KP_6", i[i.KP_7 = 87] = "KP_7", i[i.KP_8 = 88] = "KP_8", i[i.KP_9 = 89] = "KP_9", i[i.KP_PLUS = 90] = "KP_PLUS", i[i.KP_BACKSPACE = 91] = "KP_BACKSPACE", i[i.KP_CLEAR = 92] = "KP_CLEAR", i[i.KP_CLEAR_ENTRY = 93] = "KP_CLEAR_ENTRY", i[i.KP_COMMA = 94] = "KP_COMMA", i[i.KP_PERIOD = 95] = "KP_PERIOD", i[i.KP_DIVIDE = 96] = "KP_DIVIDE", i[i.KP_ENTER = 97] = "KP_ENTER", i[i.KP_EQUAL = 98] = "KP_EQUAL", i[i.KP_MEMORY_ADD = 99] = "KP_MEMORY_ADD", i[i.KP_MEMORY_CLEAR = 100] = "KP_MEMORY_CLEAR", i[i.KP_MEMORY_RECALL = 101] = "KP_MEMORY_RECALL", i[i.KP_MEMORY_STORE = 102] = "KP_MEMORY_STORE", i[i.KP_MEMORY_SUBTRACT = 103] = "KP_MEMORY_SUBTRACT", i[i.KP_MULTIPLY = 104] = "KP_MULTIPLY", i[i.KP_PAREN_LEFT = 105] = "KP_PAREN_LEFT", i[i.KP_PAREN_RIGHT = 106] = "KP_PAREN_RIGHT", i[i.KP_MINUS = 107] = "KP_MINUS", i[i.KP_SEPARATOR = 108] = "KP_SEPARATOR", i[i.NUMPAD_UP = 109] = "NUMPAD_UP", i[i.NUMPAD_DOWN = 110] = "NUMPAD_DOWN", i[i.NUMPAD_RIGHT = 111] = "NUMPAD_RIGHT", i[i.NUMPAD_LEFT = 112] = "NUMPAD_LEFT", i[i.NUMPAD_BEGIN = 113] = "NUMPAD_BEGIN", i[i.NUMPAD_HOME = 114] = "NUMPAD_HOME", i[i.NUMPAD_END = 115] = "NUMPAD_END", i[i.NUMPAD_INSERT = 116] = "NUMPAD_INSERT", i[i.NUMPAD_DELETE = 117] = "NUMPAD_DELETE", i[i.NUMPAD_PAGE_UP = 118] = "NUMPAD_PAGE_UP", i[i.NUMPAD_PAGE_DOWN = 119] = "NUMPAD_PAGE_DOWN", i[i.ESCAPE = 120] = "ESCAPE", i[i.F1 = 121] = "F1", i[i.F2 = 122] = "F2", i[i.F3 = 123] = "F3", i[i.F4 = 124] = "F4", i[i.F5 = 125] = "F5", i[i.F6 = 126] = "F6", i[i.F7 = 127] = "F7", i[i.F8 = 128] = "F8", i[i.F9 = 129] = "F9", i[i.F10 = 130] = "F10", i[i.F11 = 131] = "F11", i[i.F12 = 132] = "F12", i[i.F13 = 133] = "F13", i[i.F14 = 134] = "F14", i[i.F15 = 135] = "F15", i[i.F16 = 136] = "F16", i[i.F17 = 137] = "F17", i[i.F18 = 138] = "F18", i[i.F19 = 139] = "F19", i[i.F20 = 140] = "F20", i[i.F21 = 141] = "F21", i[i.F22 = 142] = "F22", i[i.F23 = 143] = "F23", i[i.F24 = 144] = "F24", i[i.F25 = 145] = "F25", i[i.FN_LOCK = 146] = "FN_LOCK", i[i.PRINT_SCREEN = 147] = "PRINT_SCREEN", i[i.SCROLL_LOCK = 148] = "SCROLL_LOCK", i[i.PAUSE = 149] = "PAUSE", i[i.BROWSER_BACK = 150] = "BROWSER_BACK", i[i.BROWSER_FAVORITES = 151] = "BROWSER_FAVORITES", i[i.BROWSER_FORWARD = 152] = "BROWSER_FORWARD", i[i.BROWSER_HOME = 153] = "BROWSER_HOME", i[i.BROWSER_REFRESH = 154] = "BROWSER_REFRESH", i[i.BROWSER_SEARCH = 155] = "BROWSER_SEARCH", i[i.BROWSER_STOP = 156] = "BROWSER_STOP", i[i.EJECT = 157] = "EJECT", i[i.LAUNCH_APP_1 = 158] = "LAUNCH_APP_1", i[i.LAUNCH_APP_2 = 159] = "LAUNCH_APP_2", i[i.LAUNCH_MAIL = 160] = "LAUNCH_MAIL", i[i.MEDIA_PLAY_PAUSE = 161] = "MEDIA_PLAY_PAUSE", i[i.MEDIA_SELECT = 162] = "MEDIA_SELECT", i[i.MEDIA_STOP = 163] = "MEDIA_STOP", i[i.MEDIA_TRACK_NEXT = 164] = "MEDIA_TRACK_NEXT", i[i.MEDIA_TRACK_PREVIOUS = 165] = "MEDIA_TRACK_PREVIOUS", i[i.POWER = 166] = "POWER", i[i.SLEEP = 167] = "SLEEP", i[i.AUDIO_VOLUME_DOWN = 168] = "AUDIO_VOLUME_DOWN", i[i.AUDIO_VOLUME_MUTE = 169] = "AUDIO_VOLUME_MUTE", i[i.AUDIO_VOLUME_UP = 170] = "AUDIO_VOLUME_UP", i[i.WAKE_UP = 171] = "WAKE_UP", i[i.COPY = 172] = "COPY", i[i.CUT = 173] = "CUT", i[i.PASTE = 174] = "PASTE", i))(h || {}), C = /* @__PURE__ */ ((i) => (i[i.NONE = 0] = "NONE", i[i.SHIFT = 1] = "SHIFT", i[i.CTRL = 2] = "CTRL", i[i.ALT = 4] = "ALT", i[i.SUPER = 8] = "SUPER", i[i.CAPSLOCK = 16] = "CAPSLOCK", i[i.NUMLOCK = 32] = "NUMLOCK", i))(C || {}), I = /* @__PURE__ */ ((i) => (i[i.NONE = 0] = "NONE", i[i.PARTIAL = 1] = "PARTIAL", i[i.FULL = 2] = "FULL", i))(I || {});
const Y = 80;
var E = /* @__PURE__ */ ((i) => (i[i.BOLD = 1] = "BOLD", i[i.ITALIC = 2] = "ITALIC", i[i.UNDERLINE = 4] = "UNDERLINE", i[i.STRIKETHROUGH = 8] = "STRIKETHROUGH", i[i.INVERSE = 16] = "INVERSE", i[i.INVISIBLE = 32] = "INVISIBLE", i[i.BLINK = 64] = "BLINK", i[i.FAINT = 128] = "FAINT", i))(E || {});
class k {
constructor(t) {
this.exports = t.exports, this.memory = this.exports.memory;
}
createKeyEncoder() {
return new q(this.exports);
}
createTerminal(t = 80, e = 24, s) {
return new Z(this.exports, this.memory, t, e, s);
}
static async load(t) {
if (t)
return k.loadFromPath(t);
const e = new URL("../ghostty-vt.wasm", self.location), s = [];
if (e.protocol === "file:") {
let o = e.pathname;
o.match(/^\/[A-Za-z]:\//) && (o = o.slice(1)), s.push(o);
}
s.push(e.href, "./ghostty-vt.wasm", "/ghostty-vt.wasm");
let r = null;
for (const o of s)
try {
return await k.loadFromPath(o);
} catch (n) {
r = n instanceof Error ? n : new Error(String(n));
}
throw r || new Error("Failed to load Ghostty WASM");
}
static async loadFromPath(t) {
let e;
if (typeof Bun < "u" && typeof Bun.file == "function")
try {
const o = Bun.file(t);
await o.exists() && (e = await o.arrayBuffer());
} catch {
}
if (!e)
try {
const n = await (await import("./__vite-browser-external-2447137e.js")).readFile(t);
e = n.buffer.slice(n.byteOffset, n.byteOffset + n.byteLength);
} catch {
}
if (!e) {
const o = await fetch(t);
if (!o.ok)
throw new Error(`Failed to fetch WASM: ${o.status} ${o.statusText}`);
if (e = await o.arrayBuffer(), e.byteLength === 0)
throw new Error(`WASM file is empty (0 bytes). Check path: ${t}`);
}
if (!e)
throw new Error(`Could not load WASM from path: ${t}`);
const s = await WebAssembly.compile(e), r = await WebAssembly.instantiate(s, {
env: {
log: (o, n) => {
const a = new Uint8Array(
r.exports.memory.buffer,
o,
n
);
console.log("[ghostty-vt]", new TextDecoder().decode(a));
}
}
});
return new k(r);
}
}
class q {
constructor(t) {
this.encoder = 0, this.exports = t;
const e = this.exports.ghostty_wasm_alloc_opaque(), s = this.exports.ghostty_key_encoder_new(0, e);
if (s !== 0)
throw new Error(`Failed to create key encoder: ${s}`);
const r = new DataView(this.exports.memory.buffer);
this.encoder = r.getUint32(e, !0), this.exports.ghostty_wasm_free_opaque(e);
}
setOption(t, e) {
const s = this.exports.ghostty_wasm_alloc_u8();
new DataView(this.exports.memory.buffer).setUint8(s, typeof e == "boolean" ? e ? 1 : 0 : e), this.exports.ghostty_key_encoder_setopt(this.encoder, t, s), this.exports.ghostty_wasm_free_u8(s);
}
setKittyFlags(t) {
this.setOption(N.KITTY_KEYBOARD_FLAGS, t);
}
encode(t) {
const e = this.exports.ghostty_wasm_alloc_opaque(), s = this.exports.ghostty_key_event_new(0, e);
if (s !== 0)
throw new Error(`Failed to create key event: ${s}`);
const r = new DataView(this.exports.memory.buffer), o = r.getUint32(e, !0);
if (this.exports.ghostty_wasm_free_opaque(e), this.exports.ghostty_key_event_set_action(o, t.action), this.exports.ghostty_key_event_set_key(o, t.key), this.exports.ghostty_key_event_set_mods(o, t.mods), t.utf8) {
const w = new TextEncoder().encode(t.utf8), d = this.exports.ghostty_wasm_alloc_u8_array(w.length);
new Uint8Array(this.exports.memory.buffer).set(w, d), this.exports.ghostty_key_event_set_utf8(o, d, w.length), this.exports.ghostty_wasm_free_u8_array(d, w.length);
}
const n = 32, a = this.exports.ghostty_wasm_alloc_u8_array(n), l = this.exports.ghostty_wasm_alloc_usize(), c = this.exports.ghostty_key_encoder_encode(
this.encoder,
o,
a,
n,
l
);
if (c !== 0)
throw this.exports.ghostty_wasm_free_u8_array(a, n), this.exports.ghostty_wasm_free_usize(l), this.exports.ghostty_key_event_free(o), new Error(`Failed to encode key: ${c}`);
const u = r.getUint32(l, !0), m = new Uint8Array(this.exports.memory.buffer, a, u).slice();
return this.exports.ghostty_wasm_free_u8_array(a, n), this.exports.ghostty_wasm_free_usize(l), this.exports.ghostty_key_event_free(o), m;
}
dispose() {
this.encoder && (this.exports.ghostty_key_encoder_free(this.encoder), this.encoder = 0);
}
}
const z = class L {
constructor(t, e, s = 80, r = 24, o) {
var n;
if (this.viewportBufferPtr = 0, this.viewportBufferSize = 0, this.cellPool = [], this.graphemeBuffer = null, this.graphemeBufferPtr = 0, this.exports = t, this.memory = e, this._cols = s, this._rows = r, o) {
const a = this.exports.ghostty_wasm_alloc_u8_array(Y);
if (a === 0)
throw new Error("Failed to allocate config (out of memory)");
try {
const l = new DataView(this.memory.buffer);
let c = a;
l.setUint32(c, o.scrollbackLimit ?? 1e4, !0), c += 4, l.setUint32(c, o.fgColor ?? 0, !0), c += 4, l.setUint32(c, o.bgColor ?? 0, !0), c += 4, l.setUint32(c, o.cursorColor ?? 0, !0), c += 4;
for (let u = 0; u < 16; u++)
l.setUint32(c, ((n = o.palette) == null ? void 0 : n[u]) ?? 0, !0), c += 4;
this.handle = this.exports.ghostty_terminal_new_with_config(s, r, a);
} finally {
this.exports.ghostty_wasm_free_u8_array(a, Y);
}
} else
this.handle = this.exports.ghostty_terminal_new(s, r);
if (!this.handle)
throw new Error("Failed to create terminal");
this.initCellPool();
}
get cols() {
return this._cols;
}
get rows() {
return this._rows;
}
// ==========================================================================
// Lifecycle
// ==========================================================================
write(t) {
const e = typeof t == "string" ? new TextEncoder().encode(t) : t, s = this.exports.ghostty_wasm_alloc_u8_array(e.length);
new Uint8Array(this.memory.buffer).set(e, s), this.exports.ghostty_terminal_write(this.handle, s, e.length), this.exports.ghostty_wasm_free_u8_array(s, e.length);
}
resize(t, e) {
t === this._cols && e === this._rows || (this._cols = t, this._rows = e, this.exports.ghostty_terminal_resize(this.handle, t, e), this.invalidateBuffers(), this.initCellPool());
}
free() {
this.viewportBufferPtr && (this.exports.ghostty_wasm_free_u8_array(this.viewportBufferPtr, this.viewportBufferSize), this.viewportBufferPtr = 0), this.exports.ghostty_terminal_free(this.handle);
}
// ==========================================================================
// RenderState API - The key performance optimization
// ==========================================================================
/**
* Update render state from terminal.
*
* This syncs the RenderState with the current Terminal state.
* The dirty state (full/partial/none) is stored in the WASM RenderState
* and can be queried via isRowDirty(). When dirty==full, isRowDirty()
* returns true for ALL rows.
*
* The WASM layer automatically detects screen switches (normal <-> alternate)
* and returns FULL dirty state when switching screens (e.g., vim exit).
*
* Safe to call multiple times - dirty state persists until markClean().
*/
update() {
return this.exports.ghostty_render_state_update(this.handle);
}
/**
* Get cursor state from render state.
* Ensures render state is fresh by calling update().
*/
getCursor() {
return this.update(), {
x: this.exports.ghostty_render_state_get_cursor_x(this.handle),
y: this.exports.ghostty_render_state_get_cursor_y(this.handle),
viewportX: this.exports.ghostty_render_state_get_cursor_x(this.handle),
viewportY: this.exports.ghostty_render_state_get_cursor_y(this.handle),
visible: this.exports.ghostty_render_state_get_cursor_visible(this.handle),
blinking: !1,
// TODO: Add blinking support
style: "block"
// TODO: Add style support
};
}
/**
* Get default colors from render state
*/
getColors() {
const t = this.exports.ghostty_render_state_get_bg_color(this.handle), e = this.exports.ghostty_render_state_get_fg_color(this.handle);
return {
background: {
r: t >> 16 & 255,
g: t >> 8 & 255,
b: t & 255
},
foreground: {
r: e >> 16 & 255,
g: e >> 8 & 255,
b: e & 255
},
cursor: null
// TODO: Add cursor color support
};
}
/**
* Check if a specific row is dirty
*/
isRowDirty(t) {
return this.exports.ghostty_render_state_is_row_dirty(this.handle, t);
}
/**
* Mark render state as clean (call after rendering)
*/
markClean() {
this.exports.ghostty_render_state_mark_clean(this.handle);
}
/**
* Get ALL viewport cells in ONE WASM call - the key performance optimization!
* Returns a reusable cell array (zero allocation after warmup).
*/
getViewport() {
const t = this._cols * this._rows, e = t * L.CELL_SIZE;
return (!this.viewportBufferPtr || this.viewportBufferSize < e) && (this.viewportBufferPtr && this.exports.ghostty_wasm_free_u8_array(this.viewportBufferPtr, this.viewportBufferSize), this.viewportBufferPtr = this.exports.ghostty_wasm_alloc_u8_array(e), this.viewportBufferSize = e), this.exports.ghostty_render_state_get_viewport(
this.handle,
this.viewportBufferPtr,
t
) < 0 ? this.cellPool : (this.parseCellsIntoPool(this.viewportBufferPtr, t), this.cellPool);
}
// ==========================================================================
// Compatibility methods (delegate to render state)
// ==========================================================================
/**
* Get line - for compatibility, extracts from viewport.
* Ensures render state is fresh by calling update().
* Returns a COPY of the cells to avoid pool reference issues.
*/
getLine(t) {
if (t < 0 || t >= this._rows)
return null;
this.update();
const e = this.getViewport(), s = t * this._cols;
return e.slice(s, s + this._cols).map((r) => ({ ...r }));
}
/** For compatibility with old API */
isDirty() {
return this.update() !== I.NONE;
}
/**
* Check if a full redraw is needed (screen change, resize, etc.)
* Note: This calls update() to ensure fresh state. Safe to call multiple times.
*/
needsFullRedraw() {
return this.update() === I.FULL;
}
/** Mark render state as clean after rendering */
clearDirty() {
this.markClean();
}
// ==========================================================================
// Terminal modes
// ==========================================================================
isAlternateScreen() {
return !!this.exports.ghostty_terminal_is_alternate_screen(this.handle);
}
hasBracketedPaste() {
return this.getMode(2004, !1);
}
hasFocusEvents() {
return this.getMode(1004, !1);
}
hasMouseTracking() {
return this.exports.ghostty_terminal_has_mouse_tracking(this.handle) !== 0;
}
// ==========================================================================
// Extended API (scrollback, modes, etc.)
// ==========================================================================
/** Get dimensions - for compatibility */
getDimensions() {
return { cols: this._cols, rows: this._rows };
}
/** Get number of scrollback lines (history, not including active screen) */
getScrollbackLength() {
return this.exports.ghostty_terminal_get_scrollback_length(this.handle);
}
/**
* Get a line from the scrollback buffer.
* Ensures render state is fresh by calling update().
* @param offset 0 = oldest line, (length-1) = most recent scrollback line
*/
getScrollbackLine(t) {
const e = this._cols * L.CELL_SIZE;
(!this.viewportBufferPtr || this.viewportBufferSize < e) && (this.viewportBufferPtr && this.exports.ghostty_wasm_free_u8_array(this.viewportBufferPtr, this.viewportBufferSize), this.viewportBufferPtr = this.exports.ghostty_wasm_alloc_u8_array(e), this.viewportBufferSize = e), this.update();
const s = this.exports.ghostty_terminal_get_scrollback_line(
this.handle,
t,
this.viewportBufferPtr,
this._cols
);
if (s < 0)
return null;
const r = [], o = this.memory.buffer, n = new Uint8Array(o, this.viewportBufferPtr, s * L.CELL_SIZE), a = new DataView(o, this.viewportBufferPtr, s * L.CELL_SIZE);
for (let l = 0; l < s; l++) {
const c = l * L.CELL_SIZE;
r.push({
codepoint: a.getUint32(c, !0),
fg_r: n[c + 4],
fg_g: n[c + 5],
fg_b: n[c + 6],
bg_r: n[c + 7],
bg_g: n[c + 8],
bg_b: n[c + 9],
flags: n[c + 10],
width: n[c + 11],
hyperlink_id: a.getUint16(c + 12, !0),
grapheme_len: n[c + 14]
});
}
return r;
}
/** Check if a row in the active screen is wrapped (soft-wrapped to next line) */
isRowWrapped(t) {
return this.exports.ghostty_terminal_is_row_wrapped(this.handle, t) !== 0;
}
/**
* Get the hyperlink URI for a cell at the given position.
* @param row Row index (0-based, in active viewport)
* @param col Column index (0-based)
* @returns The URI string, or null if no hyperlink at that position
*/
getHyperlinkUri(t, e) {
if (!this.exports.ghostty_terminal_get_hyperlink_uri)
return null;
const s = [2048, 8192, 32768];
for (const r of s) {
const o = this.exports.ghostty_wasm_alloc_u8_array(r);
try {
const n = this.exports.ghostty_terminal_get_hyperlink_uri(
this.handle,
t,
e,
o,
r
);
if (n === 0)
return null;
if (n === -1)
continue;
if (n < 0)
return null;
const a = new Uint8Array(this.memory.buffer, o, n);
return new TextDecoder().decode(a.slice());
} finally {
this.exports.ghostty_wasm_free_u8_array(o, r);
}
}
return null;
}
/**
* Get the hyperlink URI for a cell in the scrollback buffer.
* @param offset Scrollback line offset (0 = oldest, scrollback_len-1 = newest)
* @param col Column index (0-based)
* @returns The URI string, or null if no hyperlink at that position
*/
getScrollbackHyperlinkUri(t, e) {
if (!this.exports.ghostty_terminal_get_scrollback_hyperlink_uri)
return null;
const s = [2048, 8192, 32768];
for (const r of s) {
const o = this.exports.ghostty_wasm_alloc_u8_array(r);
try {
const n = this.exports.ghostty_terminal_get_scrollback_hyperlink_uri(
this.handle,
t,
e,
o,
r
);
if (n === 0)
return null;
if (n === -1)
continue;
if (n < 0)
return null;
const a = new Uint8Array(this.memory.buffer, o, n);
return new TextDecoder().decode(a.slice());
} finally {
this.exports.ghostty_wasm_free_u8_array(o, r);
}
}
return null;
}
/**
* Check if there are pending responses from the terminal.
* Responses are generated by escape sequences like DSR (Device Status Report).
*/
hasResponse() {
return this.exports.ghostty_terminal_has_response(this.handle);
}
/**
* Read pending responses from the terminal.
* Returns the response string, or null if no responses pending.
*
* Responses are generated by escape sequences that require replies:
* - DSR 6 (cursor position): Returns \x1b[row;colR
* - DSR 5 (operating status): Returns \x1b[0n
*/
readResponse() {
if (!this.hasResponse())
return null;
const t = 256, e = this.exports.ghostty_wasm_alloc_u8_array(t);
try {
const s = this.exports.ghostty_terminal_read_response(this.handle, e, t);
if (s <= 0)
return null;
const r = new Uint8Array(this.memory.buffer, e, s);
return new TextDecoder().decode(r.slice());
} finally {
this.exports.ghostty_wasm_free_u8_array(e, t);
}
}
/**
* Query arbitrary terminal mode by number
* @param mode Mode number (e.g., 25 for cursor visibility, 2004 for bracketed paste)
* @param isAnsi True for ANSI modes, false for DEC modes (default: false)
*/
getMode(t, e = !1) {
return this.exports.ghostty_terminal_get_mode(this.handle, t, e) !== 0;
}
// ==========================================================================
// Private helpers
// ==========================================================================
initCellPool() {
const t = this._cols * this._rows;
if (this.cellPool.length < t)
for (let e = this.cellPool.length; e < t; e++)
this.cellPool.push({
codepoint: 0,
fg_r: 204,
fg_g: 204,
fg_b: 204,
bg_r: 0,
bg_g: 0,
bg_b: 0,
flags: 0,
width: 1,
hyperlink_id: 0,
grapheme_len: 0
});
}
parseCellsIntoPool(t, e) {
const s = this.memory.buffer, r = new Uint8Array(s, t, e * L.CELL_SIZE), o = new DataView(s, t, e * L.CELL_SIZE);
for (let n = 0; n < e; n++) {
const a = n * L.CELL_SIZE, l = this.cellPool[n];
l.codepoint = o.getUint32(a, !0), l.fg_r = r[a + 4], l.fg_g = r[a + 5], l.fg_b = r[a + 6], l.bg_r = r[a + 7], l.bg_g = r[a + 8], l.bg_b = r[a + 9], l.flags = r[a + 10], l.width = r[a + 11], l.hyperlink_id = o.getUint16(a + 12, !0), l.grapheme_len = r[a + 14];
}
}
/**
* Get all codepoints for a grapheme cluster at the given position.
* For most cells this returns a single codepoint, but for complex scripts
* (Hindi, emoji with ZWJ, etc.) it returns multiple codepoints.
* @returns Array of codepoints, or null on error
*/
getGrapheme(t, e) {
this.graphemeBuffer || (this.graphemeBufferPtr = this.exports.ghostty_wasm_alloc_u8_array(16 * 4), this.graphemeBuffer = new Uint32Array(this.memory.buffer, this.graphemeBufferPtr, 16));
const s = this.exports.ghostty_render_state_get_grapheme(
this.handle,
t,
e,
this.graphemeBufferPtr,
16
);
if (s < 0)
return null;
const r = new Uint32Array(this.memory.buffer, this.graphemeBufferPtr, s);
return Array.from(r);
}
/**
* Get a string representation of the grapheme at the given position.
* This properly handles complex scripts like Hindi, emoji with ZWJ, etc.
*/
getGraphemeString(t, e) {
const s = this.getGrapheme(t, e);
return !s || s.length === 0 ? " " : String.fromCodePoint(...s);
}
/**
* Get all codepoints for a grapheme cluster in the scrollback buffer.
* @param offset Scrollback line offset (0 = oldest)
* @param col Column index
* @returns Array of codepoints, or null on error
*/
getScrollbackGrapheme(t, e) {
this.graphemeBuffer || (this.graphemeBufferPtr = this.exports.ghostty_wasm_alloc_u8_array(16 * 4), this.graphemeBuffer = new Uint32Array(this.memory.buffer, this.graphemeBufferPtr, 16));
const s = this.exports.ghostty_terminal_get_scrollback_grapheme(
this.handle,
t,
e,
this.graphemeBufferPtr,
16
);
if (s < 0)
return null;
const r = new Uint32Array(this.memory.buffer, this.graphemeBufferPtr, s);
return Array.from(r);
}
/**
* Get a string representation of a grapheme in the scrollback buffer.
*/
getScrollbackGraphemeString(t, e) {
const s = this.getScrollbackGrapheme(t, e);
return !s || s.length === 0 ? " " : String.fromCodePoint(...s);
}
invalidateBuffers() {
this.viewportBufferPtr && (this.exports.ghostty_wasm_free_u8_array(this.viewportBufferPtr, this.viewportBufferSize), this.viewportBufferPtr = 0, this.viewportBufferSize = 0), this.graphemeBufferPtr && (this.exports.ghostty_wasm_free_u8_array(this.graphemeBufferPtr, 16 * 4), this.graphemeBufferPtr = 0), this.graphemeBuffer = null;
}
};
z.CELL_SIZE = 16;
let Z = z;
class T {
constructor() {
this.listeners = [], this.event = (t) => (this.listeners.push(t), {
dispose: () => {
const e = this.listeners.indexOf(t);
e >= 0 && this.listeners.splice(e, 1);
}
});
}
fire(t) {
for (const e of this.listeners)
e(t);
}
dispose() {
this.listeners = [];
}
}
class Q {
constructor(t) {
this.bufferChangeEmitter = new T(), this.terminal = t;
}
get active() {
const t = this.terminal.wasmTerm;
return t ? t.isAlternateScreen() ? this.alternate : this.normal : this.normal;
}
get normal() {
return this._normalBuffer || (this._normalBuffer = new W(this.terminal, "normal")), this._normalBuffer;
}
get alternate() {
return this._alternateBuffer || (this._alternateBuffer = new W(this.terminal, "alternate")), this._alternateBuffer;
}
get onBufferChange() {
return this.bufferChangeEmitter.event;
}
/**
* Internal: Fire buffer change event when screen switches
* Should be called by Terminal when detecting screen change
*/
_fireBufferChange(t) {
this.bufferChangeEmitter.fire(t);
}
}
class W {
constructor(t, e) {
this.terminal = t, this.bufferType = e;
const s = {
codepoint: 0,
fg_r: 204,
fg_g: 204,
fg_b: 204,
bg_r: 0,
bg_g: 0,
bg_b: 0,
flags: 0,
width: 1,
hyperlink_id: 0,
grapheme_len: 0
};
this.nullCell = new B(s, 0);
}
get type() {
return this.bufferType;
}
get cursorX() {
const t = this.getWasmTerm();
return t ? t.getCursor().x : 0;
}
get cursorY() {
const t = this.getWasmTerm();
return t ? t.getCursor().y : 0;
}
get viewportY() {
return 0;
}
get baseY() {
return 0;
}
get length() {
const t = this.getWasmTerm();
return t ? this.bufferType === "alternate" ? t.rows : t.getScrollbackLength() + t.rows : 0;
}
getLine(t) {
const e = this.getWasmTerm();
if (!e || t < 0 || t >= this.length)
return;
const s = e.getScrollbackLength();
let r, o, n;
if (this.bufferType === "normal" && t < s) {
const a = t;
r = e.getScrollbackLine(a), n = !1;
} else
o = this.bufferType === "normal" ? t - s : t, r = e.getLine(o), n = e.isRowWrapped(o);
if (r)
return new J(r, n, e.cols);
}
getNullCell() {
return this.nullCell;
}
getWasmTerm() {
return this.terminal.wasmTerm;
}
}
class J {
constructor(t, e, s) {
this.cells = t, this._isWrapped = e, this._length = s;
}
get length() {
return this._length;
}
get isWrapped() {
return this._isWrapped;
}
getCell(t) {
if (!(t < 0 || t >= this._length))
return t >= this.cells.length ? new B(
{
codepoint: 0,
fg_r: 204,
fg_g: 204,
fg_b: 204,
bg_r: 0,
bg_g: 0,
bg_b: 0,
flags: 0,
width: 1,
hyperlink_id: 0,
grapheme_len: 0
},
t
) : new B(this.cells[t], t);
}
translateToString(t = !1, e = 0, s = this._length) {
const r = Math.max(0, Math.min(e, this._length)), o = Math.max(r, Math.min(s, this._length));
let n = "";
for (let a = r; a < o; a++) {
const l = this.getCell(a);
if (l) {
const c = l.getChars();
n += c;
}
}
return t && (n = n.trimEnd()), n;
}
}
class B {
constructor(t, e) {
this.cell = t, this.x = e;
}
getChars() {
const t = this.cell.codepoint;
return t === 0 ? "" : t < 0 || t > 1114111 || t >= 55296 && t <= 57343 ? "�" : String.fromCodePoint(t);
}
getCode() {
return this.cell.codepoint;
}
getWidth() {
return this.cell.width;
}
getFgColorMode() {
return -1;
}
getBgColorMode() {
return -1;
}
getFgColor() {
return this.cell.fg_r << 16 | this.cell.fg_g << 8 | this.cell.fg_b;
}
getBgColor() {
return this.cell.bg_r << 16 | this.cell.bg_g << 8 | this.cell.bg_b;
}
isBold() {
return this.cell.flags & E.BOLD ? 1 : 0;
}
isItalic() {
return this.cell.flags & E.ITALIC ? 1 : 0;
}
isUnderline() {
return this.cell.flags & E.UNDERLINE ? 1 : 0;
}
isStrikethrough() {
return this.cell.flags & E.STRIKETHROUGH ? 1 : 0;
}
isBlink() {
return this.cell.flags & E.BLINK ? 1 : 0;
}
isInverse() {
return this.cell.flags & E.INVERSE ? 1 : 0;
}
isInvisible() {
return this.cell.flags & E.INVISIBLE ? 1 : 0;
}
isFaint() {
return this.cell.flags & E.FAINT ? 1 : 0;
}
/**
* Get hyperlink ID for this cell (0 = no link)
* Used by link detection system
*/
getHyperlinkId() {
return this.cell.hyperlink_id;
}
/**
* Get the Unicode codepoint for this cell
* Used by link detection system
*/
getCodepoint() {
return this.cell.codepoint;
}
/**
* Check if cell has dim/faint attribute
* Added for IBufferCell compatibility
*/
isDim() {
return (this.cell.flags & E.FAINT) !== 0;
}
}
const j = {
// Letters
KeyA: h.A,
KeyB: h.B,
KeyC: h.C,
KeyD: h.D,
KeyE: h.E,
KeyF: h.F,
KeyG: h.G,
KeyH: h.H,
KeyI: h.I,
KeyJ: h.J,
KeyK: h.K,
KeyL: h.L,
KeyM: h.M,
KeyN: h.N,
KeyO: h.O,
KeyP: h.P,
KeyQ: h.Q,
KeyR: h.R,
KeyS: h.S,
KeyT: h.T,
KeyU: h.U,
KeyV: h.V,
KeyW: h.W,
KeyX: h.X,
KeyY: h.Y,
KeyZ: h.Z,
// Numbers
Digit1: h.ONE,
Digit2: h.TWO,
Digit3: h.THREE,
Digit4: h.FOUR,
Digit5: h.FIVE,
Digit6: h.SIX,
Digit7: h.SEVEN,
Digit8: h.EIGHT,
Digit9: h.NINE,
Digit0: h.ZERO,
// Special keys
Enter: h.ENTER,
Escape: h.ESCAPE,
Backspace: h.BACKSPACE,
Tab: h.TAB,
Space: h.SPACE,
// Punctuation
Minus: h.MINUS,
Equal: h.EQUAL,
BracketLeft: h.BRACKET_LEFT,
BracketRight: h.BRACKET_RIGHT,
Backslash: h.BACKSLASH,
Semicolon: h.SEMICOLON,
Quote: h.QUOTE,
Backquote: h.GRAVE,
Comma: h.COMMA,
Period: h.PERIOD,
Slash: h.SLASH,
// Function keys
CapsLock: h.CAPS_LOCK,
F1: h.F1,
F2: h.F2,
F3: h.F3,
F4: h.F4,
F5: h.F5,
F6: h.F6,
F7: h.F7,
F8: h.F8,
F9: h.F9,
F10: h.F10,
F11: h.F11,
F12: h.F12,
// Special function keys
PrintScreen: h.PRINT_SCREEN,
ScrollLock: h.SCROLL_LOCK,
Pause: h.PAUSE,
Insert: h.INSERT,
Home: h.HOME,
PageUp: h.PAGE_UP,
Delete: h.DELETE,
End: h.END,
PageDown: h.PAGE_DOWN,
// Arrow keys
ArrowRight: h.RIGHT,
ArrowLeft: h.LEFT,
ArrowDown: h.DOWN,
ArrowUp: h.UP,
// Keypad
NumLock: h.NUM_LOCK,
NumpadDivide: h.KP_DIVIDE,
NumpadMultiply: h.KP_MULTIPLY,
NumpadSubtract: h.KP_MINUS,
NumpadAdd: h.KP_PLUS,
NumpadEnter: h.KP_ENTER,
Numpad1: h.KP_1,
Numpad2: h.KP_2,
Numpad3: h.KP_3,
Numpad4: h.KP_4,
Numpad5: h.KP_5,
Numpad6: h.KP_6,
Numpad7: h.KP_7,
Numpad8: h.KP_8,
Numpad9: h.KP_9,
Numpad0: h.KP_0,
NumpadDecimal: h.KP_PERIOD,
// International
IntlBackslash: h.INTL_BACKSLASH,
ContextMenu: h.CONTEXT_MENU,
// Additional function keys
F13: h.F13,
F14: h.F14,
F15: h.F15,
F16: h.F16,
F17: h.F17,
F18: h.F18,
F19: h.F19,
F20: h.F20,
F21: h.F21,
F22: h.F22,
F23: h.F23,
F24: h.F24
}, X = class x {
/**
* Create a new InputHandler
* @param ghostty - Ghostty instance (for creating KeyEncoder)
* @param container - DOM element to attach listeners to
* @param onData - Callback for terminal data (escape sequences to send to PTY)
* @param onBell - Callback for bell/beep event
* @param onKey - Optional callback for raw key events
* @param customKeyEventHandler - Optional custom key event handler
* @param getMode - Optional callback to query terminal mode state (for application cursor mode)
* @param onCopy - Optional callback to handle copy (Cmd+C/Ctrl+C with selection)
* @param inputElement - Optional input element for beforeinput events
* @param mouseConfig - Optional mouse tracking configuration
*/
constructor(t, e, s, r, o, n, a, l, c, u) {
this.keydownListener = null, this.keypressListener = null, this.pasteListener = null, this.beforeInputListener = null, this.compositionStartListener = null, this.compositionUpdateListener = null, this.compositionEndListener = null, this.mousedownListener = null, this.mouseupListener = null, this.mousemoveListener = null, this.wheelListener = null, this.isComposing = !1, this.isDisposed = !1, this.mouseButtonsPressed = 0, this.lastKeyDownData = null, this.lastKeyDownTime = 0, this.lastPasteData = null, this.lastPasteTime = 0, this.lastPasteSource = null, this.lastCompositionData = null, this.lastCompositionTime = 0, this.lastBeforeInputData = null, this.lastBeforeInputTime = 0, this.encoder = t.createKeyEncoder(), this.container = e, this.inputElement = c, this.onDataCallback = s, this.onBellCallback = r, this.onKeyCallback = o, this.customKeyEventHandler = n, this.getModeCallback = a, this.onCopyCallback = l, this.mouseConfig = u, this.attach();
}
/**
* Set custom key event handler (for runtime updates)
*/
setCustomKeyEventHandler(t) {
this.customKeyEventHandler = t;
}
/**
* Attach keyboard event listeners to container
*/
attach() {
typeof this.container.hasAttribute == "function" && typeof this.container.setAttribute == "function" && (this.container.hasAttribute("tabindex") || this.container.setAttribute("tabindex", "0"), this.container.style && (this.container.style.outline = "none")), this.keydownListener = this.handleKeyDown.bind(this), this.container.addEventListener("keydown", this.keydownListener), this.pasteListener = this.handlePaste.bind(this), this.container.addEventListener("paste", this.pasteListener), this.inputElement && this.inputElement !== this.container && this.inputElement.addEventListener("paste", this.pasteListener), this.inputElement && (this.beforeInputListener = this.handleBeforeInput.bind(this), this.inputElement.addEventListener("beforeinput", this.beforeInputListener)), this.compositionStartListener = this.handleCompositionStart.bind(this), this.container.addEventListener("compositionstart", this.compositionStartListener), this.compositionUpdateListener = this.handleCompositionUpdate.bind(this), this.container.addEventListener("compositionupdate", this.compositionUpdateListener), this.compositionEndListener = this.handleCompositionEnd.bind(this), this.container.addEventListener("compositionend", this.compositionEndListener), this.mousedownListener = this.handleMouseDown.bind(this), this.container.addEventListener("mousedown", this.mousedownListener), this.mouseupListener = this.handleMouseUp.bind(this), this.container.addEventListener("mouseup", this.mouseupListener), this.mousemoveListener = this.handleMouseMove.bind(this), this.container.addEventListener("mousemove", this.mousemoveListener), this.wheelListener = this.handleWheel.bind(this), this.container.addEventListener("wheel", this.wheelListener, { passive: !1 });
}
/**
* Map KeyboardEvent.code to USB HID Key enum value
* @param code - KeyboardEvent.code value
* @returns Key enum value or null if unmapped
*/
mapKeyCode(t) {
return j[t] ?? null;
}
/**
* Extract modifier flags from KeyboardEvent
* @param event - KeyboardEvent
* @returns Mods flags
*/
extractModifiers(t) {
let e = C.NONE;
return t.shiftKey && (e |= C.SHIFT), t.ctrlKey && (e |= C.CTRL), t.altKey && (e |= C.ALT), t.metaKey && (e |= C.SUPER), e;
}
/**
* Check if this is a printable character with no special modifiers
* @param event - KeyboardEvent
* @returns true if printable character
*/
isPrintableCharacter(t) {
return t.ctrlKey && !t.altKey || t.altKey && !t.ctrlKey || t.metaKey ? !1 : t.key.length === 1;
}
/**
* Handle keydown event
* @param event - KeyboardEvent
*/
handleKeyDown(t) {
if (this.isDisposed || this.isComposing || t.isComposing || t.keyCode === 229)
return;
if (this.onKeyCallback && this.onKeyCallback({ key: t.key, domEvent: t }), this.customKeyEventHandler && this.customKeyEventHandler(t)) {
t.preventDefault();
return;
}
if ((t.ctrlKey || t.metaKey) && t.code === "KeyV")
return;
if (t.metaKey && t.code === "KeyC") {
this.onCopyCallback && this.onCopyCallback() && t.preventDefault();
return;
}
if (this.isPrintableCharacter(t)) {
t.preventDefault(), this.onDataCallback(t.key), this.recordKeyDownData(t.key);
return;
}
const e = this.mapKeyCode(t.code);
if (e === null)
return;
const s = this.extractModifiers(t);
if (s === C.NONE || s === C.SHIFT) {
let o = null;
switch (e) {
case h.ENTER:
o = "\r";
break;
case h.TAB:
s === C.SHIFT ? o = "\x1B[Z" : o = " ";
break;
case h.BACKSPACE:
o = "";
break;
case h.ESCAPE:
o = "\x1B";
break;
case h.HOME:
o = "\x1B[H";
break;
case h.END:
o = "\x1B[F";
break;
case h.INSERT:
o = "\x1B[2~";
break;
case h.DELETE:
o = "\x1B[3~";
break;
case h.PAGE_UP:
o = "\x1B[5~";
break;
case h.PAGE_DOWN:
o = "\x1B[6~";
break;
case h.F1:
o = "\x1BOP";
break;
case h.F2:
o = "\x1BOQ";
break;
case h.F3:
o = "\x1BOR";
break;
case h.F4:
o = "\x1BOS";
break;
case h.F5:
o = "\x1B[15~";
break;
case h.F6:
o = "\x1B[17~";
break;
case h.F7:
o = "\x1B[18~";
break;
case h.F8:
o = "\x1B[19~";
break;
case h.F9:
o = "\x1B[20~";
break;
case h.F10:
o = "\x1B[21~";
break;
case h.F11: