@elizaos/tui 2.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +761 -0
  2. package/dist/autocomplete.d.ts +48 -0
  3. package/dist/autocomplete.d.ts.map +1 -0
  4. package/dist/autocomplete.js +555 -0
  5. package/dist/components/box.d.ts +22 -0
  6. package/dist/components/box.d.ts.map +1 -0
  7. package/dist/components/box.js +103 -0
  8. package/dist/components/cancellable-loader.d.ts +22 -0
  9. package/dist/components/cancellable-loader.d.ts.map +1 -0
  10. package/dist/components/cancellable-loader.js +34 -0
  11. package/dist/components/editor/history.d.ts +38 -0
  12. package/dist/components/editor/history.d.ts.map +1 -0
  13. package/dist/components/editor/history.js +76 -0
  14. package/dist/components/editor/index.d.ts +18 -0
  15. package/dist/components/editor/index.d.ts.map +1 -0
  16. package/dist/components/editor/index.js +21 -0
  17. package/dist/components/editor/kill-ring.d.ts +41 -0
  18. package/dist/components/editor/kill-ring.d.ts.map +1 -0
  19. package/dist/components/editor/kill-ring.js +81 -0
  20. package/dist/components/editor/layout.d.ts +40 -0
  21. package/dist/components/editor/layout.d.ts.map +1 -0
  22. package/dist/components/editor/layout.js +205 -0
  23. package/dist/components/editor/types.d.ts +68 -0
  24. package/dist/components/editor/types.d.ts.map +1 -0
  25. package/dist/components/editor/types.js +4 -0
  26. package/dist/components/editor/undo.d.ts +46 -0
  27. package/dist/components/editor/undo.d.ts.map +1 -0
  28. package/dist/components/editor/undo.js +58 -0
  29. package/dist/components/editor.d.ts +196 -0
  30. package/dist/components/editor.d.ts.map +1 -0
  31. package/dist/components/editor.js +1707 -0
  32. package/dist/components/image.d.ts +28 -0
  33. package/dist/components/image.d.ts.map +1 -0
  34. package/dist/components/image.js +68 -0
  35. package/dist/components/input.d.ts +19 -0
  36. package/dist/components/input.d.ts.map +1 -0
  37. package/dist/components/input.js +195 -0
  38. package/dist/components/loader.d.ts +21 -0
  39. package/dist/components/loader.d.ts.map +1 -0
  40. package/dist/components/loader.js +49 -0
  41. package/dist/components/markdown/index.d.ts +13 -0
  42. package/dist/components/markdown/index.d.ts.map +1 -0
  43. package/dist/components/markdown/index.js +9 -0
  44. package/dist/components/markdown/inline-renderer.d.ts +22 -0
  45. package/dist/components/markdown/inline-renderer.d.ts.map +1 -0
  46. package/dist/components/markdown/inline-renderer.js +88 -0
  47. package/dist/components/markdown/list-renderer.d.ts +33 -0
  48. package/dist/components/markdown/list-renderer.d.ts.map +1 -0
  49. package/dist/components/markdown/list-renderer.js +110 -0
  50. package/dist/components/markdown/table-renderer.d.ts +43 -0
  51. package/dist/components/markdown/table-renderer.d.ts.map +1 -0
  52. package/dist/components/markdown/table-renderer.js +184 -0
  53. package/dist/components/markdown/types.d.ts +57 -0
  54. package/dist/components/markdown/types.d.ts.map +1 -0
  55. package/dist/components/markdown/types.js +13 -0
  56. package/dist/components/markdown.d.ts +44 -0
  57. package/dist/components/markdown.d.ts.map +1 -0
  58. package/dist/components/markdown.js +319 -0
  59. package/dist/components/progress-bar.d.ts +67 -0
  60. package/dist/components/progress-bar.d.ts.map +1 -0
  61. package/dist/components/progress-bar.js +124 -0
  62. package/dist/components/select-list.d.ts +32 -0
  63. package/dist/components/select-list.d.ts.map +1 -0
  64. package/dist/components/select-list.js +151 -0
  65. package/dist/components/settings-list.d.ts +50 -0
  66. package/dist/components/settings-list.d.ts.map +1 -0
  67. package/dist/components/settings-list.js +184 -0
  68. package/dist/components/spacer.d.ts +12 -0
  69. package/dist/components/spacer.d.ts.map +1 -0
  70. package/dist/components/spacer.js +22 -0
  71. package/dist/components/text.d.ts +19 -0
  72. package/dist/components/text.d.ts.map +1 -0
  73. package/dist/components/text.js +88 -0
  74. package/dist/components/toast.d.ts +73 -0
  75. package/dist/components/toast.d.ts.map +1 -0
  76. package/dist/components/toast.js +119 -0
  77. package/dist/components/truncated-text.d.ts +13 -0
  78. package/dist/components/truncated-text.d.ts.map +1 -0
  79. package/dist/components/truncated-text.js +50 -0
  80. package/dist/constants.d.ts +97 -0
  81. package/dist/constants.d.ts.map +1 -0
  82. package/dist/constants.js +126 -0
  83. package/dist/core/container.d.ts +32 -0
  84. package/dist/core/container.d.ts.map +1 -0
  85. package/dist/core/container.js +49 -0
  86. package/dist/core/index.d.ts +9 -0
  87. package/dist/core/index.d.ts.map +1 -0
  88. package/dist/core/index.js +10 -0
  89. package/dist/core/overlay.d.ts +44 -0
  90. package/dist/core/overlay.d.ts.map +1 -0
  91. package/dist/core/overlay.js +171 -0
  92. package/dist/core/types.d.ts +116 -0
  93. package/dist/core/types.d.ts.map +1 -0
  94. package/dist/core/types.js +14 -0
  95. package/dist/editor-component.d.ts +37 -0
  96. package/dist/editor-component.d.ts.map +1 -0
  97. package/dist/editor-component.js +1 -0
  98. package/dist/fuzzy.d.ts +16 -0
  99. package/dist/fuzzy.d.ts.map +1 -0
  100. package/dist/fuzzy.js +108 -0
  101. package/dist/index.d.ts +29 -0
  102. package/dist/index.d.ts.map +1 -0
  103. package/dist/index.js +59 -0
  104. package/dist/keybindings.d.ts +39 -0
  105. package/dist/keybindings.d.ts.map +1 -0
  106. package/dist/keybindings.js +113 -0
  107. package/dist/keys.d.ts +153 -0
  108. package/dist/keys.d.ts.map +1 -0
  109. package/dist/keys.js +951 -0
  110. package/dist/stdin-buffer.d.ts +48 -0
  111. package/dist/stdin-buffer.d.ts.map +1 -0
  112. package/dist/stdin-buffer.js +316 -0
  113. package/dist/terminal-image.d.ts +68 -0
  114. package/dist/terminal-image.d.ts.map +1 -0
  115. package/dist/terminal-image.js +287 -0
  116. package/dist/terminal.d.ts +71 -0
  117. package/dist/terminal.d.ts.map +1 -0
  118. package/dist/terminal.js +216 -0
  119. package/dist/themes/index.d.ts +103 -0
  120. package/dist/themes/index.d.ts.map +1 -0
  121. package/dist/themes/index.js +161 -0
  122. package/dist/tui.d.ts +90 -0
  123. package/dist/tui.d.ts.map +1 -0
  124. package/dist/tui.js +745 -0
  125. package/dist/types/marked-tokens.d.ts +57 -0
  126. package/dist/types/marked-tokens.d.ts.map +1 -0
  127. package/dist/types/marked-tokens.js +17 -0
  128. package/dist/utils/cursor-movement.d.ts +127 -0
  129. package/dist/utils/cursor-movement.d.ts.map +1 -0
  130. package/dist/utils/cursor-movement.js +251 -0
  131. package/dist/utils/index.d.ts +6 -0
  132. package/dist/utils/index.d.ts.map +1 -0
  133. package/dist/utils/index.js +7 -0
  134. package/dist/utils/paste-handler.d.ts +86 -0
  135. package/dist/utils/paste-handler.d.ts.map +1 -0
  136. package/dist/utils/paste-handler.js +121 -0
  137. package/dist/utils.d.ts +75 -0
  138. package/dist/utils.d.ts.map +1 -0
  139. package/dist/utils.js +796 -0
  140. package/package.json +53 -0
