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