@dungle-scrubs/tallow 0.8.21 → 0.8.23

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 (217) hide show
  1. package/dist/cli.js +35 -4
  2. package/dist/cli.js.map +1 -1
  3. package/dist/config.d.ts +1 -1
  4. package/dist/config.js +1 -1
  5. package/dist/interactive-mode-patch.d.ts +2 -0
  6. package/dist/interactive-mode-patch.d.ts.map +1 -1
  7. package/dist/interactive-mode-patch.js +82 -0
  8. package/dist/interactive-mode-patch.js.map +1 -1
  9. package/dist/sdk.d.ts +17 -0
  10. package/dist/sdk.d.ts.map +1 -1
  11. package/dist/sdk.js +68 -1
  12. package/dist/sdk.js.map +1 -1
  13. package/dist/workspace-transition-relay.d.ts +40 -7
  14. package/dist/workspace-transition-relay.d.ts.map +1 -1
  15. package/dist/workspace-transition-relay.js +81 -16
  16. package/dist/workspace-transition-relay.js.map +1 -1
  17. package/extensions/__integration__/background-task-widget-ownership.test.ts +216 -0
  18. package/extensions/__integration__/claude-hooks-compat.test.ts +156 -0
  19. package/extensions/__integration__/slash-command-bridge.test.ts +169 -23
  20. package/extensions/_shared/atomic-write.ts +1 -1
  21. package/extensions/_shared/bordered-box.ts +102 -0
  22. package/extensions/_shared/interop-events.ts +5 -0
  23. package/extensions/_shared/pid-registry.ts +1 -1
  24. package/extensions/agent-commands-tool/index.ts +4 -1
  25. package/extensions/background-task-tool/__tests__/lifecycle.test.ts +50 -25
  26. package/extensions/background-task-tool/index.ts +139 -221
  27. package/extensions/bash-tool-enhanced/index.ts +1 -75
  28. package/extensions/cd-tool/index.ts +2 -2
  29. package/extensions/context-fork/spawn.ts +4 -1
  30. package/extensions/health/index.ts +6 -6
  31. package/extensions/hooks/__tests__/claude-compat.test.ts +35 -0
  32. package/extensions/hooks/__tests__/subprocess-hardening.test.ts +73 -0
  33. package/extensions/hooks/index.ts +27 -4
  34. package/extensions/loop/__tests__/loop.test.ts +168 -4
  35. package/extensions/loop/extension.json +6 -5
  36. package/extensions/loop/index.ts +242 -31
  37. package/extensions/plan-mode-tool/__tests__/agent-end-execution.test.ts +373 -0
  38. package/extensions/plan-mode-tool/index.ts +103 -41
  39. package/extensions/prompt-suggestions/__tests__/editor-compatibility.test.ts +42 -0
  40. package/extensions/prompt-suggestions/index.ts +41 -6
  41. package/extensions/slash-command-bridge/__tests__/slash-command-bridge.test.ts +267 -671
  42. package/extensions/slash-command-bridge/extension.json +1 -1
  43. package/extensions/slash-command-bridge/index.ts +230 -116
  44. package/extensions/subagent-tool/index.ts +2 -2
  45. package/extensions/subagent-tool/process.ts +4 -5
  46. package/extensions/tasks/commands/register-tasks-extension.ts +41 -0
  47. package/extensions/teams-tool/__tests__/peer-messaging.test.ts +29 -24
  48. package/extensions/teams-tool/dashboard.ts +3 -5
  49. package/extensions/teams-tool/dispatch/auto-dispatch.ts +18 -1
  50. package/extensions/teams-tool/tools/teammate-tools.ts +9 -6
  51. package/extensions/wezterm-pane-control/__tests__/index.test.ts +88 -4
  52. package/extensions/wezterm-pane-control/index.ts +113 -8
  53. package/package.json +6 -4
  54. package/packages/tallow-tui/README.md +51 -0
  55. package/packages/tallow-tui/dist/autocomplete.d.ts +48 -0
  56. package/packages/tallow-tui/dist/autocomplete.d.ts.map +1 -0
  57. package/packages/tallow-tui/dist/autocomplete.js +564 -0
  58. package/packages/tallow-tui/dist/autocomplete.js.map +1 -0
  59. package/packages/tallow-tui/dist/border-styles.d.ts +32 -0
  60. package/packages/tallow-tui/dist/border-styles.d.ts.map +1 -0
  61. package/packages/tallow-tui/dist/border-styles.js +46 -0
  62. package/packages/tallow-tui/dist/border-styles.js.map +1 -0
  63. package/packages/tallow-tui/dist/components/bordered-box.d.ts +52 -0
  64. package/packages/tallow-tui/dist/components/bordered-box.d.ts.map +1 -0
  65. package/packages/tallow-tui/dist/components/bordered-box.js +89 -0
  66. package/packages/tallow-tui/dist/components/bordered-box.js.map +1 -0
  67. package/packages/tallow-tui/dist/components/box.d.ts +22 -0
  68. package/packages/tallow-tui/dist/components/box.d.ts.map +1 -0
  69. package/packages/tallow-tui/dist/components/box.js +104 -0
  70. package/packages/tallow-tui/dist/components/box.js.map +1 -0
  71. package/packages/tallow-tui/dist/components/cancellable-loader.d.ts +22 -0
  72. package/packages/tallow-tui/dist/components/cancellable-loader.d.ts.map +1 -0
  73. package/packages/tallow-tui/dist/components/cancellable-loader.js +35 -0
  74. package/packages/tallow-tui/dist/components/cancellable-loader.js.map +1 -0
  75. package/packages/tallow-tui/dist/components/editor.d.ts +240 -0
  76. package/packages/tallow-tui/dist/components/editor.d.ts.map +1 -0
  77. package/packages/tallow-tui/dist/components/editor.js +1766 -0
  78. package/packages/tallow-tui/dist/components/editor.js.map +1 -0
  79. package/packages/tallow-tui/dist/components/image.d.ts +126 -0
  80. package/packages/tallow-tui/dist/components/image.d.ts.map +1 -0
  81. package/packages/tallow-tui/dist/components/image.js +245 -0
  82. package/packages/tallow-tui/dist/components/image.js.map +1 -0
  83. package/packages/tallow-tui/dist/components/input.d.ts +37 -0
  84. package/packages/tallow-tui/dist/components/input.d.ts.map +1 -0
  85. package/packages/tallow-tui/dist/components/input.js +439 -0
  86. package/packages/tallow-tui/dist/components/input.js.map +1 -0
  87. package/packages/tallow-tui/dist/components/loader.d.ts +88 -0
  88. package/packages/tallow-tui/dist/components/loader.d.ts.map +1 -0
  89. package/packages/tallow-tui/dist/components/loader.js +146 -0
  90. package/packages/tallow-tui/dist/components/loader.js.map +1 -0
  91. package/packages/tallow-tui/dist/components/markdown.d.ts +95 -0
  92. package/packages/tallow-tui/dist/components/markdown.d.ts.map +1 -0
  93. package/packages/tallow-tui/dist/components/markdown.js +633 -0
  94. package/packages/tallow-tui/dist/components/markdown.js.map +1 -0
  95. package/packages/tallow-tui/dist/components/select-list.d.ts +32 -0
  96. package/packages/tallow-tui/dist/components/select-list.d.ts.map +1 -0
  97. package/packages/tallow-tui/dist/components/select-list.js +156 -0
  98. package/packages/tallow-tui/dist/components/select-list.js.map +1 -0
  99. package/packages/tallow-tui/dist/components/settings-list.d.ts +50 -0
  100. package/packages/tallow-tui/dist/components/settings-list.d.ts.map +1 -0
  101. package/packages/tallow-tui/dist/components/settings-list.js +189 -0
  102. package/packages/tallow-tui/dist/components/settings-list.js.map +1 -0
  103. package/packages/tallow-tui/dist/components/spacer.d.ts +12 -0
  104. package/packages/tallow-tui/dist/components/spacer.d.ts.map +1 -0
  105. package/packages/tallow-tui/dist/components/spacer.js +23 -0
  106. package/packages/tallow-tui/dist/components/spacer.js.map +1 -0
  107. package/packages/tallow-tui/dist/components/text.d.ts +19 -0
  108. package/packages/tallow-tui/dist/components/text.d.ts.map +1 -0
  109. package/packages/tallow-tui/dist/components/text.js +91 -0
  110. package/packages/tallow-tui/dist/components/text.js.map +1 -0
  111. package/packages/tallow-tui/dist/components/truncated-text.d.ts +13 -0
  112. package/packages/tallow-tui/dist/components/truncated-text.d.ts.map +1 -0
  113. package/packages/tallow-tui/dist/components/truncated-text.js +51 -0
  114. package/packages/tallow-tui/dist/components/truncated-text.js.map +1 -0
  115. package/packages/tallow-tui/dist/editor-component.d.ts +50 -0
  116. package/packages/tallow-tui/dist/editor-component.d.ts.map +1 -0
  117. package/packages/tallow-tui/dist/editor-component.js +2 -0
  118. package/packages/tallow-tui/dist/editor-component.js.map +1 -0
  119. package/packages/tallow-tui/dist/fuzzy.d.ts +16 -0
  120. package/packages/tallow-tui/dist/fuzzy.d.ts.map +1 -0
  121. package/packages/tallow-tui/dist/fuzzy.js +107 -0
  122. package/packages/tallow-tui/dist/fuzzy.js.map +1 -0
  123. package/packages/tallow-tui/dist/index.d.ts +25 -0
  124. package/packages/tallow-tui/dist/index.d.ts.map +1 -0
  125. package/packages/tallow-tui/dist/index.js +35 -0
  126. package/packages/tallow-tui/dist/index.js.map +1 -0
  127. package/packages/tallow-tui/dist/keybindings.d.ts +39 -0
  128. package/packages/tallow-tui/dist/keybindings.d.ts.map +1 -0
  129. package/packages/tallow-tui/dist/keybindings.js +114 -0
  130. package/packages/tallow-tui/dist/keybindings.js.map +1 -0
  131. package/packages/tallow-tui/dist/keys.d.ts +168 -0
  132. package/packages/tallow-tui/dist/keys.d.ts.map +1 -0
  133. package/packages/tallow-tui/dist/keys.js +971 -0
  134. package/packages/tallow-tui/dist/keys.js.map +1 -0
  135. package/packages/tallow-tui/dist/kill-ring.d.ts +28 -0
  136. package/packages/tallow-tui/dist/kill-ring.d.ts.map +1 -0
  137. package/packages/tallow-tui/dist/kill-ring.js +44 -0
  138. package/packages/tallow-tui/dist/kill-ring.js.map +1 -0
  139. package/packages/tallow-tui/dist/stdin-buffer.d.ts +48 -0
  140. package/packages/tallow-tui/dist/stdin-buffer.d.ts.map +1 -0
  141. package/packages/tallow-tui/dist/stdin-buffer.js +317 -0
  142. package/packages/tallow-tui/dist/stdin-buffer.js.map +1 -0
  143. package/packages/tallow-tui/dist/terminal-image.d.ts +161 -0
  144. package/packages/tallow-tui/dist/terminal-image.d.ts.map +1 -0
  145. package/packages/tallow-tui/dist/terminal-image.js +460 -0
  146. package/packages/tallow-tui/dist/terminal-image.js.map +1 -0
  147. package/packages/tallow-tui/dist/terminal.d.ts +102 -0
  148. package/packages/tallow-tui/dist/terminal.d.ts.map +1 -0
  149. package/packages/tallow-tui/dist/terminal.js +263 -0
  150. package/packages/tallow-tui/dist/terminal.js.map +1 -0
  151. package/packages/tallow-tui/dist/test-utils/capability-env.d.ts +14 -0
  152. package/packages/tallow-tui/dist/test-utils/capability-env.d.ts.map +1 -0
  153. package/packages/tallow-tui/dist/test-utils/capability-env.js +55 -0
  154. package/packages/tallow-tui/dist/test-utils/capability-env.js.map +1 -0
  155. package/packages/tallow-tui/dist/tui.d.ts +239 -0
  156. package/packages/tallow-tui/dist/tui.d.ts.map +1 -0
  157. package/packages/tallow-tui/dist/tui.js +1058 -0
  158. package/packages/tallow-tui/dist/tui.js.map +1 -0
  159. package/packages/tallow-tui/dist/undo-stack.d.ts +17 -0
  160. package/packages/tallow-tui/dist/undo-stack.d.ts.map +1 -0
  161. package/packages/tallow-tui/dist/undo-stack.js +25 -0
  162. package/packages/tallow-tui/dist/undo-stack.js.map +1 -0
  163. package/packages/tallow-tui/dist/utils.d.ts +96 -0
  164. package/packages/tallow-tui/dist/utils.d.ts.map +1 -0
  165. package/packages/tallow-tui/dist/utils.js +843 -0
  166. package/packages/tallow-tui/dist/utils.js.map +1 -0
  167. package/packages/tallow-tui/package.json +24 -0
  168. package/packages/tallow-tui/src/__tests__/__snapshots__/render.test.ts.snap +121 -0
  169. package/packages/tallow-tui/src/__tests__/editor-border.test.ts +72 -0
  170. package/packages/tallow-tui/src/__tests__/editor-change-listener.test.ts +121 -0
  171. package/packages/tallow-tui/src/__tests__/editor-ghost-text.test.ts +112 -0
  172. package/packages/tallow-tui/src/__tests__/fuzzy.test.ts +91 -0
  173. package/packages/tallow-tui/src/__tests__/image-component.test.ts +113 -0
  174. package/packages/tallow-tui/src/__tests__/keys.test.ts +141 -0
  175. package/packages/tallow-tui/src/__tests__/render.test.ts +179 -0
  176. package/packages/tallow-tui/src/__tests__/stdin-buffer.test.ts +82 -0
  177. package/packages/tallow-tui/src/__tests__/terminal-image.test.ts +363 -0
  178. package/packages/tallow-tui/src/__tests__/tui-diff-regression.test.ts +454 -0
  179. package/packages/tallow-tui/src/__tests__/tui-render-scheduling.test.ts +256 -0
  180. package/packages/tallow-tui/src/__tests__/utils.test.ts +259 -0
  181. package/packages/tallow-tui/src/autocomplete.ts +716 -0
  182. package/packages/tallow-tui/src/border-styles.ts +60 -0
  183. package/packages/tallow-tui/src/components/bordered-box.ts +113 -0
  184. package/packages/tallow-tui/src/components/box.ts +137 -0
  185. package/packages/tallow-tui/src/components/cancellable-loader.ts +40 -0
  186. package/packages/tallow-tui/src/components/editor.ts +2143 -0
  187. package/packages/tallow-tui/src/components/image.ts +315 -0
  188. package/packages/tallow-tui/src/components/input.ts +522 -0
  189. package/packages/tallow-tui/src/components/loader.ts +187 -0
  190. package/packages/tallow-tui/src/components/markdown.ts +780 -0
  191. package/packages/tallow-tui/src/components/select-list.ts +197 -0
  192. package/packages/tallow-tui/src/components/settings-list.ts +264 -0
  193. package/packages/tallow-tui/src/components/spacer.ts +28 -0
  194. package/packages/tallow-tui/src/components/text.ts +113 -0
  195. package/packages/tallow-tui/src/components/truncated-text.ts +65 -0
  196. package/packages/tallow-tui/src/editor-component.ts +92 -0
  197. package/packages/tallow-tui/src/fuzzy.ts +133 -0
  198. package/packages/tallow-tui/src/index.ts +118 -0
  199. package/packages/tallow-tui/src/keybindings.ts +183 -0
  200. package/packages/tallow-tui/src/keys.ts +1189 -0
  201. package/packages/tallow-tui/src/kill-ring.ts +46 -0
  202. package/packages/tallow-tui/src/stdin-buffer.ts +386 -0
  203. package/packages/tallow-tui/src/terminal-image.ts +619 -0
  204. package/packages/tallow-tui/src/terminal.ts +350 -0
  205. package/packages/tallow-tui/src/test-utils/capability-env.ts +56 -0
  206. package/packages/tallow-tui/src/tui.ts +1336 -0
  207. package/packages/tallow-tui/src/undo-stack.ts +28 -0
  208. package/packages/tallow-tui/src/utils.ts +948 -0
  209. package/packages/tallow-tui/tsconfig.build.json +21 -0
  210. package/runtime/agent-runner.ts +20 -0
  211. package/runtime/atomic-write.ts +8 -0
  212. package/runtime/otel.ts +12 -0
  213. package/runtime/resolve-module.ts +23 -0
  214. package/runtime/runtime-path-provider.ts +12 -0
  215. package/runtime/runtime-provenance.ts +17 -0
  216. package/runtime/workspace-transition-relay.ts +21 -0
  217. package/runtime/workspace-transition.ts +29 -0