package/dist/keys.js ADDED
@@ -0,0 +1,951 @@
1
+ /**
2
+ * Keyboard input handling for terminal applications.
3
+ *
4
+ * Supports both legacy terminal sequences and Kitty keyboard protocol.
5
+ * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
6
+ * Reference: https://github.com/sst/opentui/blob/7da92b4088aebfe27b9f691c04163a48821e49fd/packages/core/src/lib/parse.keypress.ts
7
+ *
8
+ * Symbol keys are also supported, however some ctrl+symbol combos
9
+ * overlap with ASCII codes, e.g. ctrl+[ = ESC.
10
+ * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/#legacy-ctrl-mapping-of-ascii-keys
11
+ * Those can still be * used for ctrl+shift combos
12
+ *
13
+ * API:
14
+ * - matchesKey(data, keyId) - Check if input matches a key identifier
15
+ * - parseKey(data) - Parse input and return the key identifier
16
+ * - Key - Helper object for creating typed key identifiers
17
+ * - setKittyProtocolActive(active) - Set global Kitty protocol state
18
+ * - isKittyProtocolActive() - Query global Kitty protocol state
19
+ */
20
+ // =============================================================================
21
+ // Global Kitty Protocol State
22
+ // =============================================================================
23
+ let _kittyProtocolActive = false;
24
+ export function setKittyProtocolActive(active) {
25
+ _kittyProtocolActive = active;
26
+ }
27
+ export function isKittyProtocolActive() {
28
+ return _kittyProtocolActive;
29
+ }
30
+ /**
31
+ * Helper object for creating typed key identifiers with autocomplete.
32
+ *
33
+ * Usage:
34
+ * - Key.escape, Key.enter, Key.tab, etc. for special keys
35
+ * - Key.backtick, Key.comma, Key.period, etc. for symbol keys
36
+ * - Key.ctrl("c"), Key.alt("x") for single modifier
37
+ * - Key.ctrlShift("p"), Key.ctrlAlt("x") for combined modifiers
38
+ */
39
+ export const Key = {
40
+ // Special keys
41
+ escape: "escape",
42
+ esc: "esc",
43
+ enter: "enter",
44
+ return: "return",
45
+ tab: "tab",
46
+ space: "space",
47
+ backspace: "backspace",
48
+ delete: "delete",
49
+ insert: "insert",
50
+ clear: "clear",
51
+ home: "home",
52
+ end: "end",
53
+ pageUp: "pageUp",
54
+ pageDown: "pageDown",
55
+ up: "up",
56
+ down: "down",
57
+ left: "left",
58
+ right: "right",
59
+ f1: "f1",
60
+ f2: "f2",
61
+ f3: "f3",
62
+ f4: "f4",
63
+ f5: "f5",
64
+ f6: "f6",
65
+ f7: "f7",
66
+ f8: "f8",
67
+ f9: "f9",
68
+ f10: "f10",
69
+ f11: "f11",
70
+ f12: "f12",
71
+ // Symbol keys
72
+ backtick: "`",
73
+ hyphen: "-",
74
+ equals: "=",
75
+ leftbracket: "[",
76
+ rightbracket: "]",
77
+ backslash: "\\",
78
+ semicolon: ";",
79
+ quote: "'",
80
+ comma: ",",
81
+ period: ".",
82
+ slash: "/",
83
+ exclamation: "!",
84
+ at: "@",
85
+ hash: "#",
86
+ dollar: "$",
87
+ percent: "%",
88
+ caret: "^",
89
+ ampersand: "&",
90
+ asterisk: "*",
91
+ leftparen: "(",
92
+ rightparen: ")",
93
+ underscore: "_",
94
+ plus: "+",
95
+ pipe: "|",
96
+ tilde: "~",
97
+ leftbrace: "{",
98
+ rightbrace: "}",
99
+ colon: ":",
100
+ lessthan: "<",
101
+ greaterthan: ">",
102
+ question: "?",
103
+ // Single modifiers
104
+ ctrl: (key) => `ctrl+${key}`,
105
+ shift: (key) => `shift+${key}`,
106
+ alt: (key) => `alt+${key}`,
107
+ // Combined modifiers
108
+ ctrlShift: (key) => `ctrl+shift+${key}`,
109
+ shiftCtrl: (key) => `shift+ctrl+${key}`,
110
+ ctrlAlt: (key) => `ctrl+alt+${key}`,
111
+ altCtrl: (key) => `alt+ctrl+${key}`,
112
+ shiftAlt: (key) => `shift+alt+${key}`,
113
+ altShift: (key) => `alt+shift+${key}`,
114
+ // Triple modifiers
115
+ ctrlShiftAlt: (key) => `ctrl+shift+alt+${key}`,
116
+ };
117
+ // =============================================================================
118
+ // Constants
119
+ // =============================================================================
120
+ const SYMBOL_KEYS = new Set([
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
+ const MODIFIERS = {
154
+ shift: 1,
155
+ alt: 2,
156
+ ctrl: 4,
157
+ };
158
+ const LOCK_MASK = 64 + 128; // Caps Lock + Num Lock
159
+ const CODEPOINTS = {
160
+ escape: 27,
161
+ tab: 9,
162
+ enter: 13,
163
+ space: 32,
164
+ backspace: 127,
165
+ kpEnter: 57414, // Numpad Enter (Kitty protocol)
166
+ };
167
+ const ARROW_CODEPOINTS = {
168
+ up: -1,
169
+ down: -2,
170
+ right: -3,
171
+ left: -4,
172
+ };
173
+ const FUNCTIONAL_CODEPOINTS = {
174
+ delete: -10,
175
+ insert: -11,
176
+ pageUp: -12,
177
+ pageDown: -13,
178
+ home: -14,
179
+ end: -15,
180
+ };
181
+ const LEGACY_KEY_SEQUENCES = {
182
+ up: ["\x1b[A", "\x1bOA"],
183
+ down: ["\x1b[B", "\x1bOB"],
184
+ right: ["\x1b[C", "\x1bOC"],
185
+ left: ["\x1b[D", "\x1bOD"],
186
+ home: ["\x1b[H", "\x1bOH", "\x1b[1~", "\x1b[7~"],
187
+ end: ["\x1b[F", "\x1bOF", "\x1b[4~", "\x1b[8~"],
188
+ insert: ["\x1b[2~"],
189
+ delete: ["\x1b[3~"],
190
+ pageUp: ["\x1b[5~", "\x1b[[5~"],
191
+ pageDown: ["\x1b[6~", "\x1b[[6~"],
192
+ clear: ["\x1b[E", "\x1bOE"],
193
+ f1: ["\x1bOP", "\x1b[11~", "\x1b[[A"],
194
+ f2: ["\x1bOQ", "\x1b[12~", "\x1b[[B"],
195
+ f3: ["\x1bOR", "\x1b[13~", "\x1b[[C"],
196
+ f4: ["\x1bOS", "\x1b[14~", "\x1b[[D"],
197
+ f5: ["\x1b[15~", "\x1b[[E"],
198
+ f6: ["\x1b[17~"],
199
+ f7: ["\x1b[18~"],
200
+ f8: ["\x1b[19~"],
201
+ f9: ["\x1b[20~"],
202
+ f10: ["\x1b[21~"],
203
+ f11: ["\x1b[23~"],
204
+ f12: ["\x1b[24~"],
205
+ };
206
+ const LEGACY_SHIFT_SEQUENCES = {
207
+ up: ["\x1b[a"],
208
+ down: ["\x1b[b"],
209
+ right: ["\x1b[c"],
210
+ left: ["\x1b[d"],
211
+ clear: ["\x1b[e"],
212
+ insert: ["\x1b[2$"],
213
+ delete: ["\x1b[3$"],
214
+ pageUp: ["\x1b[5$"],
215
+ pageDown: ["\x1b[6$"],
216
+ home: ["\x1b[7$"],
217
+ end: ["\x1b[8$"],
218
+ };
219
+ const LEGACY_CTRL_SEQUENCES = {
220
+ up: ["\x1bOa"],
221
+ down: ["\x1bOb"],
222
+ right: ["\x1bOc"],
223
+ left: ["\x1bOd"],
224
+ clear: ["\x1bOe"],
225
+ insert: ["\x1b[2^"],
226
+ delete: ["\x1b[3^"],
227
+ pageUp: ["\x1b[5^"],
228
+ pageDown: ["\x1b[6^"],
229
+ home: ["\x1b[7^"],
230
+ end: ["\x1b[8^"],
231
+ };
232
+ const LEGACY_SEQUENCE_KEY_IDS = {
233
+ "\x1bOA": "up",
234
+ "\x1bOB": "down",
235
+ "\x1bOC": "right",
236
+ "\x1bOD": "left",
237
+ "\x1bOH": "home",
238
+ "\x1bOF": "end",
239
+ "\x1b[E": "clear",
240
+ "\x1bOE": "clear",
241
+ "\x1bOe": "ctrl+clear",
242
+ "\x1b[e": "shift+clear",
243
+ "\x1b[2~": "insert",
244
+ "\x1b[2$": "shift+insert",
245
+ "\x1b[2^": "ctrl+insert",
246
+ "\x1b[3$": "shift+delete",
247
+ "\x1b[3^": "ctrl+delete",
248
+ "\x1b[[5~": "pageUp",
249
+ "\x1b[[6~": "pageDown",
250
+ "\x1b[a": "shift+up",
251
+ "\x1b[b": "shift+down",
252
+ "\x1b[c": "shift+right",
253
+ "\x1b[d": "shift+left",
254
+ "\x1bOa": "ctrl+up",
255
+ "\x1bOb": "ctrl+down",
256
+ "\x1bOc": "ctrl+right",
257
+ "\x1bOd": "ctrl+left",
258
+ "\x1b[5$": "shift+pageUp",
259
+ "\x1b[6$": "shift+pageDown",
260
+ "\x1b[7$": "shift+home",
261
+ "\x1b[8$": "shift+end",
262
+ "\x1b[5^": "ctrl+pageUp",
263
+ "\x1b[6^": "ctrl+pageDown",
264
+ "\x1b[7^": "ctrl+home",
265
+ "\x1b[8^": "ctrl+end",
266
+ "\x1bOP": "f1",
267
+ "\x1bOQ": "f2",
268
+ "\x1bOR": "f3",
269
+ "\x1bOS": "f4",
270
+ "\x1b[11~": "f1",
271
+ "\x1b[12~": "f2",
272
+ "\x1b[13~": "f3",
273
+ "\x1b[14~": "f4",
274
+ "\x1b[[A": "f1",
275
+ "\x1b[[B": "f2",
276
+ "\x1b[[C": "f3",
277
+ "\x1b[[D": "f4",
278
+ "\x1b[[E": "f5",
279
+ "\x1b[15~": "f5",
280
+ "\x1b[17~": "f6",
281
+ "\x1b[18~": "f7",
282
+ "\x1b[19~": "f8",
283
+ "\x1b[20~": "f9",
284
+ "\x1b[21~": "f10",
285
+ "\x1b[23~": "f11",
286
+ "\x1b[24~": "f12",
287
+ "\x1bb": "alt+left",
288
+ "\x1bf": "alt+right",
289
+ "\x1bp": "alt+up",
290
+ "\x1bn": "alt+down",
291
+ };
292
+ const matchesLegacySequence = (data, sequences) => sequences.includes(data);
293
+ const matchesLegacyModifierSequence = (data, key, modifier) => {
294
+ if (modifier === MODIFIERS.shift) {
295
+ return matchesLegacySequence(data, LEGACY_SHIFT_SEQUENCES[key]);
296
+ }
297
+ if (modifier === MODIFIERS.ctrl) {
298
+ return matchesLegacySequence(data, LEGACY_CTRL_SEQUENCES[key]);
299
+ }
300
+ return false;
301
+ };
302
+ // Store the last parsed event type for isKeyRelease() to query
303
+ let _lastEventType = "press";
304
+ /**
305
+ * Check if the last parsed key event was a key release.
306
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
307
+ */
308
+ export function isKeyRelease(data) {
309
+ // Don't treat bracketed paste content as key release, even if it contains
310
+ // patterns like ":3F" (e.g., bluetooth MAC addresses like "90:62:3F:A5").
311
+ // Terminal.ts re-wraps paste content with bracketed paste markers before
312
+ // passing to TUI, so pasted data will always contain \x1b[200~.
313
+ if (data.includes("\x1b[200~")) {
314
+ return false;
315
+ }
316
+ // Quick check: release events with flag 2 contain ":3"
317
+ // Format: \x1b[<codepoint>;<modifier>:3u
318
+ if (data.includes(":3u") ||
319
+ data.includes(":3~") ||
320
+ data.includes(":3A") ||
321
+ data.includes(":3B") ||
322
+ data.includes(":3C") ||
323
+ data.includes(":3D") ||
324
+ data.includes(":3H") ||
325
+ data.includes(":3F")) {
326
+ return true;
327
+ }
328
+ return false;
329
+ }
330
+ /**
331
+ * Check if the last parsed key event was a key repeat.
332
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
333
+ */
334
+ export function isKeyRepeat(data) {
335
+ // Don't treat bracketed paste content as key repeat, even if it contains
336
+ // patterns like ":2F". See isKeyRelease() for details.
337
+ if (data.includes("\x1b[200~")) {
338
+ return false;
339
+ }
340
+ if (data.includes(":2u") ||
341
+ data.includes(":2~") ||
342
+ data.includes(":2A") ||
343
+ data.includes(":2B") ||
344
+ data.includes(":2C") ||
345
+ data.includes(":2D") ||
346
+ data.includes(":2H") ||
347
+ data.includes(":2F")) {
348
+ return true;
349
+ }
350
+ return false;
351
+ }
352
+ function parseEventType(eventTypeStr) {
353
+ if (!eventTypeStr)
354
+ return "press";
355
+ const eventType = parseInt(eventTypeStr, 10);
356
+ if (eventType === 2)
357
+ return "repeat";
358
+ if (eventType === 3)
359
+ return "release";
360
+ return "press";
361
+ }
362
+ function parseKittySequence(data) {
363
+ // CSI u format with alternate keys (flag 4):
364
+ // \x1b[<codepoint>u
365
+ // \x1b[<codepoint>;<mod>u
366
+ // \x1b[<codepoint>;<mod>:<event>u
367
+ // \x1b[<codepoint>:<shifted>;<mod>u
368
+ // \x1b[<codepoint>:<shifted>:<base>;<mod>u
369
+ // \x1b[<codepoint>::<base>;<mod>u (no shifted key, only base)
370
+ //
371
+ // With flag 2, event type is appended after modifier colon: 1=press, 2=repeat, 3=release
372
+ // With flag 4, alternate keys are appended after codepoint with colons
373
+ const csiUMatch = data.match(/^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/);
374
+ if (csiUMatch) {
375
+ const codepoint = parseInt(csiUMatch[1], 10);
376
+ const shiftedKey = csiUMatch[2] && csiUMatch[2].length > 0 ? parseInt(csiUMatch[2], 10) : undefined;
377
+ const baseLayoutKey = csiUMatch[3] ? parseInt(csiUMatch[3], 10) : undefined;
378
+ const modValue = csiUMatch[4] ? parseInt(csiUMatch[4], 10) : 1;
379
+ const eventType = parseEventType(csiUMatch[5]);
380
+ _lastEventType = eventType;
381
+ return { codepoint, shiftedKey, baseLayoutKey, modifier: modValue - 1, eventType };
382
+ }
383
+ // Arrow keys with modifier: \x1b[1;<mod>A/B/C/D or \x1b[1;<mod>:<event>A/B/C/D
384
+ const arrowMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([ABCD])$/);
385
+ if (arrowMatch) {
386
+ const modValue = parseInt(arrowMatch[1], 10);
387
+ const eventType = parseEventType(arrowMatch[2]);
388
+ const arrowCodes = { A: -1, B: -2, C: -3, D: -4 };
389
+ _lastEventType = eventType;
390
+ return { codepoint: arrowCodes[arrowMatch[3]], modifier: modValue - 1, eventType };
391
+ }
392
+ // Functional keys: \x1b[<num>~ or \x1b[<num>;<mod>~ or \x1b[<num>;<mod>:<event>~
393
+ const funcMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?(?::(\d+))?~$/);
394
+ if (funcMatch) {
395
+ const keyNum = parseInt(funcMatch[1], 10);
396
+ const modValue = funcMatch[2] ? parseInt(funcMatch[2], 10) : 1;
397
+ const eventType = parseEventType(funcMatch[3]);
398
+ const funcCodes = {
399
+ 2: FUNCTIONAL_CODEPOINTS.insert,
400
+ 3: FUNCTIONAL_CODEPOINTS.delete,
401
+ 5: FUNCTIONAL_CODEPOINTS.pageUp,
402
+ 6: FUNCTIONAL_CODEPOINTS.pageDown,
403
+ 7: FUNCTIONAL_CODEPOINTS.home,
404
+ 8: FUNCTIONAL_CODEPOINTS.end,
405
+ };
406
+ const codepoint = funcCodes[keyNum];
407
+ if (codepoint !== undefined) {
408
+ _lastEventType = eventType;
409
+ return { codepoint, modifier: modValue - 1, eventType };
410
+ }
411
+ }
412
+ // Home/End with modifier: \x1b[1;<mod>H/F or \x1b[1;<mod>:<event>H/F
413
+ const homeEndMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([HF])$/);
414
+ if (homeEndMatch) {
415
+ const modValue = parseInt(homeEndMatch[1], 10);
416
+ const eventType = parseEventType(homeEndMatch[2]);
417
+ const codepoint = homeEndMatch[3] === "H" ? FUNCTIONAL_CODEPOINTS.home : FUNCTIONAL_CODEPOINTS.end;
418
+ _lastEventType = eventType;
419
+ return { codepoint, modifier: modValue - 1, eventType };
420
+ }
421
+ return null;
422
+ }
423
+ function matchesKittySequence(data, expectedCodepoint, expectedModifier) {
424
+ const parsed = parseKittySequence(data);
425
+ if (!parsed)
426
+ return false;
427
+ const actualMod = parsed.modifier & ~LOCK_MASK;
428
+ const expectedMod = expectedModifier & ~LOCK_MASK;
429
+ // Check if modifiers match
430
+ if (actualMod !== expectedMod)
431
+ return false;
432
+ // Primary match: codepoint matches directly
433
+ if (parsed.codepoint === expectedCodepoint)
434
+ return true;
435
+ // Alternate match: use base layout key for non-Latin keyboard layouts.
436
+ // This allows Ctrl+С (Cyrillic) to match Ctrl+c (Latin) when terminal reports
437
+ // the base layout key (the key in standard PC-101 layout).
438
+ //
439
+ // Only fall back to base layout key when the codepoint is NOT already a
440
+ // recognized Latin letter (a-z) or symbol (e.g., /, -, [, ;, etc.).
441
+ // When the codepoint is a recognized key, it is authoritative regardless
442
+ // of physical key position. This prevents remapped layouts (Dvorak, Colemak,
443
+ // xremap, etc.) from causing false matches: both letters and symbols move
444
+ // to different physical positions, so Ctrl+K could falsely match Ctrl+V
445
+ // (letter remapping) and Ctrl+/ could falsely match Ctrl+[ (symbol remapping)
446
+ // if the base layout key were always considered.
447
+ if (parsed.baseLayoutKey !== undefined && parsed.baseLayoutKey === expectedCodepoint) {
448
+ const cp = parsed.codepoint;
449
+ const isLatinLetter = cp >= 97 && cp <= 122; // a-z
450
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(cp));
451
+ if (!isLatinLetter && !isKnownSymbol)
452
+ return true;
453
+ }
454
+ return false;
455
+ }
456
+ /**
457
+ * Match xterm modifyOtherKeys format: CSI 27 ; modifiers ; keycode ~
458
+ * This is used by terminals when Kitty protocol is not enabled.
459
+ * Modifier values are 1-indexed: 2=shift, 3=alt, 5=ctrl, etc.
460
+ */
461
+ function matchesModifyOtherKeys(data, expectedKeycode, expectedModifier) {
462
+ const match = data.match(/^\x1b\[27;(\d+);(\d+)~$/);
463
+ if (!match)
464
+ return false;
465
+ const modValue = parseInt(match[1], 10);
466
+ const keycode = parseInt(match[2], 10);
467
+ // Convert from 1-indexed xterm format to our 0-indexed format
468
+ const actualMod = modValue - 1;
469
+ return keycode === expectedKeycode && actualMod === expectedModifier;
470
+ }
471
+ // =============================================================================
472
+ // Generic Key Matching
473
+ // =============================================================================
474
+ /**
475
+ * Get the control character for a key.
476
+ * Uses the universal formula: code & 0x1f (mask to lower 5 bits)
477
+ *
478
+ * Works for:
479
+ * - Letters a-z → 1-26
480
+ * - Symbols [\]_ → 27, 28, 29, 31
481
+ * - Also maps - to same as _ (same physical key on US keyboards)
482
+ */
483
+ function rawCtrlChar(key) {
484
+ const char = key.toLowerCase();
485
+ const code = char.charCodeAt(0);
486
+ if ((code >= 97 && code <= 122) || char === "[" || char === "\\" || char === "]" || char === "_") {
487
+ return String.fromCharCode(code & 0x1f);
488
+ }
489
+ // Handle - as _ (same physical key on US keyboards)
490
+ if (char === "-") {
491
+ return String.fromCharCode(31); // Same as Ctrl+_
492
+ }
493
+ return null;
494
+ }
495
+ function parseKeyId(keyId) {
496
+ const parts = keyId.toLowerCase().split("+");
497
+ const key = parts[parts.length - 1];
498
+ if (!key)
499
+ return null;
500
+ return {
501
+ key,
502
+ ctrl: parts.includes("ctrl"),
503
+ shift: parts.includes("shift"),
504
+ alt: parts.includes("alt"),
505
+ };
506
+ }
507
+ /**
508
+ * Match input data against a key identifier string.
509
+ *
510
+ * Supported key identifiers:
511
+ * - Single keys: "escape", "tab", "enter", "backspace", "delete", "home", "end", "space"
512
+ * - Arrow keys: "up", "down", "left", "right"
513
+ * - Ctrl combinations: "ctrl+c", "ctrl+z", etc.
514
+ * - Shift combinations: "shift+tab", "shift+enter"
515
+ * - Alt combinations: "alt+enter", "alt+backspace"
516
+ * - Combined modifiers: "shift+ctrl+p", "ctrl+alt+x"
517
+ *
518
+ * Use the Key helper for autocomplete: Key.ctrl("c"), Key.escape, Key.ctrlShift("p")
519
+ *
520
+ * @param data - Raw input data from terminal
521
+ * @param keyId - Key identifier (e.g., "ctrl+c", "escape", Key.ctrl("c"))
522
+ */
523
+ export function matchesKey(data, keyId) {
524
+ const parsed = parseKeyId(keyId);
525
+ if (!parsed)
526
+ return false;
527
+ const { key, ctrl, shift, alt } = parsed;
528
+ let modifier = 0;
529
+ if (shift)
530
+ modifier |= MODIFIERS.shift;
531
+ if (alt)
532
+ modifier |= MODIFIERS.alt;
533
+ if (ctrl)
534
+ modifier |= MODIFIERS.ctrl;
535
+ switch (key) {
536
+ case "escape":
537
+ case "esc":
538
+ if (modifier !== 0)
539
+ return false;
540
+ return data === "\x1b" || matchesKittySequence(data, CODEPOINTS.escape, 0);
541
+ case "space":
542
+ if (!_kittyProtocolActive) {
543
+ if (ctrl && !alt && !shift && data === "\x00") {
544
+ return true;
545
+ }
546
+ if (alt && !ctrl && !shift && data === "\x1b ") {
547
+ return true;
548
+ }
549
+ }
550
+ if (modifier === 0) {
551
+ return data === " " || matchesKittySequence(data, CODEPOINTS.space, 0);
552
+ }
553
+ return matchesKittySequence(data, CODEPOINTS.space, modifier);
554
+ case "tab":
555
+ if (shift && !ctrl && !alt) {
556
+ return data === "\x1b[Z" || matchesKittySequence(data, CODEPOINTS.tab, MODIFIERS.shift);
557
+ }
558
+ if (modifier === 0) {
559
+ return data === "\t" || matchesKittySequence(data, CODEPOINTS.tab, 0);
560
+ }
561
+ return matchesKittySequence(data, CODEPOINTS.tab, modifier);
562
+ case "enter":
563
+ case "return":
564
+ if (shift && !ctrl && !alt) {
565
+ // CSI u sequences (standard Kitty protocol)
566
+ if (matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.shift) ||
567
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.shift)) {
568
+ return true;
569
+ }
570
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
571
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.shift)) {
572
+ return true;
573
+ }
574
+ // When Kitty protocol is active, legacy sequences are custom terminal mappings
575
+ // \x1b\r = Kitty's "map shift+enter send_text all \e\r"
576
+ // \n = Ghostty's "keybind = shift+enter=text:\n"
577
+ if (_kittyProtocolActive) {
578
+ return data === "\x1b\r" || data === "\n";
579
+ }
580
+ return false;
581
+ }
582
+ if (alt && !ctrl && !shift) {
583
+ // CSI u sequences (standard Kitty protocol)
584
+ if (matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.alt) ||
585
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.alt)) {
586
+ return true;
587
+ }
588
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
589
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.alt)) {
590
+ return true;
591
+ }
592
+ // \x1b\r is alt+enter only in legacy mode (no Kitty protocol)
593
+ // When Kitty protocol is active, alt+enter comes as CSI u sequence
594
+ if (!_kittyProtocolActive) {
595
+ return data === "\x1b\r";
596
+ }
597
+ return false;
598
+ }
599
+ if (modifier === 0) {
600
+ return (data === "\r" ||
601
+ (!_kittyProtocolActive && data === "\n") ||
602
+ data === "\x1bOM" || // SS3 M (numpad enter in some terminals)
603
+ matchesKittySequence(data, CODEPOINTS.enter, 0) ||
604
+ matchesKittySequence(data, CODEPOINTS.kpEnter, 0));
605
+ }
606
+ return (matchesKittySequence(data, CODEPOINTS.enter, modifier) ||
607
+ matchesKittySequence(data, CODEPOINTS.kpEnter, modifier));
608
+ case "backspace":
609
+ if (alt && !ctrl && !shift) {
610
+ if (data === "\x1b\x7f" || data === "\x1b\b") {
611
+ return true;
612
+ }
613
+ return matchesKittySequence(data, CODEPOINTS.backspace, MODIFIERS.alt);
614
+ }
615
+ if (modifier === 0) {
616
+ return data === "\x7f" || data === "\x08" || matchesKittySequence(data, CODEPOINTS.backspace, 0);
617
+ }
618
+ return matchesKittySequence(data, CODEPOINTS.backspace, modifier);
619
+ case "insert":
620
+ if (modifier === 0) {
621
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.insert) ||
622
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, 0));
623
+ }
624
+ if (matchesLegacyModifierSequence(data, "insert", modifier)) {
625
+ return true;
626
+ }
627
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, modifier);
628
+ case "delete":
629
+ if (modifier === 0) {
630
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.delete) ||
631
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, 0));
632
+ }
633
+ if (matchesLegacyModifierSequence(data, "delete", modifier)) {
634
+ return true;
635
+ }
636
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, modifier);
637
+ case "clear":
638
+ if (modifier === 0) {
639
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.clear);
640
+ }
641
+ return matchesLegacyModifierSequence(data, "clear", modifier);
642
+ case "home":
643
+ if (modifier === 0) {
644
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.home) ||
645
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, 0));
646
+ }
647
+ if (matchesLegacyModifierSequence(data, "home", modifier)) {
648
+ return true;
649
+ }
650
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, modifier);
651
+ case "end":
652
+ if (modifier === 0) {
653
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.end) ||
654
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, 0));
655
+ }
656
+ if (matchesLegacyModifierSequence(data, "end", modifier)) {
657
+ return true;
658
+ }
659
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, modifier);
660
+ case "pageup":
661
+ if (modifier === 0) {
662
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageUp) ||
663
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, 0));
664
+ }
665
+ if (matchesLegacyModifierSequence(data, "pageUp", modifier)) {
666
+ return true;
667
+ }
668
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, modifier);
669
+ case "pagedown":
670
+ if (modifier === 0) {
671
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageDown) ||
672
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, 0));
673
+ }
674
+ if (matchesLegacyModifierSequence(data, "pageDown", modifier)) {
675
+ return true;
676
+ }
677
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, modifier);
678
+ case "up":
679
+ if (alt && !ctrl && !shift) {
680
+ return data === "\x1bp" || matchesKittySequence(data, ARROW_CODEPOINTS.up, MODIFIERS.alt);
681
+ }
682
+ if (modifier === 0) {
683
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.up) ||
684
+ matchesKittySequence(data, ARROW_CODEPOINTS.up, 0));
685
+ }
686
+ if (matchesLegacyModifierSequence(data, "up", modifier)) {
687
+ return true;
688
+ }
689
+ return matchesKittySequence(data, ARROW_CODEPOINTS.up, modifier);
690
+ case "down":
691
+ if (alt && !ctrl && !shift) {
692
+ return data === "\x1bn" || matchesKittySequence(data, ARROW_CODEPOINTS.down, MODIFIERS.alt);
693
+ }
694
+ if (modifier === 0) {
695
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.down) ||
696
+ matchesKittySequence(data, ARROW_CODEPOINTS.down, 0));
697
+ }
698
+ if (matchesLegacyModifierSequence(data, "down", modifier)) {
699
+ return true;
700
+ }
701
+ return matchesKittySequence(data, ARROW_CODEPOINTS.down, modifier);
702
+ case "left":
703
+ if (alt && !ctrl && !shift) {
704
+ return (data === "\x1b[1;3D" ||
705
+ (!_kittyProtocolActive && data === "\x1bB") ||
706
+ data === "\x1bb" ||
707
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.alt));
708
+ }
709
+ if (ctrl && !alt && !shift) {
710
+ return (data === "\x1b[1;5D" ||
711
+ matchesLegacyModifierSequence(data, "left", MODIFIERS.ctrl) ||
712
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.ctrl));
713
+ }
714
+ if (modifier === 0) {
715
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.left) ||
716
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, 0));
717
+ }
718
+ if (matchesLegacyModifierSequence(data, "left", modifier)) {
719
+ return true;
720
+ }
721
+ return matchesKittySequence(data, ARROW_CODEPOINTS.left, modifier);
722
+ case "right":
723
+ if (alt && !ctrl && !shift) {
724
+ return (data === "\x1b[1;3C" ||
725
+ (!_kittyProtocolActive && data === "\x1bF") ||
726
+ data === "\x1bf" ||
727
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.alt));
728
+ }
729
+ if (ctrl && !alt && !shift) {
730
+ return (data === "\x1b[1;5C" ||
731
+ matchesLegacyModifierSequence(data, "right", MODIFIERS.ctrl) ||
732
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.ctrl));
733
+ }
734
+ if (modifier === 0) {
735
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.right) ||
736
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, 0));
737
+ }
738
+ if (matchesLegacyModifierSequence(data, "right", modifier)) {
739
+ return true;
740
+ }
741
+ return matchesKittySequence(data, ARROW_CODEPOINTS.right, modifier);
742
+ case "f1":
743
+ case "f2":
744
+ case "f3":
745
+ case "f4":
746
+ case "f5":
747
+ case "f6":
748
+ case "f7":
749
+ case "f8":
750
+ case "f9":
751
+ case "f10":
752
+ case "f11":
753
+ case "f12": {
754
+ if (modifier !== 0) {
755
+ return false;
756
+ }
757
+ const functionKey = key;
758
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES[functionKey]);
759
+ }
760
+ }
761
+ // Handle single letter keys (a-z) and some symbols
762
+ if (key.length === 1 && ((key >= "a" && key <= "z") || SYMBOL_KEYS.has(key))) {
763
+ const codepoint = key.charCodeAt(0);
764
+ const rawCtrl = rawCtrlChar(key);
765
+ if (ctrl && alt && !shift && !_kittyProtocolActive && rawCtrl) {
766
+ // Legacy: ctrl+alt+key is ESC followed by the control character
767
+ return data === `\x1b${rawCtrl}`;
768
+ }
769
+ if (alt && !ctrl && !shift && !_kittyProtocolActive && key >= "a" && key <= "z") {
770
+ // Legacy: alt+letter is ESC followed by the letter
771
+ if (data === `\x1b${key}`)
772
+ return true;
773
+ }
774
+ if (ctrl && !shift && !alt) {
775
+ // Legacy: ctrl+key sends the control character
776
+ if (rawCtrl && data === rawCtrl)
777
+ return true;
778
+ return matchesKittySequence(data, codepoint, MODIFIERS.ctrl);
779
+ }
780
+ if (ctrl && shift && !alt) {
781
+ return matchesKittySequence(data, codepoint, MODIFIERS.shift + MODIFIERS.ctrl);
782
+ }
783
+ if (shift && !ctrl && !alt) {
784
+ // Legacy: shift+letter produces uppercase
785
+ if (data === key.toUpperCase())
786
+ return true;
787
+ return matchesKittySequence(data, codepoint, MODIFIERS.shift);
788
+ }
789
+ if (modifier !== 0) {
790
+ return matchesKittySequence(data, codepoint, modifier);
791
+ }
792
+ // Check both raw char and Kitty sequence (needed for release events)
793
+ return data === key || matchesKittySequence(data, codepoint, 0);
794
+ }
795
+ return false;
796
+ }
797
+ /**
798
+ * Parse input data and return the key identifier if recognized.
799
+ *
800
+ * @param data - Raw input data from terminal
801
+ * @returns Key identifier string (e.g., "ctrl+c") or undefined
802
+ */
803
+ export function parseKey(data) {
804
+ const kitty = parseKittySequence(data);
805
+ if (kitty) {
806
+ const { codepoint, baseLayoutKey, modifier } = kitty;
807
+ const mods = [];
808
+ const effectiveMod = modifier & ~LOCK_MASK;
809
+ if (effectiveMod & MODIFIERS.shift)
810
+ mods.push("shift");
811
+ if (effectiveMod & MODIFIERS.ctrl)
812
+ mods.push("ctrl");
813
+ if (effectiveMod & MODIFIERS.alt)
814
+ mods.push("alt");
815
+ // Use base layout key only when codepoint is not a recognized Latin
816
+ // letter (a-z) or symbol (/, -, [, ;, etc.). For those, the codepoint
817
+ // is authoritative regardless of physical key position. This prevents
818
+ // remapped layouts (Dvorak, Colemak, xremap, etc.) from reporting the
819
+ // wrong key name based on the QWERTY physical position.
820
+ const isLatinLetter = codepoint >= 97 && codepoint <= 122; // a-z
821
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(codepoint));
822
+ const effectiveCodepoint = isLatinLetter || isKnownSymbol ? codepoint : (baseLayoutKey ?? codepoint);
823
+ let keyName;
824
+ if (effectiveCodepoint === CODEPOINTS.escape)
825
+ keyName = "escape";
826
+ else if (effectiveCodepoint === CODEPOINTS.tab)
827
+ keyName = "tab";
828
+ else if (effectiveCodepoint === CODEPOINTS.enter || effectiveCodepoint === CODEPOINTS.kpEnter)
829
+ keyName = "enter";
830
+ else if (effectiveCodepoint === CODEPOINTS.space)
831
+ keyName = "space";
832
+ else if (effectiveCodepoint === CODEPOINTS.backspace)
833
+ keyName = "backspace";
834
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.delete)
835
+ keyName = "delete";
836
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.insert)
837
+ keyName = "insert";
838
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.home)
839
+ keyName = "home";
840
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.end)
841
+ keyName = "end";
842
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageUp)
843
+ keyName = "pageUp";
844
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageDown)
845
+ keyName = "pageDown";
846
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.up)
847
+ keyName = "up";
848
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.down)
849
+ keyName = "down";
850
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.left)
851
+ keyName = "left";
852
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.right)
853
+ keyName = "right";
854
+ else if (effectiveCodepoint >= 97 && effectiveCodepoint <= 122)
855
+ keyName = String.fromCharCode(effectiveCodepoint);
856
+ else if (SYMBOL_KEYS.has(String.fromCharCode(effectiveCodepoint)))
857
+ keyName = String.fromCharCode(effectiveCodepoint);
858
+ if (keyName) {
859
+ return mods.length > 0 ? `${mods.join("+")}+${keyName}` : keyName;
860
+ }
861
+ }
862
+ // Mode-aware legacy sequences
863
+ // When Kitty protocol is active, ambiguous sequences are interpreted as custom terminal mappings:
864
+ // - \x1b\r = shift+enter (Kitty mapping), not alt+enter
865
+ // - \n = shift+enter (Ghostty mapping)
866
+ if (_kittyProtocolActive) {
867
+ if (data === "\x1b\r" || data === "\n")
868
+ return "shift+enter";
869
+ }
870
+ const legacySequenceKeyId = LEGACY_SEQUENCE_KEY_IDS[data];
871
+ if (legacySequenceKeyId)
872
+ return legacySequenceKeyId;
873
+ // Legacy sequences (used when Kitty protocol is not active, or for unambiguous sequences)
874
+ if (data === "\x1b")
875
+ return "escape";
876
+ if (data === "\x1c")
877
+ return "ctrl+\\";
878
+ if (data === "\x1d")
879
+ return "ctrl+]";
880
+ if (data === "\x1f")
881
+ return "ctrl+-";
882
+ if (data === "\x1b\x1b")
883
+ return "ctrl+alt+[";
884
+ if (data === "\x1b\x1c")
885
+ return "ctrl+alt+\\";
886
+ if (data === "\x1b\x1d")
887
+ return "ctrl+alt+]";
888
+ if (data === "\x1b\x1f")
889
+ return "ctrl+alt+-";
890
+ if (data === "\t")
891
+ return "tab";
892
+ if (data === "\r" || (!_kittyProtocolActive && data === "\n") || data === "\x1bOM")
893
+ return "enter";
894
+ if (data === "\x00")
895
+ return "ctrl+space";
896
+ if (data === " ")
897
+ return "space";
898
+ if (data === "\x7f" || data === "\x08")
899
+ return "backspace";
900
+ if (data === "\x1b[Z")
901
+ return "shift+tab";
902
+ if (!_kittyProtocolActive && data === "\x1b\r")
903
+ return "alt+enter";
904
+ if (!_kittyProtocolActive && data === "\x1b ")
905
+ return "alt+space";
906
+ if (data === "\x1b\x7f" || data === "\x1b\b")
907
+ return "alt+backspace";
908
+ if (!_kittyProtocolActive && data === "\x1bB")
909
+ return "alt+left";
910
+ if (!_kittyProtocolActive && data === "\x1bF")
911
+ return "alt+right";
912
+ if (!_kittyProtocolActive && data.length === 2 && data[0] === "\x1b") {
913
+ const code = data.charCodeAt(1);
914
+ if (code >= 1 && code <= 26) {
915
+ return `ctrl+alt+${String.fromCharCode(code + 96)}`;
916
+ }
917
+ // Legacy alt+letter (ESC followed by letter a-z)
918
+ if (code >= 97 && code <= 122) {
919
+ return `alt+${String.fromCharCode(code)}`;
920
+ }
921
+ }
922
+ if (data === "\x1b[A")
923
+ return "up";
924
+ if (data === "\x1b[B")
925
+ return "down";
926
+ if (data === "\x1b[C")
927
+ return "right";
928
+ if (data === "\x1b[D")
929
+ return "left";
930
+ if (data === "\x1b[H" || data === "\x1bOH")
931
+ return "home";
932
+ if (data === "\x1b[F" || data === "\x1bOF")
933
+ return "end";
934
+ if (data === "\x1b[3~")
935
+ return "delete";
936
+ if (data === "\x1b[5~")
937
+ return "pageUp";
938
+ if (data === "\x1b[6~")
939
+ return "pageDown";
940
+ // Raw Ctrl+letter
941
+ if (data.length === 1) {
942
+ const code = data.charCodeAt(0);
943
+ if (code >= 1 && code <= 26) {
944
+ return `ctrl+${String.fromCharCode(code + 96)}`;
945
+ }
946
+ if (code >= 32 && code <= 126) {
947
+ return data;
948
+ }
949
+ }
950
+ return undefined;
951
+ }