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