@@ -0,0 +1,1189 @@
1
+ /**
2
+ * Keyboard input handling for terminal applications.
3
+ *
4
+ * Supports both legacy terminal sequences and Kitty keyboard protocol.
5
+ * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
6
+ * Reference: https://github.com/sst/opentui/blob/7da92b4088aebfe27b9f691c04163a48821e49fd/packages/core/src/lib/parse.keypress.ts
7
+ *
8
+ * Symbol keys are also supported, however some ctrl+symbol combos
9
+ * overlap with ASCII codes, e.g. ctrl+[ = ESC.
10
+ * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/#legacy-ctrl-mapping-of-ascii-keys
11
+ * Those can still be * used for ctrl+shift combos
12
+ *
13
+ * API:
14
+ * - matchesKey(data, keyId) - Check if input matches a key identifier
15
+ * - parseKey(data) - Parse input and return the key identifier
16
+ * - Key - Helper object for creating typed key identifiers
17
+ * - setKittyProtocolActive(active) - Set global Kitty protocol state
18
+ * - isKittyProtocolActive() - Query global Kitty protocol state
19
+ */
20
+
21
+ // =============================================================================
22
+ // Global Kitty Protocol State
23
+ // =============================================================================
24
+
25
+ let _kittyProtocolActive = false;
26
+
27
+ /**
28
+ * Set the global Kitty keyboard protocol state.
29
+ * Called by ProcessTerminal after detecting protocol support.
30
+ */
31
+ export function setKittyProtocolActive(active: boolean): void {
32
+ _kittyProtocolActive = active;
33
+ }
34
+
35
+ /**
36
+ * Query whether Kitty keyboard protocol is currently active.
37
+ */
38
+ export function isKittyProtocolActive(): boolean {
39
+ return _kittyProtocolActive;
40
+ }
41
+
42
+ // =============================================================================
43
+ // Type-Safe Key Identifiers
44
+ // =============================================================================
45
+
46
+ type Letter =
47
+ | "a"
48
+ | "b"
49
+ | "c"
50
+ | "d"
51
+ | "e"
52
+ | "f"
53
+ | "g"
54
+ | "h"
55
+ | "i"
56
+ | "j"
57
+ | "k"
58
+ | "l"
59
+ | "m"
60
+ | "n"
61
+ | "o"
62
+ | "p"
63
+ | "q"
64
+ | "r"
65
+ | "s"
66
+ | "t"
67
+ | "u"
68
+ | "v"
69
+ | "w"
70
+ | "x"
71
+ | "y"
72
+ | "z";
73
+
74
+ type SymbolKey =
75
+ | "`"
76
+ | "-"
77
+ | "="
78
+ | "["
79
+ | "]"
80
+ | "\\"
81
+ | ";"
82
+ | "'"
83
+ | ","
84
+ | "."
85
+ | "/"
86
+ | "!"
87
+ | "@"
88
+ | "#"
89
+ | "$"
90
+ | "%"
91
+ | "^"
92
+ | "&"
93
+ | "*"
94
+ | "("
95
+ | ")"
96
+ | "_"
97
+ | "+"
98
+ | "|"
99
+ | "~"
100
+ | "{"
101
+ | "}"
102
+ | ":"
103
+ | "<"
104
+ | ">"
105
+ | "?";
106
+
107
+ type SpecialKey =
108
+ | "escape"
109
+ | "esc"
110
+ | "enter"
111
+ | "return"
112
+ | "tab"
113
+ | "space"
114
+ | "backspace"
115
+ | "delete"
116
+ | "insert"
117
+ | "clear"
118
+ | "home"
119
+ | "end"
120
+ | "pageUp"
121
+ | "pageDown"
122
+ | "up"
123
+ | "down"
124
+ | "left"
125
+ | "right"
126
+ | "f1"
127
+ | "f2"
128
+ | "f3"
129
+ | "f4"
130
+ | "f5"
131
+ | "f6"
132
+ | "f7"
133
+ | "f8"
134
+ | "f9"
135
+ | "f10"
136
+ | "f11"
137
+ | "f12";
138
+
139
+ type BaseKey = Letter | SymbolKey | SpecialKey;
140
+
141
+ /**
142
+ * Union type of all valid key identifiers.
143
+ * Provides autocomplete and catches typos at compile time.
144
+ */
145
+ export type KeyId =
146
+ | BaseKey
147
+ | `ctrl+${BaseKey}`
148
+ | `shift+${BaseKey}`
149
+ | `alt+${BaseKey}`
150
+ | `ctrl+shift+${BaseKey}`
151
+ | `shift+ctrl+${BaseKey}`
152
+ | `ctrl+alt+${BaseKey}`
153
+ | `alt+ctrl+${BaseKey}`
154
+ | `shift+alt+${BaseKey}`
155
+ | `alt+shift+${BaseKey}`
156
+ | `ctrl+shift+alt+${BaseKey}`
157
+ | `ctrl+alt+shift+${BaseKey}`
158
+ | `shift+ctrl+alt+${BaseKey}`
159
+ | `shift+alt+ctrl+${BaseKey}`
160
+ | `alt+ctrl+shift+${BaseKey}`
161
+ | `alt+shift+ctrl+${BaseKey}`;
162
+
163
+ /**
164
+ * Helper object for creating typed key identifiers with autocomplete.
165
+ *
166
+ * Usage:
167
+ * - Key.escape, Key.enter, Key.tab, etc. for special keys
168
+ * - Key.backtick, Key.comma, Key.period, etc. for symbol keys
169
+ * - Key.ctrl("c"), Key.alt("x") for single modifier
170
+ * - Key.ctrlShift("p"), Key.ctrlAlt("x") for combined modifiers
171
+ */
172
+ export const Key = {
173
+ // Special keys
174
+ escape: "escape" as const,
175
+ esc: "esc" as const,
176
+ enter: "enter" as const,
177
+ return: "return" as const,
178
+ tab: "tab" as const,
179
+ space: "space" as const,
180
+ backspace: "backspace" as const,
181
+ delete: "delete" as const,
182
+ insert: "insert" as const,
183
+ clear: "clear" as const,
184
+ home: "home" as const,
185
+ end: "end" as const,
186
+ pageUp: "pageUp" as const,
187
+ pageDown: "pageDown" as const,
188
+ up: "up" as const,
189
+ down: "down" as const,
190
+ left: "left" as const,
191
+ right: "right" as const,
192
+ f1: "f1" as const,
193
+ f2: "f2" as const,
194
+ f3: "f3" as const,
195
+ f4: "f4" as const,
196
+ f5: "f5" as const,
197
+ f6: "f6" as const,
198
+ f7: "f7" as const,
199
+ f8: "f8" as const,
200
+ f9: "f9" as const,
201
+ f10: "f10" as const,
202
+ f11: "f11" as const,
203
+ f12: "f12" as const,
204
+
205
+ // Symbol keys
206
+ backtick: "`" as const,
207
+ hyphen: "-" as const,
208
+ equals: "=" as const,
209
+ leftbracket: "[" as const,
210
+ rightbracket: "]" as const,
211
+ backslash: "\\" as const,
212
+ semicolon: ";" as const,
213
+ quote: "'" as const,
214
+ comma: "," as const,
215
+ period: "." as const,
216
+ slash: "/" as const,
217
+ exclamation: "!" as const,
218
+ at: "@" as const,
219
+ hash: "#" as const,
220
+ dollar: "$" as const,
221
+ percent: "%" as const,
222
+ caret: "^" as const,
223
+ ampersand: "&" as const,
224
+ asterisk: "*" as const,
225
+ leftparen: "(" as const,
226
+ rightparen: ")" as const,
227
+ underscore: "_" as const,
228
+ plus: "+" as const,
229
+ pipe: "|" as const,
230
+ tilde: "~" as const,
231
+ leftbrace: "{" as const,
232
+ rightbrace: "}" as const,
233
+ colon: ":" as const,
234
+ lessthan: "<" as const,
235
+ greaterthan: ">" as const,
236
+ question: "?" as const,
237
+
238
+ // Single modifiers
239
+ ctrl: <K extends BaseKey>(key: K): `ctrl+${K}` => `ctrl+${key}`,
240
+ shift: <K extends BaseKey>(key: K): `shift+${K}` => `shift+${key}`,
241
+ alt: <K extends BaseKey>(key: K): `alt+${K}` => `alt+${key}`,
242
+
243
+ // Combined modifiers
244
+ ctrlShift: <K extends BaseKey>(key: K): `ctrl+shift+${K}` => `ctrl+shift+${key}`,
245
+ shiftCtrl: <K extends BaseKey>(key: K): `shift+ctrl+${K}` => `shift+ctrl+${key}`,
246
+ ctrlAlt: <K extends BaseKey>(key: K): `ctrl+alt+${K}` => `ctrl+alt+${key}`,
247
+ altCtrl: <K extends BaseKey>(key: K): `alt+ctrl+${K}` => `alt+ctrl+${key}`,
248
+ shiftAlt: <K extends BaseKey>(key: K): `shift+alt+${K}` => `shift+alt+${key}`,
249
+ altShift: <K extends BaseKey>(key: K): `alt+shift+${K}` => `alt+shift+${key}`,
250
+
251
+ // Triple modifiers
252
+ ctrlShiftAlt: <K extends BaseKey>(key: K): `ctrl+shift+alt+${K}` => `ctrl+shift+alt+${key}`,
253
+ } as const;
254
+
255
+ // =============================================================================
256
+ // Constants
257
+ // =============================================================================
258
+
259
+ const SYMBOL_KEYS = new Set([
260
+ "`",
261
+ "-",
262
+ "=",
263
+ "[",
264
+ "]",
265
+ "\\",
266
+ ";",
267
+ "'",
268
+ ",",
269
+ ".",
270
+ "/",
271
+ "!",
272
+ "@",
273
+ "#",
274
+ "$",
275
+ "%",
276
+ "^",
277
+ "&",
278
+ "*",
279
+ "(",
280
+ ")",
281
+ "_",
282
+ "+",
283
+ "|",
284
+ "~",
285
+ "{",
286
+ "}",
287
+ ":",
288
+ "<",
289
+ ">",
290
+ "?",
291
+ ]);
292
+
293
+ const MODIFIERS = {
294
+ shift: 1,
295
+ alt: 2,
296
+ ctrl: 4,
297
+ } as const;
298
+
299
+ const LOCK_MASK = 64 + 128; // Caps Lock + Num Lock
300
+
301
+ const CODEPOINTS = {
302
+ escape: 27,
303
+ tab: 9,
304
+ enter: 13,
305
+ space: 32,
306
+ backspace: 127,
307
+ kpEnter: 57414, // Numpad Enter (Kitty protocol)
308
+ } as const;
309
+
310
+ const ARROW_CODEPOINTS = {
311
+ up: -1,
312
+ down: -2,
313
+ right: -3,
314
+ left: -4,
315
+ } as const;
316
+
317
+ const FUNCTIONAL_CODEPOINTS = {
318
+ delete: -10,
319
+ insert: -11,
320
+ pageUp: -12,
321
+ pageDown: -13,
322
+ home: -14,
323
+ end: -15,
324
+ } as const;
325
+
326
+ const LEGACY_KEY_SEQUENCES = {
327
+ up: ["\x1b[A", "\x1bOA"],
328
+ down: ["\x1b[B", "\x1bOB"],
329
+ right: ["\x1b[C", "\x1bOC"],
330
+ left: ["\x1b[D", "\x1bOD"],
331
+ home: ["\x1b[H", "\x1bOH", "\x1b[1~", "\x1b[7~"],
332
+ end: ["\x1b[F", "\x1bOF", "\x1b[4~", "\x1b[8~"],
333
+ insert: ["\x1b[2~"],
334
+ delete: ["\x1b[3~"],
335
+ pageUp: ["\x1b[5~", "\x1b[[5~"],
336
+ pageDown: ["\x1b[6~", "\x1b[[6~"],
337
+ clear: ["\x1b[E", "\x1bOE"],
338
+ f1: ["\x1bOP", "\x1b[11~", "\x1b[[A"],
339
+ f2: ["\x1bOQ", "\x1b[12~", "\x1b[[B"],
340
+ f3: ["\x1bOR", "\x1b[13~", "\x1b[[C"],
341
+ f4: ["\x1bOS", "\x1b[14~", "\x1b[[D"],
342
+ f5: ["\x1b[15~", "\x1b[[E"],
343
+ f6: ["\x1b[17~"],
344
+ f7: ["\x1b[18~"],
345
+ f8: ["\x1b[19~"],
346
+ f9: ["\x1b[20~"],
347
+ f10: ["\x1b[21~"],
348
+ f11: ["\x1b[23~"],
349
+ f12: ["\x1b[24~"],
350
+ } as const;
351
+
352
+ const LEGACY_SHIFT_SEQUENCES = {
353
+ up: ["\x1b[a"],
354
+ down: ["\x1b[b"],
355
+ right: ["\x1b[c"],
356
+ left: ["\x1b[d"],
357
+ clear: ["\x1b[e"],
358
+ insert: ["\x1b[2$"],
359
+ delete: ["\x1b[3$"],
360
+ pageUp: ["\x1b[5$"],
361
+ pageDown: ["\x1b[6$"],
362
+ home: ["\x1b[7$"],
363
+ end: ["\x1b[8$"],
364
+ } as const;
365
+
366
+ const LEGACY_CTRL_SEQUENCES = {
367
+ up: ["\x1bOa"],
368
+ down: ["\x1bOb"],
369
+ right: ["\x1bOc"],
370
+ left: ["\x1bOd"],
371
+ clear: ["\x1bOe"],
372
+ insert: ["\x1b[2^"],
373
+ delete: ["\x1b[3^"],
374
+ pageUp: ["\x1b[5^"],
375
+ pageDown: ["\x1b[6^"],
376
+ home: ["\x1b[7^"],
377
+ end: ["\x1b[8^"],
378
+ } as const;
379
+
380
+ const LEGACY_SEQUENCE_KEY_IDS: Record<string, KeyId> = {
381
+ "\x1bOA": "up",
382
+ "\x1bOB": "down",
383
+ "\x1bOC": "right",
384
+ "\x1bOD": "left",
385
+ "\x1bOH": "home",
386
+ "\x1bOF": "end",
387
+ "\x1b[E": "clear",
388
+ "\x1bOE": "clear",
389
+ "\x1bOe": "ctrl+clear",
390
+ "\x1b[e": "shift+clear",
391
+ "\x1b[2~": "insert",
392
+ "\x1b[2$": "shift+insert",
393
+ "\x1b[2^": "ctrl+insert",
394
+ "\x1b[3$": "shift+delete",
395
+ "\x1b[3^": "ctrl+delete",
396
+ "\x1b[[5~": "pageUp",
397
+ "\x1b[[6~": "pageDown",
398
+ "\x1b[a": "shift+up",
399
+ "\x1b[b": "shift+down",
400
+ "\x1b[c": "shift+right",
401
+ "\x1b[d": "shift+left",
402
+ "\x1bOa": "ctrl+up",
403
+ "\x1bOb": "ctrl+down",
404
+ "\x1bOc": "ctrl+right",
405
+ "\x1bOd": "ctrl+left",
406
+ "\x1b[5$": "shift+pageUp",
407
+ "\x1b[6$": "shift+pageDown",
408
+ "\x1b[7$": "shift+home",
409
+ "\x1b[8$": "shift+end",
410
+ "\x1b[5^": "ctrl+pageUp",
411
+ "\x1b[6^": "ctrl+pageDown",
412
+ "\x1b[7^": "ctrl+home",
413
+ "\x1b[8^": "ctrl+end",
414
+ "\x1bOP": "f1",
415
+ "\x1bOQ": "f2",
416
+ "\x1bOR": "f3",
417
+ "\x1bOS": "f4",
418
+ "\x1b[11~": "f1",
419
+ "\x1b[12~": "f2",
420
+ "\x1b[13~": "f3",
421
+ "\x1b[14~": "f4",
422
+ "\x1b[[A": "f1",
423
+ "\x1b[[B": "f2",
424
+ "\x1b[[C": "f3",
425
+ "\x1b[[D": "f4",
426
+ "\x1b[[E": "f5",
427
+ "\x1b[15~": "f5",
428
+ "\x1b[17~": "f6",
429
+ "\x1b[18~": "f7",
430
+ "\x1b[19~": "f8",
431
+ "\x1b[20~": "f9",
432
+ "\x1b[21~": "f10",
433
+ "\x1b[23~": "f11",
434
+ "\x1b[24~": "f12",
435
+ "\x1bb": "alt+left",
436
+ "\x1bf": "alt+right",
437
+ "\x1bp": "alt+up",
438
+ "\x1bn": "alt+down",
439
+ } as const;
440
+
441
+ type LegacyModifierKey = keyof typeof LEGACY_SHIFT_SEQUENCES;
442
+
443
+ const matchesLegacySequence = (data: string, sequences: readonly string[]): boolean =>
444
+ sequences.includes(data);
445
+
446
+ const matchesLegacyModifierSequence = (
447
+ data: string,
448
+ key: LegacyModifierKey,
449
+ modifier: number
450
+ ): boolean => {
451
+ if (modifier === MODIFIERS.shift) {
452
+ return matchesLegacySequence(data, LEGACY_SHIFT_SEQUENCES[key]);
453
+ }
454
+ if (modifier === MODIFIERS.ctrl) {
455
+ return matchesLegacySequence(data, LEGACY_CTRL_SEQUENCES[key]);
456
+ }
457
+ return false;
458
+ };
459
+
460
+ // =============================================================================
461
+ // Kitty Protocol Parsing
462
+ // =============================================================================
463
+
464
+ /**
465
+ * Event types from Kitty keyboard protocol (flag 2)
466
+ * 1 = key press, 2 = key repeat, 3 = key release
467
+ */
468
+ export type KeyEventType = "press" | "repeat" | "release";
469
+
470
+ interface ParsedKittySequence {
471
+ codepoint: number;
472
+ shiftedKey?: number; // Shifted version of the key (when shift is pressed)
473
+ baseLayoutKey?: number; // Key in standard PC-101 layout (for non-Latin layouts)
474
+ modifier: number;
475
+ eventType: KeyEventType;
476
+ }
477
+
478
+ // Tracks the event type from the most recent parseKey() call.
479
+ let _lastEventType: KeyEventType = "press";
480
+
481
+ /**
482
+ * Check if the given input data represents a key release event.
483
+ * Performs fast pattern matching on raw terminal escape sequences.
484
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
485
+ *
486
+ * @param data - Raw terminal input string
487
+ * @returns true if the data contains a key release escape sequence
488
+ */
489
+ export function isKeyRelease(data: string): boolean {
490
+ // Don't treat bracketed paste content as key release, even if it contains
491
+ // patterns like ":3F" (e.g., bluetooth MAC addresses like "90:62:3F:A5").
492
+ // Terminal.ts re-wraps paste content with bracketed paste markers before
493
+ // passing to TUI, so pasted data will always contain \x1b[200~.
494
+ if (data.includes("\x1b[200~")) {
495
+ return false;
496
+ }
497
+
498
+ // Quick check: release events with flag 2 contain ":3"
499
+ // Format: \x1b[<codepoint>;<modifier>:3u
500
+ if (
501
+ data.includes(":3u") ||
502
+ data.includes(":3~") ||
503
+ data.includes(":3A") ||
504
+ data.includes(":3B") ||
505
+ data.includes(":3C") ||
506
+ data.includes(":3D") ||
507
+ data.includes(":3H") ||
508
+ data.includes(":3F")
509
+ ) {
510
+ return true;
511
+ }
512
+ return false;
513
+ }
514
+
515
+ /**
516
+ * Check if the given input data represents a key repeat event.
517
+ * Performs fast pattern matching on raw terminal escape sequences.
518
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
519
+ *
520
+ * @param data - Raw terminal input string
521
+ * @returns true if the data contains a key repeat escape sequence
522
+ */
523
+ export function isKeyRepeat(data: string): boolean {
524
+ // Don't treat bracketed paste content as key repeat, even if it contains
525
+ // patterns like ":2F". See isKeyRelease() for details.
526
+ if (data.includes("\x1b[200~")) {
527
+ return false;
528
+ }
529
+
530
+ if (
531
+ data.includes(":2u") ||
532
+ data.includes(":2~") ||
533
+ data.includes(":2A") ||
534
+ data.includes(":2B") ||
535
+ data.includes(":2C") ||
536
+ data.includes(":2D") ||
537
+ data.includes(":2H") ||
538
+ data.includes(":2F")
539
+ ) {
540
+ return true;
541
+ }
542
+ return false;
543
+ }
544
+
545
+ function parseEventType(eventTypeStr: string | undefined): KeyEventType {
546
+ if (!eventTypeStr) return "press";
547
+ const eventType = parseInt(eventTypeStr, 10);
548
+ if (eventType === 2) return "repeat";
549
+ if (eventType === 3) return "release";
550
+ return "press";
551
+ }
552
+
553
+ function parseKittySequence(data: string): ParsedKittySequence | null {
554
+ // CSI u format with alternate keys (flag 4):
555
+ // \x1b[<codepoint>u
556
+ // \x1b[<codepoint>;<mod>u
557
+ // \x1b[<codepoint>;<mod>:<event>u
558
+ // \x1b[<codepoint>:<shifted>;<mod>u
559
+ // \x1b[<codepoint>:<shifted>:<base>;<mod>u
560
+ // \x1b[<codepoint>::<base>;<mod>u (no shifted key, only base)
561
+ //
562
+ // With flag 2, event type is appended after modifier colon: 1=press, 2=repeat, 3=release
563
+ // With flag 4, alternate keys are appended after codepoint with colons
564
+ const csiUMatch = data.match(/^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/);
565
+ if (csiUMatch) {
566
+ const codepoint = parseInt(csiUMatch[1]!, 10);
567
+ const shiftedKey =
568
+ csiUMatch[2] && csiUMatch[2].length > 0 ? parseInt(csiUMatch[2], 10) : undefined;
569
+ const baseLayoutKey = csiUMatch[3] ? parseInt(csiUMatch[3], 10) : undefined;
570
+ const modValue = csiUMatch[4] ? parseInt(csiUMatch[4], 10) : 1;
571
+ const eventType = parseEventType(csiUMatch[5]);
572
+ _lastEventType = eventType;
573
+ return { codepoint, shiftedKey, baseLayoutKey, modifier: modValue - 1, eventType };
574
+ }
575
+
576
+ // Arrow keys with modifier: \x1b[1;<mod>A/B/C/D or \x1b[1;<mod>:<event>A/B/C/D
577
+ const arrowMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([ABCD])$/);
578
+ if (arrowMatch) {
579
+ const modValue = parseInt(arrowMatch[1]!, 10);
580
+ const eventType = parseEventType(arrowMatch[2]);
581
+ const arrowCodes: Record<string, number> = { A: -1, B: -2, C: -3, D: -4 };
582
+ _lastEventType = eventType;
583
+ return { codepoint: arrowCodes[arrowMatch[3]!]!, modifier: modValue - 1, eventType };
584
+ }
585
+
586
+ // Functional keys: \x1b[<num>~ or \x1b[<num>;<mod>~ or \x1b[<num>;<mod>:<event>~
587
+ const funcMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?(?::(\d+))?~$/);
588
+ if (funcMatch) {
589
+ const keyNum = parseInt(funcMatch[1]!, 10);
590
+ const modValue = funcMatch[2] ? parseInt(funcMatch[2], 10) : 1;
591
+ const eventType = parseEventType(funcMatch[3]);
592
+ const funcCodes: Record<number, number> = {
593
+ 2: FUNCTIONAL_CODEPOINTS.insert,
594
+ 3: FUNCTIONAL_CODEPOINTS.delete,
595
+ 5: FUNCTIONAL_CODEPOINTS.pageUp,
596
+ 6: FUNCTIONAL_CODEPOINTS.pageDown,
597
+ 7: FUNCTIONAL_CODEPOINTS.home,
598
+ 8: FUNCTIONAL_CODEPOINTS.end,
599
+ };
600
+ const codepoint = funcCodes[keyNum];
601
+ if (codepoint !== undefined) {
602
+ _lastEventType = eventType;
603
+ return { codepoint, modifier: modValue - 1, eventType };
604
+ }
605
+ }
606
+
607
+ // Home/End with modifier: \x1b[1;<mod>H/F or \x1b[1;<mod>:<event>H/F
608
+ const homeEndMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([HF])$/);
609
+ if (homeEndMatch) {
610
+ const modValue = parseInt(homeEndMatch[1]!, 10);
611
+ const eventType = parseEventType(homeEndMatch[2]);
612
+ const codepoint =
613
+ homeEndMatch[3] === "H" ? FUNCTIONAL_CODEPOINTS.home : FUNCTIONAL_CODEPOINTS.end;
614
+ _lastEventType = eventType;
615
+ return { codepoint, modifier: modValue - 1, eventType };
616
+ }
617
+
618
+ return null;
619
+ }
620
+
621
+ function matchesKittySequence(
622
+ data: string,
623
+ expectedCodepoint: number,
624
+ expectedModifier: number
625
+ ): boolean {
626
+ const parsed = parseKittySequence(data);
627
+ if (!parsed) return false;
628
+ const actualMod = parsed.modifier & ~LOCK_MASK;
629
+ const expectedMod = expectedModifier & ~LOCK_MASK;
630
+
631
+ // Check if modifiers match
632
+ if (actualMod !== expectedMod) return false;
633
+
634
+ // Primary match: codepoint matches directly
635
+ if (parsed.codepoint === expectedCodepoint) return true;
636
+
637
+ // Alternate match: use base layout key for non-Latin keyboard layouts.
638
+ // This allows Ctrl+С (Cyrillic) to match Ctrl+c (Latin) when terminal reports
639
+ // the base layout key (the key in standard PC-101 layout).
640
+ //
641
+ // Only fall back to base layout key when the codepoint is NOT already a
642
+ // recognized Latin letter (a-z) or symbol (e.g., /, -, [, ;, etc.).
643
+ // When the codepoint is a recognized key, it is authoritative regardless
644
+ // of physical key position. This prevents remapped layouts (Dvorak, Colemak,
645
+ // xremap, etc.) from causing false matches: both letters and symbols move
646
+ // to different physical positions, so Ctrl+K could falsely match Ctrl+V
647
+ // (letter remapping) and Ctrl+/ could falsely match Ctrl+[ (symbol remapping)
648
+ // if the base layout key were always considered.
649
+ if (parsed.baseLayoutKey !== undefined && parsed.baseLayoutKey === expectedCodepoint) {
650
+ const cp = parsed.codepoint;
651
+ const isLatinLetter = cp >= 97 && cp <= 122; // a-z
652
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(cp));
653
+ if (!isLatinLetter && !isKnownSymbol) return true;
654
+ }
655
+
656
+ return false;
657
+ }
658
+
659
+ /**
660
+ * Match xterm modifyOtherKeys format: CSI 27 ; modifiers ; keycode ~
661
+ * This is used by terminals when Kitty protocol is not enabled.
662
+ * Modifier values are 1-indexed: 2=shift, 3=alt, 5=ctrl, etc.
663
+ */
664
+ function matchesModifyOtherKeys(
665
+ data: string,
666
+ expectedKeycode: number,
667
+ expectedModifier: number
668
+ ): boolean {
669
+ const match = data.match(/^\x1b\[27;(\d+);(\d+)~$/);
670
+ if (!match) return false;
671
+ const modValue = parseInt(match[1]!, 10);
672
+ const keycode = parseInt(match[2]!, 10);
673
+ // Convert from 1-indexed xterm format to our 0-indexed format
674
+ const actualMod = modValue - 1;
675
+ return keycode === expectedKeycode && actualMod === expectedModifier;
676
+ }
677
+
678
+ // =============================================================================
679
+ // Generic Key Matching
680
+ // =============================================================================
681
+
682
+ /**
683
+ * Get the control character for a key.
684
+ * Uses the universal formula: code & 0x1f (mask to lower 5 bits)
685
+ *
686
+ * Works for:
687
+ * - Letters a-z → 1-26
688
+ * - Symbols [\]_ → 27, 28, 29, 31
689
+ * - Also maps - to same as _ (same physical key on US keyboards)
690
+ */
691
+ function rawCtrlChar(key: string): string | null {
692
+ const char = key.toLowerCase();
693
+ const code = char.charCodeAt(0);
694
+ if (
695
+ (code >= 97 && code <= 122) ||
696
+ char === "[" ||
697
+ char === "\\" ||
698
+ char === "]" ||
699
+ char === "_"
700
+ ) {
701
+ return String.fromCharCode(code & 0x1f);
702
+ }
703
+ // Handle - as _ (same physical key on US keyboards)
704
+ if (char === "-") {
705
+ return String.fromCharCode(31); // Same as Ctrl+_
706
+ }
707
+ return null;
708
+ }
709
+
710
+ function parseKeyId(
711
+ keyId: string
712
+ ): { key: string; ctrl: boolean; shift: boolean; alt: boolean } | null {
713
+ const parts = keyId.toLowerCase().split("+");
714
+ const key = parts[parts.length - 1];
715
+ if (!key) return null;
716
+ return {
717
+ key,
718
+ ctrl: parts.includes("ctrl"),
719
+ shift: parts.includes("shift"),
720
+ alt: parts.includes("alt"),
721
+ };
722
+ }
723
+
724
+ /**
725
+ * Match input data against a key identifier string.
726
+ *
727
+ * Supported key identifiers:
728
+ * - Single keys: "escape", "tab", "enter", "backspace", "delete", "home", "end", "space"
729
+ * - Arrow keys: "up", "down", "left", "right"
730
+ * - Ctrl combinations: "ctrl+c", "ctrl+z", etc.
731
+ * - Shift combinations: "shift+tab", "shift+enter"
732
+ * - Alt combinations: "alt+enter", "alt+backspace"
733
+ * - Combined modifiers: "shift+ctrl+p", "ctrl+alt+x"
734
+ *
735
+ * Use the Key helper for autocomplete: Key.ctrl("c"), Key.escape, Key.ctrlShift("p")
736
+ *
737
+ * @param data - Raw input data from terminal
738
+ * @param keyId - Key identifier (e.g., "ctrl+c", "escape", Key.ctrl("c"))
739
+ */
740
+ export function matchesKey(data: string, keyId: KeyId): boolean {
741
+ const parsed = parseKeyId(keyId);
742
+ if (!parsed) return false;
743
+
744
+ const { key, ctrl, shift, alt } = parsed;
745
+ let modifier = 0;
746
+ if (shift) modifier |= MODIFIERS.shift;
747
+ if (alt) modifier |= MODIFIERS.alt;
748
+ if (ctrl) modifier |= MODIFIERS.ctrl;
749
+
750
+ switch (key) {
751
+ case "escape":
752
+ case "esc":
753
+ if (modifier !== 0) return false;
754
+ return data === "\x1b" || matchesKittySequence(data, CODEPOINTS.escape, 0);
755
+
756
+ case "space":
757
+ if (!_kittyProtocolActive) {
758
+ if (ctrl && !alt && !shift && data === "\x00") {
759
+ return true;
760
+ }
761
+ if (alt && !ctrl && !shift && data === "\x1b ") {
762
+ return true;
763
+ }
764
+ }
765
+ if (modifier === 0) {
766
+ return data === " " || matchesKittySequence(data, CODEPOINTS.space, 0);
767
+ }
768
+ return matchesKittySequence(data, CODEPOINTS.space, modifier);
769
+
770
+ case "tab":
771
+ if (shift && !ctrl && !alt) {
772
+ return data === "\x1b[Z" || matchesKittySequence(data, CODEPOINTS.tab, MODIFIERS.shift);
773
+ }
774
+ if (modifier === 0) {
775
+ return data === "\t" || matchesKittySequence(data, CODEPOINTS.tab, 0);
776
+ }
777
+ return matchesKittySequence(data, CODEPOINTS.tab, modifier);
778
+
779
+ case "enter":
780
+ case "return":
781
+ if (shift && !ctrl && !alt) {
782
+ // CSI u sequences (standard Kitty protocol)
783
+ if (
784
+ matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.shift) ||
785
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.shift)
786
+ ) {
787
+ return true;
788
+ }
789
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
790
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.shift)) {
791
+ return true;
792
+ }
793
+ // When Kitty protocol is active, legacy sequences are custom terminal mappings
794
+ // \x1b\r = Kitty's "map shift+enter send_text all \e\r"
795
+ // \n = Ghostty's "keybind = shift+enter=text:\n"
796
+ if (_kittyProtocolActive) {
797
+ return data === "\x1b\r" || data === "\n";
798
+ }
799
+ return false;
800
+ }
801
+ if (alt && !ctrl && !shift) {
802
+ // CSI u sequences (standard Kitty protocol)
803
+ if (
804
+ matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.alt) ||
805
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.alt)
806
+ ) {
807
+ return true;
808
+ }
809
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
810
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.alt)) {
811
+ return true;
812
+ }
813
+ // \x1b\r is alt+enter only in legacy mode (no Kitty protocol)
814
+ // When Kitty protocol is active, alt+enter comes as CSI u sequence
815
+ if (!_kittyProtocolActive) {
816
+ return data === "\x1b\r";
817
+ }
818
+ return false;
819
+ }
820
+ if (modifier === 0) {
821
+ return (
822
+ data === "\r" ||
823
+ (!_kittyProtocolActive && data === "\n") ||
824
+ data === "\x1bOM" || // SS3 M (numpad enter in some terminals)
825
+ matchesKittySequence(data, CODEPOINTS.enter, 0) ||
826
+ matchesKittySequence(data, CODEPOINTS.kpEnter, 0)
827
+ );
828
+ }
829
+ return (
830
+ matchesKittySequence(data, CODEPOINTS.enter, modifier) ||
831
+ matchesKittySequence(data, CODEPOINTS.kpEnter, modifier)
832
+ );
833
+
834
+ case "backspace":
835
+ if (alt && !ctrl && !shift) {
836
+ if (data === "\x1b\x7f" || data === "\x1b\b") {
837
+ return true;
838
+ }
839
+ return matchesKittySequence(data, CODEPOINTS.backspace, MODIFIERS.alt);
840
+ }
841
+ if (modifier === 0) {
842
+ return (
843
+ data === "\x7f" || data === "\x08" || matchesKittySequence(data, CODEPOINTS.backspace, 0)
844
+ );
845
+ }
846
+ return matchesKittySequence(data, CODEPOINTS.backspace, modifier);
847
+
848
+ case "insert":
849
+ if (modifier === 0) {
850
+ return (
851
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.insert) ||
852
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, 0)
853
+ );
854
+ }
855
+ if (matchesLegacyModifierSequence(data, "insert", modifier)) {
856
+ return true;
857
+ }
858
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, modifier);
859
+
860
+ case "delete":
861
+ if (modifier === 0) {
862
+ return (
863
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.delete) ||
864
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, 0)
865
+ );
866
+ }
867
+ if (matchesLegacyModifierSequence(data, "delete", modifier)) {
868
+ return true;
869
+ }
870
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, modifier);
871
+
872
+ case "clear":
873
+ if (modifier === 0) {
874
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.clear);
875
+ }
876
+ return matchesLegacyModifierSequence(data, "clear", modifier);
877
+
878
+ case "home":
879
+ if (modifier === 0) {
880
+ return (
881
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.home) ||
882
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, 0)
883
+ );
884
+ }
885
+ if (matchesLegacyModifierSequence(data, "home", modifier)) {
886
+ return true;
887
+ }
888
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, modifier);
889
+
890
+ case "end":
891
+ if (modifier === 0) {
892
+ return (
893
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.end) ||
894
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, 0)
895
+ );
896
+ }
897
+ if (matchesLegacyModifierSequence(data, "end", modifier)) {
898
+ return true;
899
+ }
900
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, modifier);
901
+
902
+ case "pageup":
903
+ if (modifier === 0) {
904
+ return (
905
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageUp) ||
906
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, 0)
907
+ );
908
+ }
909
+ if (matchesLegacyModifierSequence(data, "pageUp", modifier)) {
910
+ return true;
911
+ }
912
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, modifier);
913
+
914
+ case "pagedown":
915
+ if (modifier === 0) {
916
+ return (
917
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageDown) ||
918
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, 0)
919
+ );
920
+ }
921
+ if (matchesLegacyModifierSequence(data, "pageDown", modifier)) {
922
+ return true;
923
+ }
924
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, modifier);
925
+
926
+ case "up":
927
+ if (alt && !ctrl && !shift) {
928
+ return data === "\x1bp" || matchesKittySequence(data, ARROW_CODEPOINTS.up, MODIFIERS.alt);
929
+ }
930
+ if (modifier === 0) {
931
+ return (
932
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.up) ||
933
+ matchesKittySequence(data, ARROW_CODEPOINTS.up, 0)
934
+ );
935
+ }
936
+ if (matchesLegacyModifierSequence(data, "up", modifier)) {
937
+ return true;
938
+ }
939
+ return matchesKittySequence(data, ARROW_CODEPOINTS.up, modifier);
940
+
941
+ case "down":
942
+ if (alt && !ctrl && !shift) {
943
+ return data === "\x1bn" || matchesKittySequence(data, ARROW_CODEPOINTS.down, MODIFIERS.alt);
944
+ }
945
+ if (modifier === 0) {
946
+ return (
947
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.down) ||
948
+ matchesKittySequence(data, ARROW_CODEPOINTS.down, 0)
949
+ );
950
+ }
951
+ if (matchesLegacyModifierSequence(data, "down", modifier)) {
952
+ return true;
953
+ }
954
+ return matchesKittySequence(data, ARROW_CODEPOINTS.down, modifier);
955
+
956
+ case "left":
957
+ if (alt && !ctrl && !shift) {
958
+ return (
959
+ data === "\x1b[1;3D" ||
960
+ (!_kittyProtocolActive && data === "\x1bB") ||
961
+ data === "\x1bb" ||
962
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.alt)
963
+ );
964
+ }
965
+ if (ctrl && !alt && !shift) {
966
+ return (
967
+ data === "\x1b[1;5D" ||
968
+ matchesLegacyModifierSequence(data, "left", MODIFIERS.ctrl) ||
969
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.ctrl)
970
+ );
971
+ }
972
+ if (modifier === 0) {
973
+ return (
974
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.left) ||
975
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, 0)
976
+ );
977
+ }
978
+ if (matchesLegacyModifierSequence(data, "left", modifier)) {
979
+ return true;
980
+ }
981
+ return matchesKittySequence(data, ARROW_CODEPOINTS.left, modifier);
982
+
983
+ case "right":
984
+ if (alt && !ctrl && !shift) {
985
+ return (
986
+ data === "\x1b[1;3C" ||
987
+ (!_kittyProtocolActive && data === "\x1bF") ||
988
+ data === "\x1bf" ||
989
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.alt)
990
+ );
991
+ }
992
+ if (ctrl && !alt && !shift) {
993
+ return (
994
+ data === "\x1b[1;5C" ||
995
+ matchesLegacyModifierSequence(data, "right", MODIFIERS.ctrl) ||
996
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.ctrl)
997
+ );
998
+ }
999
+ if (modifier === 0) {
1000
+ return (
1001
+ matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.right) ||
1002
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, 0)
1003
+ );
1004
+ }
1005
+ if (matchesLegacyModifierSequence(data, "right", modifier)) {
1006
+ return true;
1007
+ }
1008
+ return matchesKittySequence(data, ARROW_CODEPOINTS.right, modifier);
1009
+
1010
+ case "f1":
1011
+ case "f2":
1012
+ case "f3":
1013
+ case "f4":
1014
+ case "f5":
1015
+ case "f6":
1016
+ case "f7":
1017
+ case "f8":
1018
+ case "f9":
1019
+ case "f10":
1020
+ case "f11":
1021
+ case "f12": {
1022
+ if (modifier !== 0) {
1023
+ return false;
1024
+ }
1025
+ const functionKey = key as keyof typeof LEGACY_KEY_SEQUENCES;
1026
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES[functionKey]);
1027
+ }
1028
+ }
1029
+
1030
+ // Handle single letter keys (a-z) and some symbols
1031
+ if (key.length === 1 && ((key >= "a" && key <= "z") || SYMBOL_KEYS.has(key))) {
1032
+ const codepoint = key.charCodeAt(0);
1033
+ const rawCtrl = rawCtrlChar(key);
1034
+
1035
+ if (ctrl && alt && !shift && !_kittyProtocolActive && rawCtrl) {
1036
+ // Legacy: ctrl+alt+key is ESC followed by the control character
1037
+ return data === `\x1b${rawCtrl}`;
1038
+ }
1039
+
1040
+ if (alt && !ctrl && !shift && !_kittyProtocolActive && key >= "a" && key <= "z") {
1041
+ // Legacy: alt+letter is ESC followed by the letter
1042
+ if (data === `\x1b${key}`) return true;
1043
+ }
1044
+
1045
+ if (ctrl && !shift && !alt) {
1046
+ // Legacy: ctrl+key sends the control character
1047
+ if (rawCtrl && data === rawCtrl) return true;
1048
+ return matchesKittySequence(data, codepoint, MODIFIERS.ctrl);
1049
+ }
1050
+
1051
+ if (ctrl && shift && !alt) {
1052
+ return matchesKittySequence(data, codepoint, MODIFIERS.shift + MODIFIERS.ctrl);
1053
+ }
1054
+
1055
+ if (shift && !ctrl && !alt) {
1056
+ // Legacy: shift+letter produces uppercase
1057
+ if (data === key.toUpperCase()) return true;
1058
+ return matchesKittySequence(data, codepoint, MODIFIERS.shift);
1059
+ }
1060
+
1061
+ if (modifier !== 0) {
1062
+ return matchesKittySequence(data, codepoint, modifier);
1063
+ }
1064
+
1065
+ // Check both raw char and Kitty sequence (needed for release events)
1066
+ return data === key || matchesKittySequence(data, codepoint, 0);
1067
+ }
1068
+
1069
+ return false;
1070
+ }
1071
+
1072
+ /**
1073
+ * Parse input data and return the key identifier if recognized.
1074
+ *
1075
+ * @param data - Raw input data from terminal
1076
+ * @returns Key identifier string (e.g., "ctrl+c") or undefined
1077
+ */
1078
+ export function parseKey(data: string): string | undefined {
1079
+ const kitty = parseKittySequence(data);
1080
+ if (kitty) {
1081
+ const { codepoint, baseLayoutKey, modifier } = kitty;
1082
+ const mods: string[] = [];
1083
+ const effectiveMod = modifier & ~LOCK_MASK;
1084
+ if (effectiveMod & MODIFIERS.shift) mods.push("shift");
1085
+ if (effectiveMod & MODIFIERS.ctrl) mods.push("ctrl");
1086
+ if (effectiveMod & MODIFIERS.alt) mods.push("alt");
1087
+
1088
+ // Use base layout key only when codepoint is not a recognized Latin
1089
+ // letter (a-z) or symbol (/, -, [, ;, etc.). For those, the codepoint
1090
+ // is authoritative regardless of physical key position. This prevents
1091
+ // remapped layouts (Dvorak, Colemak, xremap, etc.) from reporting the
1092
+ // wrong key name based on the QWERTY physical position.
1093
+ const isLatinLetter = codepoint >= 97 && codepoint <= 122; // a-z
1094
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(codepoint));
1095
+ const effectiveCodepoint =
1096
+ isLatinLetter || isKnownSymbol ? codepoint : (baseLayoutKey ?? codepoint);
1097
+
1098
+ let keyName: string | undefined;
1099
+ if (effectiveCodepoint === CODEPOINTS.escape) keyName = "escape";
1100
+ else if (effectiveCodepoint === CODEPOINTS.tab) keyName = "tab";
1101
+ else if (effectiveCodepoint === CODEPOINTS.enter || effectiveCodepoint === CODEPOINTS.kpEnter)
1102
+ keyName = "enter";
1103
+ else if (effectiveCodepoint === CODEPOINTS.space) keyName = "space";
1104
+ else if (effectiveCodepoint === CODEPOINTS.backspace) keyName = "backspace";
1105
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.delete) keyName = "delete";
1106
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.insert) keyName = "insert";
1107
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.home) keyName = "home";
1108
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.end) keyName = "end";
1109
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageUp) keyName = "pageUp";
1110
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageDown) keyName = "pageDown";
1111
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.up) keyName = "up";
1112
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.down) keyName = "down";
1113
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.left) keyName = "left";
1114
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.right) keyName = "right";
1115
+ else if (effectiveCodepoint >= 97 && effectiveCodepoint <= 122)
1116
+ keyName = String.fromCharCode(effectiveCodepoint);
1117
+ else if (SYMBOL_KEYS.has(String.fromCharCode(effectiveCodepoint)))
1118
+ keyName = String.fromCharCode(effectiveCodepoint);
1119
+
1120
+ if (keyName) {
1121
+ return mods.length > 0 ? `${mods.join("+")}+${keyName}` : keyName;
1122
+ }
1123
+ }
1124
+
1125
+ // Mode-aware legacy sequences
1126
+ // When Kitty protocol is active, ambiguous sequences are interpreted as custom terminal mappings:
1127
+ // - \x1b\r = shift+enter (Kitty mapping), not alt+enter
1128
+ // - \n = shift+enter (Ghostty mapping)
1129
+ if (_kittyProtocolActive) {
1130
+ if (data === "\x1b\r" || data === "\n") return "shift+enter";
1131
+ }
1132
+
1133
+ const legacySequenceKeyId = LEGACY_SEQUENCE_KEY_IDS[data];
1134
+ if (legacySequenceKeyId) return legacySequenceKeyId;
1135
+
1136
+ // Legacy sequences (used when Kitty protocol is not active, or for unambiguous sequences)
1137
+ if (data === "\x1b") return "escape";
1138
+ if (data === "\x1c") return "ctrl+\\";
1139
+ if (data === "\x1d") return "ctrl+]";
1140
+ if (data === "\x1f") return "ctrl+-";
1141
+ if (data === "\x1b\x1b") return "ctrl+alt+[";
1142
+ if (data === "\x1b\x1c") return "ctrl+alt+\\";
1143
+ if (data === "\x1b\x1d") return "ctrl+alt+]";
1144
+ if (data === "\x1b\x1f") return "ctrl+alt+-";
1145
+ if (data === "\t") return "tab";
1146
+ if (data === "\r" || (!_kittyProtocolActive && data === "\n") || data === "\x1bOM")
1147
+ return "enter";
1148
+ if (data === "\x00") return "ctrl+space";
1149
+ if (data === " ") return "space";
1150
+ if (data === "\x7f" || data === "\x08") return "backspace";
1151
+ if (data === "\x1b[Z") return "shift+tab";
1152
+ if (!_kittyProtocolActive && data === "\x1b\r") return "alt+enter";
1153
+ if (!_kittyProtocolActive && data === "\x1b ") return "alt+space";
1154
+ if (data === "\x1b\x7f" || data === "\x1b\b") return "alt+backspace";
1155
+ if (!_kittyProtocolActive && data === "\x1bB") return "alt+left";
1156
+ if (!_kittyProtocolActive && data === "\x1bF") return "alt+right";
1157
+ if (!_kittyProtocolActive && data.length === 2 && data[0] === "\x1b") {
1158
+ const code = data.charCodeAt(1);
1159
+ if (code >= 1 && code <= 26) {
1160
+ return `ctrl+alt+${String.fromCharCode(code + 96)}`;
1161
+ }
1162
+ // Legacy alt+letter (ESC followed by letter a-z)
1163
+ if (code >= 97 && code <= 122) {
1164
+ return `alt+${String.fromCharCode(code)}`;
1165
+ }
1166
+ }
1167
+ if (data === "\x1b[A") return "up";
1168
+ if (data === "\x1b[B") return "down";
1169
+ if (data === "\x1b[C") return "right";
1170
+ if (data === "\x1b[D") return "left";
1171
+ if (data === "\x1b[H" || data === "\x1bOH") return "home";
1172
+ if (data === "\x1b[F" || data === "\x1bOF") return "end";
1173
+ if (data === "\x1b[3~") return "delete";
1174
+ if (data === "\x1b[5~") return "pageUp";
1175
+ if (data === "\x1b[6~") return "pageDown";
1176
+
1177
+ // Raw Ctrl+letter
1178
+ if (data.length === 1) {
1179
+ const code = data.charCodeAt(0);
1180
+ if (code >= 1 && code <= 26) {
1181
+ return `ctrl+${String.fromCharCode(code + 96)}`;
1182
+ }
1183
+ if (code >= 32 && code <= 126) {
1184
+ return data;
1185
+ }
1186
+ }
1187
+
1188
+ return undefined;
1189
+ }