@lenylvt/pi-tui 0.64.0

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 (127) 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 +623 -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 +244 -0
  15. package/dist/components/editor.d.ts.map +1 -0
  16. package/dist/components/editor.js +1861 -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 +660 -0
  33. package/dist/components/markdown.js.map +1 -0
  34. package/dist/components/select-list.d.ts +50 -0
  35. package/dist/components/select-list.d.ts.map +1 -0
  36. package/dist/components/select-list.js +159 -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 +193 -0
  67. package/dist/keybindings.d.ts.map +1 -0
  68. package/dist/keybindings.js +174 -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 +1124 -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 +285 -0
  89. package/dist/terminal.js.map +1 -0
  90. package/dist/tui.d.ts +218 -0
  91. package/dist/tui.d.ts.map +1 -0
  92. package/dist/tui.js +966 -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 +960 -0
  101. package/dist/utils.js.map +1 -0
  102. package/package.json +55 -0
  103. package/src/autocomplete.ts +771 -0
  104. package/src/components/box.ts +137 -0
  105. package/src/components/cancellable-loader.ts +40 -0
  106. package/src/components/editor.ts +2230 -0
  107. package/src/components/image.ts +104 -0
  108. package/src/components/input.ts +503 -0
  109. package/src/components/loader.ts +55 -0
  110. package/src/components/markdown.ts +820 -0
  111. package/src/components/select-list.ts +229 -0
  112. package/src/components/settings-list.ts +250 -0
  113. package/src/components/spacer.ts +28 -0
  114. package/src/components/text.ts +106 -0
  115. package/src/components/truncated-text.ts +65 -0
  116. package/src/editor-component.ts +74 -0
  117. package/src/fuzzy.ts +133 -0
  118. package/src/index.ts +104 -0
  119. package/src/keybindings.ts +244 -0
  120. package/src/keys.ts +1356 -0
  121. package/src/kill-ring.ts +46 -0
  122. package/src/stdin-buffer.ts +386 -0
  123. package/src/terminal-image.ts +381 -0
  124. package/src/terminal.ts +360 -0
  125. package/src/tui.ts +1200 -0
  126. package/src/undo-stack.ts +28 -0
  127. package/src/utils.ts +1068 -0
