open-pi-tui 0.57.1

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