package/src/keys.ts ADDED
@@ -0,0 +1,1356 @@
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
+ // =============================================================================
22
+ // Global Kitty Protocol State
23
+ // =============================================================================
24
+
25
+ let _kittyProtocolActive = false;
26
+
27
+ /**
28
+ * Set the global Kitty keyboard protocol state.
29
+ * Called by ProcessTerminal after detecting protocol support.
30
+ */
31
+ export function setKittyProtocolActive(active: boolean): void {
32
+ _kittyProtocolActive = active;
33
+ }
34
+
35
+ /**
36
+ * Query whether Kitty keyboard protocol is currently active.
37
+ */
38
+ export function isKittyProtocolActive(): boolean {
39
+ return _kittyProtocolActive;
40
+ }
41
+
42
+ // =============================================================================
43
+ // Type-Safe Key Identifiers
44
+ // =============================================================================
45
+
46
+ type Letter =
47
+ | "a"
48
+ | "b"
49
+ | "c"
50
+ | "d"
51
+ | "e"
52
+ | "f"
53
+ | "g"
54
+ | "h"
55
+ | "i"
56
+ | "j"
57
+ | "k"
58
+ | "l"
59
+ | "m"
60
+ | "n"
61
+ | "o"
62
+ | "p"
63
+ | "q"
64
+ | "r"
65
+ | "s"
66
+ | "t"
67
+ | "u"
68
+ | "v"
69
+ | "w"
70
+ | "x"
71
+ | "y"
72
+ | "z";
73
+
74
+ type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
75
+
76
+ type SymbolKey =
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
+ type SpecialKey =
110
+ | "escape"
111
+ | "esc"
112
+ | "enter"
113
+ | "return"
114
+ | "tab"
115
+ | "space"
116
+ | "backspace"
117
+ | "delete"
118
+ | "insert"
119
+ | "clear"
120
+ | "home"
121
+ | "end"
122
+ | "pageUp"
123
+ | "pageDown"
124
+ | "up"
125
+ | "down"
126
+ | "left"
127
+ | "right"
128
+ | "f1"
129
+ | "f2"
130
+ | "f3"
131
+ | "f4"
132
+ | "f5"
133
+ | "f6"
134
+ | "f7"
135
+ | "f8"
136
+ | "f9"
137
+ | "f10"
138
+ | "f11"
139
+ | "f12";
140
+
141
+ type BaseKey = Letter | Digit | SymbolKey | SpecialKey;
142
+
143
+ /**
144
+ * Union type of all valid key identifiers.
145
+ * Provides autocomplete and catches typos at compile time.
146
+ */
147
+ export type KeyId =
148
+ | BaseKey
149
+ | `ctrl+${BaseKey}`
150
+ | `shift+${BaseKey}`
151
+ | `alt+${BaseKey}`
152
+ | `ctrl+shift+${BaseKey}`
153
+ | `shift+ctrl+${BaseKey}`
154
+ | `ctrl+alt+${BaseKey}`
155
+ | `alt+ctrl+${BaseKey}`
156
+ | `shift+alt+${BaseKey}`
157
+ | `alt+shift+${BaseKey}`
158
+ | `ctrl+shift+alt+${BaseKey}`
159
+ | `ctrl+alt+shift+${BaseKey}`
160
+ | `shift+ctrl+alt+${BaseKey}`
161
+ | `shift+alt+ctrl+${BaseKey}`
162
+ | `alt+ctrl+shift+${BaseKey}`
163
+ | `alt+shift+ctrl+${BaseKey}`;
164
+
165
+ /**
166
+ * Helper object for creating typed key identifiers with autocomplete.
167
+ *
168
+ * Usage:
169
+ * - Key.escape, Key.enter, Key.tab, etc. for special keys
170
+ * - Key.backtick, Key.comma, Key.period, etc. for symbol keys
171
+ * - Key.ctrl("c"), Key.alt("x") for single modifier
172
+ * - Key.ctrlShift("p"), Key.ctrlAlt("x") for combined modifiers
173
+ */
174
+ export const Key = {
175
+ // Special keys
176
+ escape: "escape" as const,
177
+ esc: "esc" as const,
178
+ enter: "enter" as const,
179
+ return: "return" as const,
180
+ tab: "tab" as const,
181
+ space: "space" as const,
182
+ backspace: "backspace" as const,
183
+ delete: "delete" as const,
184
+ insert: "insert" as const,
185
+ clear: "clear" as const,
186
+ home: "home" as const,
187
+ end: "end" as const,
188
+ pageUp: "pageUp" as const,
189
+ pageDown: "pageDown" as const,
190
+ up: "up" as const,
191
+ down: "down" as const,
192
+ left: "left" as const,
193
+ right: "right" as const,
194
+ f1: "f1" as const,
195
+ f2: "f2" as const,
196
+ f3: "f3" as const,
197
+ f4: "f4" as const,
198
+ f5: "f5" as const,
199
+ f6: "f6" as const,
200
+ f7: "f7" as const,
201
+ f8: "f8" as const,
202
+ f9: "f9" as const,
203
+ f10: "f10" as const,
204
+ f11: "f11" as const,
205
+ f12: "f12" as const,
206
+
207
+ // Symbol keys
208
+ backtick: "`" as const,
209
+ hyphen: "-" as const,
210
+ equals: "=" as const,
211
+ leftbracket: "[" as const,
212
+ rightbracket: "]" as const,
213
+ backslash: "\\" as const,
214
+ semicolon: ";" as const,
215
+ quote: "'" as const,
216
+ comma: "," as const,
217
+ period: "." as const,
218
+ slash: "/" as const,
219
+ exclamation: "!" as const,
220
+ at: "@" as const,
221
+ hash: "#" as const,
222
+ dollar: "$" as const,
223
+ percent: "%" as const,
224
+ caret: "^" as const,
225
+ ampersand: "&" as const,
226
+ asterisk: "*" as const,
227
+ leftparen: "(" as const,
228
+ rightparen: ")" as const,
229
+ underscore: "_" as const,
230
+ plus: "+" as const,
231
+ pipe: "|" as const,
232
+ tilde: "~" as const,
233
+ leftbrace: "{" as const,
234
+ rightbrace: "}" as const,
235
+ colon: ":" as const,
236
+ lessthan: "<" as const,
237
+ greaterthan: ">" as const,
238
+ question: "?" as const,
239
+
240
+ // Single modifiers
241
+ ctrl: <K extends BaseKey>(key: K): `ctrl+${K}` => `ctrl+${key}`,
242
+ shift: <K extends BaseKey>(key: K): `shift+${K}` => `shift+${key}`,
243
+ alt: <K extends BaseKey>(key: K): `alt+${K}` => `alt+${key}`,
244
+
245
+ // Combined modifiers
246
+ ctrlShift: <K extends BaseKey>(key: K): `ctrl+shift+${K}` => `ctrl+shift+${key}`,
247
+ shiftCtrl: <K extends BaseKey>(key: K): `shift+ctrl+${K}` => `shift+ctrl+${key}`,
248
+ ctrlAlt: <K extends BaseKey>(key: K): `ctrl+alt+${K}` => `ctrl+alt+${key}`,
249
+ altCtrl: <K extends BaseKey>(key: K): `alt+ctrl+${K}` => `alt+ctrl+${key}`,
250
+ shiftAlt: <K extends BaseKey>(key: K): `shift+alt+${K}` => `shift+alt+${key}`,
251
+ altShift: <K extends BaseKey>(key: K): `alt+shift+${K}` => `alt+shift+${key}`,
252
+
253
+ // Triple modifiers
254
+ ctrlShiftAlt: <K extends BaseKey>(key: K): `ctrl+shift+alt+${K}` => `ctrl+shift+alt+${key}`,
255
+ } as const;
256
+
257
+ // =============================================================================
258
+ // Constants
259
+ // =============================================================================
260
+
261
+ const SYMBOL_KEYS = new Set([
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
+ const MODIFIERS = {
296
+ shift: 1,
297
+ alt: 2,
298
+ ctrl: 4,
299
+ } as const;
300
+
301
+ const LOCK_MASK = 64 + 128; // Caps Lock + Num Lock
302
+
303
+ const CODEPOINTS = {
304
+ escape: 27,
305
+ tab: 9,
306
+ enter: 13,
307
+ space: 32,
308
+ backspace: 127,
309
+ kpEnter: 57414, // Numpad Enter (Kitty protocol)
310
+ } as const;
311
+
312
+ const ARROW_CODEPOINTS = {
313
+ up: -1,
314
+ down: -2,
315
+ right: -3,
316
+ left: -4,
317
+ } as const;
318
+
319
+ const FUNCTIONAL_CODEPOINTS = {
320
+ delete: -10,
321
+ insert: -11,
322
+ pageUp: -12,
323
+ pageDown: -13,
324
+ home: -14,
325
+ end: -15,
326
+ } as const;
327
+
328
+ const KITTY_FUNCTIONAL_KEY_EQUIVALENTS = new Map<number, number>([
329
+ [57399, 48], // KP_0 -> 0
330
+ [57400, 49], // KP_1 -> 1
331
+ [57401, 50], // KP_2 -> 2
332
+ [57402, 51], // KP_3 -> 3
333
+ [57403, 52], // KP_4 -> 4
334
+ [57404, 53], // KP_5 -> 5
335
+ [57405, 54], // KP_6 -> 6
336
+ [57406, 55], // KP_7 -> 7
337
+ [57407, 56], // KP_8 -> 8
338
+ [57408, 57], // KP_9 -> 9
339
+ [57409, 46], // KP_DECIMAL -> .
340
+ [57410, 47], // KP_DIVIDE -> /
341
+ [57411, 42], // KP_MULTIPLY -> *
342
+ [57412, 45], // KP_SUBTRACT -> -
343
+ [57413, 43], // KP_ADD -> +
344
+ [57415, 61], // KP_EQUAL -> =
345
+ [57416, 44], // KP_SEPARATOR -> ,
346
+ [57417, ARROW_CODEPOINTS.left],
347
+ [57418, ARROW_CODEPOINTS.right],
348
+ [57419, ARROW_CODEPOINTS.up],
349
+ [57420, ARROW_CODEPOINTS.down],
350
+ [57421, FUNCTIONAL_CODEPOINTS.pageUp],
351
+ [57422, FUNCTIONAL_CODEPOINTS.pageDown],
352
+ [57423, FUNCTIONAL_CODEPOINTS.home],
353
+ [57424, FUNCTIONAL_CODEPOINTS.end],
354
+ [57425, FUNCTIONAL_CODEPOINTS.insert],
355
+ [57426, FUNCTIONAL_CODEPOINTS.delete],
356
+ ]);
357
+
358
+ function normalizeKittyFunctionalCodepoint(codepoint: number): number {
359
+ return KITTY_FUNCTIONAL_KEY_EQUIVALENTS.get(codepoint) ?? codepoint;
360
+ }
361
+
362
+ const LEGACY_KEY_SEQUENCES = {
363
+ up: ["\x1b[A", "\x1bOA"],
364
+ down: ["\x1b[B", "\x1bOB"],
365
+ right: ["\x1b[C", "\x1bOC"],
366
+ left: ["\x1b[D", "\x1bOD"],
367
+ home: ["\x1b[H", "\x1bOH", "\x1b[1~", "\x1b[7~"],
368
+ end: ["\x1b[F", "\x1bOF", "\x1b[4~", "\x1b[8~"],
369
+ insert: ["\x1b[2~"],
370
+ delete: ["\x1b[3~"],
371
+ pageUp: ["\x1b[5~", "\x1b[[5~"],
372
+ pageDown: ["\x1b[6~", "\x1b[[6~"],
373
+ clear: ["\x1b[E", "\x1bOE"],
374
+ f1: ["\x1bOP", "\x1b[11~", "\x1b[[A"],
375
+ f2: ["\x1bOQ", "\x1b[12~", "\x1b[[B"],
376
+ f3: ["\x1bOR", "\x1b[13~", "\x1b[[C"],
377
+ f4: ["\x1bOS", "\x1b[14~", "\x1b[[D"],
378
+ f5: ["\x1b[15~", "\x1b[[E"],
379
+ f6: ["\x1b[17~"],
380
+ f7: ["\x1b[18~"],
381
+ f8: ["\x1b[19~"],
382
+ f9: ["\x1b[20~"],
383
+ f10: ["\x1b[21~"],
384
+ f11: ["\x1b[23~"],
385
+ f12: ["\x1b[24~"],
386
+ } as const;
387
+
388
+ const LEGACY_SHIFT_SEQUENCES = {
389
+ up: ["\x1b[a"],
390
+ down: ["\x1b[b"],
391
+ right: ["\x1b[c"],
392
+ left: ["\x1b[d"],
393
+ clear: ["\x1b[e"],
394
+ insert: ["\x1b[2$"],
395
+ delete: ["\x1b[3$"],
396
+ pageUp: ["\x1b[5$"],
397
+ pageDown: ["\x1b[6$"],
398
+ home: ["\x1b[7$"],
399
+ end: ["\x1b[8$"],
400
+ } as const;
401
+
402
+ const LEGACY_CTRL_SEQUENCES = {
403
+ up: ["\x1bOa"],
404
+ down: ["\x1bOb"],
405
+ right: ["\x1bOc"],
406
+ left: ["\x1bOd"],
407
+ clear: ["\x1bOe"],
408
+ insert: ["\x1b[2^"],
409
+ delete: ["\x1b[3^"],
410
+ pageUp: ["\x1b[5^"],
411
+ pageDown: ["\x1b[6^"],
412
+ home: ["\x1b[7^"],
413
+ end: ["\x1b[8^"],
414
+ } as const;
415
+
416
+ const LEGACY_SEQUENCE_KEY_IDS: Record<string, KeyId> = {
417
+ "\x1bOA": "up",
418
+ "\x1bOB": "down",
419
+ "\x1bOC": "right",
420
+ "\x1bOD": "left",
421
+ "\x1bOH": "home",
422
+ "\x1bOF": "end",
423
+ "\x1b[E": "clear",
424
+ "\x1bOE": "clear",
425
+ "\x1bOe": "ctrl+clear",
426
+ "\x1b[e": "shift+clear",
427
+ "\x1b[2~": "insert",
428
+ "\x1b[2$": "shift+insert",
429
+ "\x1b[2^": "ctrl+insert",
430
+ "\x1b[3$": "shift+delete",
431
+ "\x1b[3^": "ctrl+delete",
432
+ "\x1b[[5~": "pageUp",
433
+ "\x1b[[6~": "pageDown",
434
+ "\x1b[a": "shift+up",
435
+ "\x1b[b": "shift+down",
436
+ "\x1b[c": "shift+right",
437
+ "\x1b[d": "shift+left",
438
+ "\x1bOa": "ctrl+up",
439
+ "\x1bOb": "ctrl+down",
440
+ "\x1bOc": "ctrl+right",
441
+ "\x1bOd": "ctrl+left",
442
+ "\x1b[5$": "shift+pageUp",
443
+ "\x1b[6$": "shift+pageDown",
444
+ "\x1b[7$": "shift+home",
445
+ "\x1b[8$": "shift+end",
446
+ "\x1b[5^": "ctrl+pageUp",
447
+ "\x1b[6^": "ctrl+pageDown",
448
+ "\x1b[7^": "ctrl+home",
449
+ "\x1b[8^": "ctrl+end",
450
+ "\x1bOP": "f1",
451
+ "\x1bOQ": "f2",
452
+ "\x1bOR": "f3",
453
+ "\x1bOS": "f4",
454
+ "\x1b[11~": "f1",
455
+ "\x1b[12~": "f2",
456
+ "\x1b[13~": "f3",
457
+ "\x1b[14~": "f4",
458
+ "\x1b[[A": "f1",
459
+ "\x1b[[B": "f2",
460
+ "\x1b[[C": "f3",
461
+ "\x1b[[D": "f4",
462
+ "\x1b[[E": "f5",
463
+ "\x1b[15~": "f5",
464
+ "\x1b[17~": "f6",
465
+ "\x1b[18~": "f7",
466
+ "\x1b[19~": "f8",
467
+ "\x1b[20~": "f9",
468
+ "\x1b[21~": "f10",
469
+ "\x1b[23~": "f11",
470
+ "\x1b[24~": "f12",
471
+ "\x1bb": "alt+left",
472
+ "\x1bf": "alt+right",
473
+ "\x1bp": "alt+up",
474
+ "\x1bn": "alt+down",
475
+ } as const;
476
+
477
+ type LegacyModifierKey = keyof typeof LEGACY_SHIFT_SEQUENCES;
478
+
479
+ const matchesLegacySequence = (data: string, sequences: readonly string[]): boolean => sequences.includes(data);
480
+
481
+ const matchesLegacyModifierSequence = (data: string, key: LegacyModifierKey, modifier: number): boolean => {
482
+ if (modifier === MODIFIERS.shift) {
483
+ return matchesLegacySequence(data, LEGACY_SHIFT_SEQUENCES[key]);
484
+ }
485
+ if (modifier === MODIFIERS.ctrl) {
486
+ return matchesLegacySequence(data, LEGACY_CTRL_SEQUENCES[key]);
487
+ }
488
+ return false;
489
+ };
490
+
491
+ // =============================================================================
492
+ // Kitty Protocol Parsing
493
+ // =============================================================================
494
+
495
+ /**
496
+ * Event types from Kitty keyboard protocol (flag 2)
497
+ * 1 = key press, 2 = key repeat, 3 = key release
498
+ */
499
+ export type KeyEventType = "press" | "repeat" | "release";
500
+
501
+ interface ParsedKittySequence {
502
+ codepoint: number;
503
+ shiftedKey?: number; // Shifted version of the key (when shift is pressed)
504
+ baseLayoutKey?: number; // Key in standard PC-101 layout (for non-Latin layouts)
505
+ modifier: number;
506
+ eventType: KeyEventType;
507
+ }
508
+
509
+ interface ParsedModifyOtherKeysSequence {
510
+ codepoint: number;
511
+ modifier: number;
512
+ }
513
+
514
+ // Store the last parsed event type for isKeyRelease() to query
515
+ let _lastEventType: KeyEventType = "press";
516
+
517
+ /**
518
+ * Check if the last parsed key event was a key release.
519
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
520
+ */
521
+ export function isKeyRelease(data: string): boolean {
522
+ // Don't treat bracketed paste content as key release, even if it contains
523
+ // patterns like ":3F" (e.g., bluetooth MAC addresses like "90:62:3F:A5").
524
+ // Terminal.ts re-wraps paste content with bracketed paste markers before
525
+ // passing to TUI, so pasted data will always contain \x1b[200~.
526
+ if (data.includes("\x1b[200~")) {
527
+ return false;
528
+ }
529
+
530
+ // Quick check: release events with flag 2 contain ":3"
531
+ // Format: \x1b[<codepoint>;<modifier>:3u
532
+ if (
533
+ data.includes(":3u") ||
534
+ data.includes(":3~") ||
535
+ data.includes(":3A") ||
536
+ data.includes(":3B") ||
537
+ data.includes(":3C") ||
538
+ data.includes(":3D") ||
539
+ data.includes(":3H") ||
540
+ data.includes(":3F")
541
+ ) {
542
+ return true;
543
+ }
544
+ return false;
545
+ }
546
+
547
+ /**
548
+ * Check if the last parsed key event was a key repeat.
549
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
550
+ */
551
+ export function isKeyRepeat(data: string): boolean {
552
+ // Don't treat bracketed paste content as key repeat, even if it contains
553
+ // patterns like ":2F". See isKeyRelease() for details.
554
+ if (data.includes("\x1b[200~")) {
555
+ return false;
556
+ }
557
+
558
+ if (
559
+ data.includes(":2u") ||
560
+ data.includes(":2~") ||
561
+ data.includes(":2A") ||
562
+ data.includes(":2B") ||
563
+ data.includes(":2C") ||
564
+ data.includes(":2D") ||
565
+ data.includes(":2H") ||
566
+ data.includes(":2F")
567
+ ) {
568
+ return true;
569
+ }
570
+ return false;
571
+ }
572
+
573
+ function parseEventType(eventTypeStr: string | undefined): KeyEventType {
574
+ if (!eventTypeStr) return "press";
575
+ const eventType = parseInt(eventTypeStr, 10);
576
+ if (eventType === 2) return "repeat";
577
+ if (eventType === 3) return "release";
578
+ return "press";
579
+ }
580
+
581
+ function parseKittySequence(data: string): ParsedKittySequence | null {
582
+ // CSI u format with alternate keys (flag 4):
583
+ // \x1b[<codepoint>u
584
+ // \x1b[<codepoint>;<mod>u
585
+ // \x1b[<codepoint>;<mod>:<event>u
586
+ // \x1b[<codepoint>:<shifted>;<mod>u
587
+ // \x1b[<codepoint>:<shifted>:<base>;<mod>u
588
+ // \x1b[<codepoint>::<base>;<mod>u (no shifted key, only base)
589
+ //
590
+ // With flag 2, event type is appended after modifier colon: 1=press, 2=repeat, 3=release
591
+ // With flag 4, alternate keys are appended after codepoint with colons
592
+ const csiUMatch = data.match(/^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/);
593
+ if (csiUMatch) {
594
+ const codepoint = parseInt(csiUMatch[1]!, 10);
595
+ const shiftedKey = csiUMatch[2] && csiUMatch[2].length > 0 ? parseInt(csiUMatch[2], 10) : undefined;
596
+ const baseLayoutKey = csiUMatch[3] ? parseInt(csiUMatch[3], 10) : undefined;
597
+ const modValue = csiUMatch[4] ? parseInt(csiUMatch[4], 10) : 1;
598
+ const eventType = parseEventType(csiUMatch[5]);
599
+ _lastEventType = eventType;
600
+ return { codepoint, shiftedKey, baseLayoutKey, modifier: modValue - 1, eventType };
601
+ }
602
+
603
+ // Arrow keys with modifier: \x1b[1;<mod>A/B/C/D or \x1b[1;<mod>:<event>A/B/C/D
604
+ const arrowMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([ABCD])$/);
605
+ if (arrowMatch) {
606
+ const modValue = parseInt(arrowMatch[1]!, 10);
607
+ const eventType = parseEventType(arrowMatch[2]);
608
+ const arrowCodes: Record<string, number> = { A: -1, B: -2, C: -3, D: -4 };
609
+ _lastEventType = eventType;
610
+ return { codepoint: arrowCodes[arrowMatch[3]!]!, modifier: modValue - 1, eventType };
611
+ }
612
+
613
+ // Functional keys: \x1b[<num>~ or \x1b[<num>;<mod>~ or \x1b[<num>;<mod>:<event>~
614
+ const funcMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?(?::(\d+))?~$/);
615
+ if (funcMatch) {
616
+ const keyNum = parseInt(funcMatch[1]!, 10);
617
+ const modValue = funcMatch[2] ? parseInt(funcMatch[2], 10) : 1;
618
+ const eventType = parseEventType(funcMatch[3]);
619
+ const funcCodes: Record<number, number> = {
620
+ 2: FUNCTIONAL_CODEPOINTS.insert,
621
+ 3: FUNCTIONAL_CODEPOINTS.delete,
622
+ 5: FUNCTIONAL_CODEPOINTS.pageUp,
623
+ 6: FUNCTIONAL_CODEPOINTS.pageDown,
624
+ 7: FUNCTIONAL_CODEPOINTS.home,
625
+ 8: FUNCTIONAL_CODEPOINTS.end,
626
+ };
627
+ const codepoint = funcCodes[keyNum];
628
+ if (codepoint !== undefined) {
629
+ _lastEventType = eventType;
630
+ return { codepoint, modifier: modValue - 1, eventType };
631
+ }
632
+ }
633
+
634
+ // Home/End with modifier: \x1b[1;<mod>H/F or \x1b[1;<mod>:<event>H/F
635
+ const homeEndMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([HF])$/);
636
+ if (homeEndMatch) {
637
+ const modValue = parseInt(homeEndMatch[1]!, 10);
638
+ const eventType = parseEventType(homeEndMatch[2]);
639
+ const codepoint = homeEndMatch[3] === "H" ? FUNCTIONAL_CODEPOINTS.home : FUNCTIONAL_CODEPOINTS.end;
640
+ _lastEventType = eventType;
641
+ return { codepoint, modifier: modValue - 1, eventType };
642
+ }
643
+
644
+ return null;
645
+ }
646
+
647
+ function matchesKittySequence(data: string, expectedCodepoint: number, expectedModifier: number): boolean {
648
+ const parsed = parseKittySequence(data);
649
+ if (!parsed) return false;
650
+ const actualMod = parsed.modifier & ~LOCK_MASK;
651
+ const expectedMod = expectedModifier & ~LOCK_MASK;
652
+
653
+ // Check if modifiers match
654
+ if (actualMod !== expectedMod) return false;
655
+
656
+ const normalizedCodepoint = normalizeKittyFunctionalCodepoint(parsed.codepoint);
657
+ const normalizedExpectedCodepoint = normalizeKittyFunctionalCodepoint(expectedCodepoint);
658
+
659
+ // Primary match: codepoint matches directly after normalizing functional keys
660
+ if (normalizedCodepoint === normalizedExpectedCodepoint) return true;
661
+
662
+ // Alternate match: use base layout key for non-Latin keyboard layouts.
663
+ // This allows Ctrl+С (Cyrillic) to match Ctrl+c (Latin) when terminal reports
664
+ // the base layout key (the key in standard PC-101 layout).
665
+ //
666
+ // Only fall back to base layout key when the codepoint is NOT already a
667
+ // recognized Latin letter (a-z) or symbol (e.g., /, -, [, ;, etc.).
668
+ // When the codepoint is a recognized key, it is authoritative regardless
669
+ // of physical key position. This prevents remapped layouts (Dvorak, Colemak,
670
+ // xremap, etc.) from causing false matches: both letters and symbols move
671
+ // to different physical positions, so Ctrl+K could falsely match Ctrl+V
672
+ // (letter remapping) and Ctrl+/ could falsely match Ctrl+[ (symbol remapping)
673
+ // if the base layout key were always considered.
674
+ if (parsed.baseLayoutKey !== undefined && parsed.baseLayoutKey === expectedCodepoint) {
675
+ const cp = normalizedCodepoint;
676
+ const isLatinLetter = cp >= 97 && cp <= 122; // a-z
677
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(cp));
678
+ if (!isLatinLetter && !isKnownSymbol) return true;
679
+ }
680
+
681
+ return false;
682
+ }
683
+
684
+ function parseModifyOtherKeysSequence(data: string): ParsedModifyOtherKeysSequence | null {
685
+ const match = data.match(/^\x1b\[27;(\d+);(\d+)~$/);
686
+ if (!match) return null;
687
+ const modValue = parseInt(match[1]!, 10);
688
+ const codepoint = parseInt(match[2]!, 10);
689
+ return { codepoint, modifier: modValue - 1 };
690
+ }
691
+
692
+ /**
693
+ * Match xterm modifyOtherKeys format: CSI 27 ; modifiers ; keycode ~
694
+ * This is used by terminals when Kitty protocol is not enabled.
695
+ * Modifier values are 1-indexed: 2=shift, 3=alt, 5=ctrl, etc.
696
+ */
697
+ function matchesModifyOtherKeys(data: string, expectedKeycode: number, expectedModifier: number): boolean {
698
+ const parsed = parseModifyOtherKeysSequence(data);
699
+ if (!parsed) return false;
700
+ return parsed.codepoint === expectedKeycode && parsed.modifier === expectedModifier;
701
+ }
702
+
703
+ function isWindowsTerminalSession(): boolean {
704
+ return (
705
+ Boolean(process.env.WT_SESSION) && !process.env.SSH_CONNECTION && !process.env.SSH_CLIENT && !process.env.SSH_TTY
706
+ );
707
+ }
708
+
709
+ /**
710
+ * Raw 0x08 (BS) is ambiguous in legacy terminals.
711
+ *
712
+ * - Windows Terminal uses it for Ctrl+Backspace.
713
+ * - Some legacy terminals and tmux setups send it for plain Backspace.
714
+ *
715
+ * Prefer explicit Kitty / CSI-u / modifyOtherKeys sequences whenever they are
716
+ * available. Fall back to a Windows Terminal heuristic only for raw BS bytes.
717
+ */
718
+ function matchesRawBackspace(data: string, expectedModifier: number): boolean {
719
+ if (data === "\x7f") return expectedModifier === 0;
720
+ if (data !== "\x08") return false;
721
+ return isWindowsTerminalSession() ? expectedModifier === MODIFIERS.ctrl : expectedModifier === 0;
722
+ }
723
+
724
+ // =============================================================================
725
+ // Generic Key Matching
726
+ // =============================================================================
727
+
728
+ /**
729
+ * Get the control character for a key.
730
+ * Uses the universal formula: code & 0x1f (mask to lower 5 bits)
731
+ *
732
+ * Works for:
733
+ * - Letters a-z → 1-26
734
+ * - Symbols [\]_ → 27, 28, 29, 31
735
+ * - Also maps - to same as _ (same physical key on US keyboards)
736
+ */
737
+ function rawCtrlChar(key: string): string | null {
738
+ const char = key.toLowerCase();
739
+ const code = char.charCodeAt(0);
740
+ if ((code >= 97 && code <= 122) || char === "[" || char === "\\" || char === "]" || char === "_") {
741
+ return String.fromCharCode(code & 0x1f);
742
+ }
743
+ // Handle - as _ (same physical key on US keyboards)
744
+ if (char === "-") {
745
+ return String.fromCharCode(31); // Same as Ctrl+_
746
+ }
747
+ return null;
748
+ }
749
+
750
+ function isDigitKey(key: string): boolean {
751
+ return key >= "0" && key <= "9";
752
+ }
753
+
754
+ function matchesPrintableModifyOtherKeys(data: string, expectedKeycode: number, expectedModifier: number): boolean {
755
+ if (expectedModifier === 0) return false;
756
+ return matchesModifyOtherKeys(data, expectedKeycode, expectedModifier);
757
+ }
758
+
759
+ function formatKeyNameWithModifiers(keyName: string, modifier: number): string | undefined {
760
+ const mods: string[] = [];
761
+ const effectiveMod = modifier & ~LOCK_MASK;
762
+ const supportedModifierMask = MODIFIERS.shift | MODIFIERS.ctrl | MODIFIERS.alt;
763
+ if ((effectiveMod & ~supportedModifierMask) !== 0) return undefined;
764
+ if (effectiveMod & MODIFIERS.shift) mods.push("shift");
765
+ if (effectiveMod & MODIFIERS.ctrl) mods.push("ctrl");
766
+ if (effectiveMod & MODIFIERS.alt) mods.push("alt");
767
+ return mods.length > 0 ? `${mods.join("+")}+${keyName}` : keyName;
768
+ }
769
+
770
+ function parseKeyId(keyId: string): { key: string; ctrl: boolean; shift: boolean; alt: boolean } | null {
771
+ const parts = keyId.toLowerCase().split("+");
772
+ const key = parts[parts.length - 1];
773
+ if (!key) return null;
774
+ return {
775
+ key,
776
+ ctrl: parts.includes("ctrl"),
777
+ shift: parts.includes("shift"),
778
+ alt: parts.includes("alt"),
779
+ };
780
+ }
781
+
782
+ /**
783
+ * Match input data against a key identifier string.
784
+ *
785
+ * Supported key identifiers:
786
+ * - Single keys: "escape", "tab", "enter", "backspace", "delete", "home", "end", "space"
787
+ * - Arrow keys: "up", "down", "left", "right"
788
+ * - Ctrl combinations: "ctrl+c", "ctrl+z", etc.
789
+ * - Shift combinations: "shift+tab", "shift+enter"
790
+ * - Alt combinations: "alt+enter", "alt+backspace"
791
+ * - Combined modifiers: "shift+ctrl+p", "ctrl+alt+x"
792
+ *
793
+ * Use the Key helper for autocomplete: Key.ctrl("c"), Key.escape, Key.ctrlShift("p")
794
+ *
795
+ * @param data - Raw input data from terminal
796
+ * @param keyId - Key identifier (e.g., "ctrl+c", "escape", Key.ctrl("c"))
797
+ */
798
+ export function matchesKey(data: string, keyId: KeyId): boolean {
799
+ const parsed = parseKeyId(keyId);
800
+ if (!parsed) return false;
801
+
802
+ const { key, ctrl, shift, alt } = parsed;
803
+ let modifier = 0;
804
+ if (shift) modifier |= MODIFIERS.shift;
805
+ if (alt) modifier |= MODIFIERS.alt;
806
+ if (ctrl) modifier |= MODIFIERS.ctrl;
807
+
808
+ switch (key) {
809
+ case "escape":
810
+ case "esc":
811
+ if (modifier !== 0) return false;
812
+ return (
813
+ data === "\x1b" ||
814
+ matchesKittySequence(data, CODEPOINTS.escape, 0) ||
815
+ matchesModifyOtherKeys(data, CODEPOINTS.escape, 0)
816
+ );
817
+
818
+ case "space":
819
+ if (!_kittyProtocolActive) {
820
+ if (ctrl && !alt && !shift && data === "\x00") {
821
+ return true;
822
+ }
823
+ if (alt && !ctrl && !shift && data === "\x1b ") {
824
+ return true;
825
+ }
826
+ }
827
+ if (modifier === 0) {
828
+ return (
829
+ data === " " ||
830
+ matchesKittySequence(data, CODEPOINTS.space, 0) ||
831
+ matchesModifyOtherKeys(data, CODEPOINTS.space, 0)
832
+ );
833
+ }
834
+ return (
835
+ matchesKittySequence(data, CODEPOINTS.space, modifier) ||
836
+ matchesModifyOtherKeys(data, CODEPOINTS.space, modifier)
837
+ );
838
+
839
+ case "tab":
840
+ if (shift && !ctrl && !alt) {
841
+ return (
842
+ data === "\x1b[Z" ||
843
+ matchesKittySequence(data, CODEPOINTS.tab, MODIFIERS.shift) ||
844
+ matchesModifyOtherKeys(data, CODEPOINTS.tab, MODIFIERS.shift)
845
+ );
846
+ }
847
+ if (modifier === 0) {
848
+ return data === "\t" || matchesKittySequence(data, CODEPOINTS.tab, 0);
849
+ }
850
+ return (
851
+ matchesKittySequence(data, CODEPOINTS.tab, modifier) ||
852
+ matchesModifyOtherKeys(data, CODEPOINTS.tab, modifier)
853
+ );
854
+
855
+ case "enter":
856
+ case "return":
857
+ if (shift && !ctrl && !alt) {
858
+ // CSI u sequences (standard Kitty protocol)
859
+ if (
860
+ matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.shift) ||
861
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.shift)
862
+ ) {
863
+ return true;
864
+ }
865
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
866
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.shift)) {
867
+ return true;
868
+ }
869
+ // When Kitty protocol is active, legacy sequences are custom terminal mappings
870
+ // \x1b\r = Kitty's "map shift+enter send_text all \e\r"
871
+ // \n = Ghostty's "keybind = shift+enter=text:\n"
872
+ if (_kittyProtocolActive) {
873
+ return data === "\x1b\r" || data === "\n";
874
+ }
875
+ return false;
876
+ }
877
+ if (alt && !ctrl && !shift) {
878
+ // CSI u sequences (standard Kitty protocol)
879
+ if (
880
+ matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.alt) ||
881
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.alt)
882
+ ) {
883
+ return true;
884
+ }
885
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
886
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.alt)) {
887
+ return true;
888
+ }
889
+ // \x1b\r is alt+enter only in legacy mode (no Kitty protocol)
890
+ // When Kitty protocol is active, alt+enter comes as CSI u sequence
891
+ if (!_kittyProtocolActive) {
892
+ return data === "\x1b\r";
893
+ }
894
+ return false;
895
+ }
896
+ if (modifier === 0) {
897
+ return (
898
+ data === "\r" ||
899
+ (!_kittyProtocolActive && data === "\n") ||
900
+ data === "\x1bOM" || // SS3 M (numpad enter in some terminals)
901
+ matchesKittySequence(data, CODEPOINTS.enter, 0) ||
902
+ matchesKittySequence(data, CODEPOINTS.kpEnter, 0)
903
+ );
904
+ }
905
+ return (
906
+ matchesKittySequence(data, CODEPOINTS.enter, modifier) ||
907
+ matchesKittySequence(data, CODEPOINTS.kpEnter, modifier) ||
908
+ matchesModifyOtherKeys(data, CODEPOINTS.enter, modifier)
909
+ );
910
+
911
+ case "backspace":
912
+ if (alt && !ctrl && !shift) {
913
+ if (data === "\x1b\x7f" || data === "\x1b\b") {
914
+ return true;
915
+ }
916
+ return (
917
+ matchesKittySequence(data, CODEPOINTS.backspace, MODIFIERS.alt) ||
918
+ matchesModifyOtherKeys(data, CODEPOINTS.backspace, MODIFIERS.alt)
919
+ );
920
+ }
921
+ if (ctrl && !alt && !shift) {
922
+ // Legacy raw 0x08 is ambiguous: it can be Ctrl+Backspace on Windows
923
+ // Terminal or plain Backspace on other terminals, while also
924
+ // overlapping with Ctrl+H.
925
+ if (matchesRawBackspace(data, MODIFIERS.ctrl)) return true;
926
+ return (
927
+ matchesKittySequence(data, CODEPOINTS.backspace, MODIFIERS.ctrl) ||
928
+ matchesModifyOtherKeys(data, CODEPOINTS.backspace, MODIFIERS.ctrl)
929
+ );
930
+ }
931
+ if (modifier === 0) {
932
+ return (
933
+ matchesRawBackspace(data, 0) ||
934
+ matchesKittySequence(data, CODEPOINTS.backspace, 0) ||
935
+ matchesModifyOtherKeys(data, CODEPOINTS.backspace, 0)
936
+ );
937
+ }
938
+ return (
939
+ matchesKittySequence(data, CODEPOINTS.backspace, modifier) ||
940
+ matchesModifyOtherKeys(data, CODEPOINTS.backspace, modifier)
941
+ );
942
+
943
+ case "insert":
944
+ if (modifier === 0) {
945
+ return (
946
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.insert) ||
947
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, 0)
948
+ );
949
+ }
950
+ if (matchesLegacyModifierSequence(data, "insert", modifier)) {
951
+ return true;
952
+ }
953
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, modifier);
954
+
955
+ case "delete":
956
+ if (modifier === 0) {
957
+ return (
958
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.delete) ||
959
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, 0)
960
+ );
961
+ }
962
+ if (matchesLegacyModifierSequence(data, "delete", modifier)) {
963
+ return true;
964
+ }
965
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, modifier);
966
+
967
+ case "clear":
968
+ if (modifier === 0) {
969
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.clear);
970
+ }
971
+ return matchesLegacyModifierSequence(data, "clear", modifier);
972
+
973
+ case "home":
974
+ if (modifier === 0) {
975
+ return (
976
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.home) ||
977
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, 0)
978
+ );
979
+ }
980
+ if (matchesLegacyModifierSequence(data, "home", modifier)) {
981
+ return true;
982
+ }
983
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, modifier);
984
+
985
+ case "end":
986
+ if (modifier === 0) {
987
+ return (
988
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.end) ||
989
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, 0)
990
+ );
991
+ }
992
+ if (matchesLegacyModifierSequence(data, "end", modifier)) {
993
+ return true;
994
+ }
995
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, modifier);
996
+
997
+ case "pageup":
998
+ if (modifier === 0) {
999
+ return (
1000
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageUp) ||
1001
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, 0)
1002
+ );
1003
+ }
1004
+ if (matchesLegacyModifierSequence(data, "pageUp", modifier)) {
1005
+ return true;
1006
+ }
1007
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, modifier);
1008
+
1009
+ case "pagedown":
1010
+ if (modifier === 0) {
1011
+ return (
1012
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageDown) ||
1013
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, 0)
1014
+ );
1015
+ }
1016
+ if (matchesLegacyModifierSequence(data, "pageDown", modifier)) {
1017
+ return true;
1018
+ }
1019
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, modifier);
1020
+
1021
+ case "up":
1022
+ if (alt && !ctrl && !shift) {
1023
+ return data === "\x1bp" || matchesKittySequence(data, ARROW_CODEPOINTS.up, MODIFIERS.alt);
1024
+ }
1025
+ if (modifier === 0) {
1026
+ return (
1027
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.up) ||
1028
+ matchesKittySequence(data, ARROW_CODEPOINTS.up, 0)
1029
+ );
1030
+ }
1031
+ if (matchesLegacyModifierSequence(data, "up", modifier)) {
1032
+ return true;
1033
+ }
1034
+ return matchesKittySequence(data, ARROW_CODEPOINTS.up, modifier);
1035
+
1036
+ case "down":
1037
+ if (alt && !ctrl && !shift) {
1038
+ return data === "\x1bn" || matchesKittySequence(data, ARROW_CODEPOINTS.down, MODIFIERS.alt);
1039
+ }
1040
+ if (modifier === 0) {
1041
+ return (
1042
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.down) ||
1043
+ matchesKittySequence(data, ARROW_CODEPOINTS.down, 0)
1044
+ );
1045
+ }
1046
+ if (matchesLegacyModifierSequence(data, "down", modifier)) {
1047
+ return true;
1048
+ }
1049
+ return matchesKittySequence(data, ARROW_CODEPOINTS.down, modifier);
1050
+
1051
+ case "left":
1052
+ if (alt && !ctrl && !shift) {
1053
+ return (
1054
+ data === "\x1b[1;3D" ||
1055
+ (!_kittyProtocolActive && data === "\x1bB") ||
1056
+ data === "\x1bb" ||
1057
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.alt)
1058
+ );
1059
+ }
1060
+ if (ctrl && !alt && !shift) {
1061
+ return (
1062
+ data === "\x1b[1;5D" ||
1063
+ matchesLegacyModifierSequence(data, "left", MODIFIERS.ctrl) ||
1064
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.ctrl)
1065
+ );
1066
+ }
1067
+ if (modifier === 0) {
1068
+ return (
1069
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.left) ||
1070
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, 0)
1071
+ );
1072
+ }
1073
+ if (matchesLegacyModifierSequence(data, "left", modifier)) {
1074
+ return true;
1075
+ }
1076
+ return matchesKittySequence(data, ARROW_CODEPOINTS.left, modifier);
1077
+
1078
+ case "right":
1079
+ if (alt && !ctrl && !shift) {
1080
+ return (
1081
+ data === "\x1b[1;3C" ||
1082
+ (!_kittyProtocolActive && data === "\x1bF") ||
1083
+ data === "\x1bf" ||
1084
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.alt)
1085
+ );
1086
+ }
1087
+ if (ctrl && !alt && !shift) {
1088
+ return (
1089
+ data === "\x1b[1;5C" ||
1090
+ matchesLegacyModifierSequence(data, "right", MODIFIERS.ctrl) ||
1091
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.ctrl)
1092
+ );
1093
+ }
1094
+ if (modifier === 0) {
1095
+ return (
1096
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.right) ||
1097
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, 0)
1098
+ );
1099
+ }
1100
+ if (matchesLegacyModifierSequence(data, "right", modifier)) {
1101
+ return true;
1102
+ }
1103
+ return matchesKittySequence(data, ARROW_CODEPOINTS.right, modifier);
1104
+
1105
+ case "f1":
1106
+ case "f2":
1107
+ case "f3":
1108
+ case "f4":
1109
+ case "f5":
1110
+ case "f6":
1111
+ case "f7":
1112
+ case "f8":
1113
+ case "f9":
1114
+ case "f10":
1115
+ case "f11":
1116
+ case "f12": {
1117
+ if (modifier !== 0) {
1118
+ return false;
1119
+ }
1120
+ const functionKey = key as keyof typeof LEGACY_KEY_SEQUENCES;
1121
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES[functionKey]);
1122
+ }
1123
+ }
1124
+
1125
+ // Handle single letter/digit keys and symbols
1126
+ if (key.length === 1 && ((key >= "a" && key <= "z") || isDigitKey(key) || SYMBOL_KEYS.has(key))) {
1127
+ const codepoint = key.charCodeAt(0);
1128
+ const rawCtrl = rawCtrlChar(key);
1129
+ const isLetter = key >= "a" && key <= "z";
1130
+ const isDigit = isDigitKey(key);
1131
+
1132
+ if (ctrl && alt && !shift && !_kittyProtocolActive && rawCtrl) {
1133
+ // Legacy: ctrl+alt+key is ESC followed by the control character
1134
+ return data === `\x1b${rawCtrl}`;
1135
+ }
1136
+
1137
+ if (alt && !ctrl && !shift && !_kittyProtocolActive && (isLetter || isDigit)) {
1138
+ // Legacy: alt+letter/digit is ESC followed by the key
1139
+ if (data === `\x1b${key}`) return true;
1140
+ }
1141
+
1142
+ if (ctrl && !shift && !alt) {
1143
+ // Legacy: ctrl+key sends the control character
1144
+ if (rawCtrl && data === rawCtrl) return true;
1145
+ return (
1146
+ matchesKittySequence(data, codepoint, MODIFIERS.ctrl) ||
1147
+ matchesPrintableModifyOtherKeys(data, codepoint, MODIFIERS.ctrl)
1148
+ );
1149
+ }
1150
+
1151
+ if (ctrl && shift && !alt) {
1152
+ return (
1153
+ matchesKittySequence(data, codepoint, MODIFIERS.shift + MODIFIERS.ctrl) ||
1154
+ matchesPrintableModifyOtherKeys(data, codepoint, MODIFIERS.shift + MODIFIERS.ctrl)
1155
+ );
1156
+ }
1157
+
1158
+ if (shift && !ctrl && !alt) {
1159
+ // Legacy: shift+letter produces uppercase
1160
+ if (isLetter && data === key.toUpperCase()) return true;
1161
+ return (
1162
+ matchesKittySequence(data, codepoint, MODIFIERS.shift) ||
1163
+ matchesPrintableModifyOtherKeys(data, codepoint, MODIFIERS.shift)
1164
+ );
1165
+ }
1166
+
1167
+ if (modifier !== 0) {
1168
+ return (
1169
+ matchesKittySequence(data, codepoint, modifier) ||
1170
+ matchesPrintableModifyOtherKeys(data, codepoint, modifier)
1171
+ );
1172
+ }
1173
+
1174
+ // Check both raw char and Kitty sequence (needed for release events)
1175
+ return data === key || matchesKittySequence(data, codepoint, 0);
1176
+ }
1177
+
1178
+ return false;
1179
+ }
1180
+
1181
+ /**
1182
+ * Parse input data and return the key identifier if recognized.
1183
+ *
1184
+ * @param data - Raw input data from terminal
1185
+ * @returns Key identifier string (e.g., "ctrl+c") or undefined
1186
+ */
1187
+ function formatParsedKey(codepoint: number, modifier: number, baseLayoutKey?: number): string | undefined {
1188
+ const normalizedCodepoint = normalizeKittyFunctionalCodepoint(codepoint);
1189
+
1190
+ // Use base layout key only when codepoint is not a recognized Latin
1191
+ // letter (a-z), digit (0-9), or symbol (/, -, [, ;, etc.). For those,
1192
+ // the codepoint is authoritative regardless of physical key position.
1193
+ // This prevents remapped layouts (Dvorak, Colemak, xremap, etc.) from
1194
+ // reporting the wrong key name based on the QWERTY physical position.
1195
+ const isLatinLetter = normalizedCodepoint >= 97 && normalizedCodepoint <= 122; // a-z
1196
+ const isDigit = normalizedCodepoint >= 48 && normalizedCodepoint <= 57; // 0-9
1197
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(normalizedCodepoint));
1198
+ const effectiveCodepoint =
1199
+ isLatinLetter || isDigit || isKnownSymbol ? normalizedCodepoint : (baseLayoutKey ?? normalizedCodepoint);
1200
+
1201
+ let keyName: string | undefined;
1202
+ if (effectiveCodepoint === CODEPOINTS.escape) keyName = "escape";
1203
+ else if (effectiveCodepoint === CODEPOINTS.tab) keyName = "tab";
1204
+ else if (effectiveCodepoint === CODEPOINTS.enter || effectiveCodepoint === CODEPOINTS.kpEnter) keyName = "enter";
1205
+ else if (effectiveCodepoint === CODEPOINTS.space) keyName = "space";
1206
+ else if (effectiveCodepoint === CODEPOINTS.backspace) keyName = "backspace";
1207
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.delete) keyName = "delete";
1208
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.insert) keyName = "insert";
1209
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.home) keyName = "home";
1210
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.end) keyName = "end";
1211
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageUp) keyName = "pageUp";
1212
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageDown) keyName = "pageDown";
1213
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.up) keyName = "up";
1214
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.down) keyName = "down";
1215
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.left) keyName = "left";
1216
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.right) keyName = "right";
1217
+ else if (effectiveCodepoint >= 48 && effectiveCodepoint <= 57) keyName = String.fromCharCode(effectiveCodepoint);
1218
+ else if (effectiveCodepoint >= 97 && effectiveCodepoint <= 122) keyName = String.fromCharCode(effectiveCodepoint);
1219
+ else if (SYMBOL_KEYS.has(String.fromCharCode(effectiveCodepoint))) keyName = String.fromCharCode(effectiveCodepoint);
1220
+
1221
+ if (!keyName) return undefined;
1222
+ return formatKeyNameWithModifiers(keyName, modifier);
1223
+ }
1224
+
1225
+ export function parseKey(data: string): string | undefined {
1226
+ const kitty = parseKittySequence(data);
1227
+ if (kitty) {
1228
+ return formatParsedKey(kitty.codepoint, kitty.modifier, kitty.baseLayoutKey);
1229
+ }
1230
+
1231
+ const modifyOtherKeys = parseModifyOtherKeysSequence(data);
1232
+ if (modifyOtherKeys) {
1233
+ return formatParsedKey(modifyOtherKeys.codepoint, modifyOtherKeys.modifier);
1234
+ }
1235
+
1236
+ // Mode-aware legacy sequences
1237
+ // When Kitty protocol is active, ambiguous sequences are interpreted as custom terminal mappings:
1238
+ // - \x1b\r = shift+enter (Kitty mapping), not alt+enter
1239
+ // - \n = shift+enter (Ghostty mapping)
1240
+ if (_kittyProtocolActive) {
1241
+ if (data === "\x1b\r" || data === "\n") return "shift+enter";
1242
+ }
1243
+
1244
+ const legacySequenceKeyId = LEGACY_SEQUENCE_KEY_IDS[data];
1245
+ if (legacySequenceKeyId) return legacySequenceKeyId;
1246
+
1247
+ // Legacy sequences (used when Kitty protocol is not active, or for unambiguous sequences)
1248
+ if (data === "\x1b") return "escape";
1249
+ if (data === "\x1c") return "ctrl+\\";
1250
+ if (data === "\x1d") return "ctrl+]";
1251
+ if (data === "\x1f") return "ctrl+-";
1252
+ if (data === "\x1b\x1b") return "ctrl+alt+[";
1253
+ if (data === "\x1b\x1c") return "ctrl+alt+\\";
1254
+ if (data === "\x1b\x1d") return "ctrl+alt+]";
1255
+ if (data === "\x1b\x1f") return "ctrl+alt+-";
1256
+ if (data === "\t") return "tab";
1257
+ if (data === "\r" || (!_kittyProtocolActive && data === "\n") || data === "\x1bOM") return "enter";
1258
+ if (data === "\x00") return "ctrl+space";
1259
+ if (data === " ") return "space";
1260
+ if (data === "\x7f") return "backspace";
1261
+ if (data === "\x08") return isWindowsTerminalSession() ? "ctrl+backspace" : "backspace";
1262
+ if (data === "\x1b[Z") return "shift+tab";
1263
+ if (!_kittyProtocolActive && data === "\x1b\r") return "alt+enter";
1264
+ if (!_kittyProtocolActive && data === "\x1b ") return "alt+space";
1265
+ if (data === "\x1b\x7f" || data === "\x1b\b") return "alt+backspace";
1266
+ if (!_kittyProtocolActive && data === "\x1bB") return "alt+left";
1267
+ if (!_kittyProtocolActive && data === "\x1bF") return "alt+right";
1268
+ if (!_kittyProtocolActive && data.length === 2 && data[0] === "\x1b") {
1269
+ const code = data.charCodeAt(1);
1270
+ if (code >= 1 && code <= 26) {
1271
+ return `ctrl+alt+${String.fromCharCode(code + 96)}`;
1272
+ }
1273
+ // Legacy alt+letter/digit (ESC followed by the key)
1274
+ if ((code >= 97 && code <= 122) || (code >= 48 && code <= 57)) {
1275
+ return `alt+${String.fromCharCode(code)}`;
1276
+ }
1277
+ }
1278
+ if (data === "\x1b[A") return "up";
1279
+ if (data === "\x1b[B") return "down";
1280
+ if (data === "\x1b[C") return "right";
1281
+ if (data === "\x1b[D") return "left";
1282
+ if (data === "\x1b[H" || data === "\x1bOH") return "home";
1283
+ if (data === "\x1b[F" || data === "\x1bOF") return "end";
1284
+ if (data === "\x1b[3~") return "delete";
1285
+ if (data === "\x1b[5~") return "pageUp";
1286
+ if (data === "\x1b[6~") return "pageDown";
1287
+
1288
+ // Raw Ctrl+letter
1289
+ if (data.length === 1) {
1290
+ const code = data.charCodeAt(0);
1291
+ if (code >= 1 && code <= 26) {
1292
+ return `ctrl+${String.fromCharCode(code + 96)}`;
1293
+ }
1294
+ if (code >= 32 && code <= 126) {
1295
+ return data;
1296
+ }
1297
+ }
1298
+
1299
+ return undefined;
1300
+ }
1301
+
1302
+ // =============================================================================
1303
+ // Kitty CSI-u Printable Decoding
1304
+ // =============================================================================
1305
+
1306
+ const KITTY_CSI_U_REGEX = /^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/;
1307
+ const KITTY_PRINTABLE_ALLOWED_MODIFIERS = MODIFIERS.shift | LOCK_MASK;
1308
+
1309
+ /**
1310
+ * Decode a Kitty CSI-u sequence into a printable character, if applicable.
1311
+ *
1312
+ * When Kitty keyboard protocol flag 1 (disambiguate) is active, terminals send
1313
+ * CSI-u sequences for all keys, including plain printable characters. This
1314
+ * function extracts the printable character from such sequences.
1315
+ *
1316
+ * Only accepts plain or Shift-modified keys. Rejects Ctrl, Alt, and unsupported
1317
+ * modifier combinations (those are handled by keybinding matching instead).
1318
+ * Prefers the shifted keycode when Shift is held and a shifted key is reported.
1319
+ *
1320
+ * @param data - Raw input data from terminal
1321
+ * @returns The printable character, or undefined if not a printable CSI-u sequence
1322
+ */
1323
+ export function decodeKittyPrintable(data: string): string | undefined {
1324
+ const match = data.match(KITTY_CSI_U_REGEX);
1325
+ if (!match) return undefined;
1326
+
1327
+ // CSI-u groups: <codepoint>[:<shifted>[:<base>]];<mod>[:<event>]u
1328
+ const codepoint = Number.parseInt(match[1] ?? "", 10);
1329
+ if (!Number.isFinite(codepoint)) return undefined;
1330
+
1331
+ const shiftedKey = match[2] && match[2].length > 0 ? Number.parseInt(match[2], 10) : undefined;
1332
+ const modValue = match[4] ? Number.parseInt(match[4], 10) : 1;
1333
+ // Modifiers are 1-indexed in CSI-u; normalize to our bitmask.
1334
+ const modifier = Number.isFinite(modValue) ? modValue - 1 : 0;
1335
+
1336
+ // Only accept printable CSI-u input for plain or Shift-modified text keys.
1337
+ // Reject unsupported modifier bits (e.g. Super/Meta) to avoid inserting
1338
+ // characters from modifier-only terminal events.
1339
+ if ((modifier & ~KITTY_PRINTABLE_ALLOWED_MODIFIERS) !== 0) return undefined;
1340
+ if (modifier & (MODIFIERS.alt | MODIFIERS.ctrl)) return undefined;
1341
+
1342
+ // Prefer the shifted keycode when Shift is held.
1343
+ let effectiveCodepoint = codepoint;
1344
+ if (modifier & MODIFIERS.shift && typeof shiftedKey === "number") {
1345
+ effectiveCodepoint = shiftedKey;
1346
+ }
1347
+ effectiveCodepoint = normalizeKittyFunctionalCodepoint(effectiveCodepoint);
1348
+ // Drop control characters or invalid codepoints.
1349
+ if (!Number.isFinite(effectiveCodepoint) || effectiveCodepoint < 32) return undefined;
1350
+
1351
+ try {
1352
+ return String.fromCodePoint(effectiveCodepoint);
1353
+ } catch {
1354
+ return undefined;
1355
+ }
1356
+ }