nastech-tui 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (424) hide show
  1. package/.prettierrc +11 -0
  2. package/README.md +346 -0
  3. package/eslint.config.mjs +111 -0
  4. package/package.json +51 -0
  5. package/packages/nastech-ink/ambient.d.ts +83 -0
  6. package/packages/nastech-ink/index.d.ts +40 -0
  7. package/packages/nastech-ink/index.js +1 -0
  8. package/packages/nastech-ink/nastech-ink/ambient.d.ts +83 -0
  9. package/packages/nastech-ink/nastech-ink/index.d.ts +40 -0
  10. package/packages/nastech-ink/nastech-ink/index.js +1 -0
  11. package/packages/nastech-ink/nastech-ink/package.json +54 -0
  12. package/packages/nastech-ink/nastech-ink/src/bootstrap/state.ts +9 -0
  13. package/packages/nastech-ink/nastech-ink/src/entry-exports.ts +32 -0
  14. package/packages/nastech-ink/nastech-ink/src/hooks/use-stderr.ts +15 -0
  15. package/packages/nastech-ink/nastech-ink/src/hooks/use-stdout.ts +15 -0
  16. package/packages/nastech-ink/nastech-ink/src/ink/Ansi.tsx +435 -0
  17. package/packages/nastech-ink/nastech-ink/src/ink/app-mouse.test.ts +123 -0
  18. package/packages/nastech-ink/nastech-ink/src/ink/bidi.ts +145 -0
  19. package/packages/nastech-ink/nastech-ink/src/ink/cache-eviction.ts +45 -0
  20. package/packages/nastech-ink/nastech-ink/src/ink/clearTerminal.ts +68 -0
  21. package/packages/nastech-ink/nastech-ink/src/ink/colorize.test.ts +60 -0
  22. package/packages/nastech-ink/nastech-ink/src/ink/colorize.ts +277 -0
  23. package/packages/nastech-ink/nastech-ink/src/ink/components/AlternateScreen.tsx +133 -0
  24. package/packages/nastech-ink/nastech-ink/src/ink/components/App.tsx +830 -0
  25. package/packages/nastech-ink/nastech-ink/src/ink/components/AppContext.ts +20 -0
  26. package/packages/nastech-ink/nastech-ink/src/ink/components/Box.tsx +294 -0
  27. package/packages/nastech-ink/nastech-ink/src/ink/components/Button.tsx +236 -0
  28. package/packages/nastech-ink/nastech-ink/src/ink/components/ClockContext.tsx +133 -0
  29. package/packages/nastech-ink/nastech-ink/src/ink/components/CursorAdvanceContext.ts +35 -0
  30. package/packages/nastech-ink/nastech-ink/src/ink/components/CursorDeclarationContext.ts +28 -0
  31. package/packages/nastech-ink/nastech-ink/src/ink/components/ErrorOverview.tsx +130 -0
  32. package/packages/nastech-ink/nastech-ink/src/ink/components/Link.tsx +38 -0
  33. package/packages/nastech-ink/nastech-ink/src/ink/components/Newline.tsx +43 -0
  34. package/packages/nastech-ink/nastech-ink/src/ink/components/NoSelect.tsx +73 -0
  35. package/packages/nastech-ink/nastech-ink/src/ink/components/RawAnsi.tsx +61 -0
  36. package/packages/nastech-ink/nastech-ink/src/ink/components/ScrollBox.tsx +290 -0
  37. package/packages/nastech-ink/nastech-ink/src/ink/components/Spacer.tsx +23 -0
  38. package/packages/nastech-ink/nastech-ink/src/ink/components/StdinContext.ts +25 -0
  39. package/packages/nastech-ink/nastech-ink/src/ink/components/TerminalFocusContext.tsx +63 -0
  40. package/packages/nastech-ink/nastech-ink/src/ink/components/TerminalSizeContext.tsx +7 -0
  41. package/packages/nastech-ink/nastech-ink/src/ink/components/Text.test.ts +38 -0
  42. package/packages/nastech-ink/nastech-ink/src/ink/components/Text.tsx +336 -0
  43. package/packages/nastech-ink/nastech-ink/src/ink/constants.ts +6 -0
  44. package/packages/nastech-ink/nastech-ink/src/ink/cursor.ts +5 -0
  45. package/packages/nastech-ink/nastech-ink/src/ink/devtools.ts +2 -0
  46. package/packages/nastech-ink/nastech-ink/src/ink/dom.ts +495 -0
  47. package/packages/nastech-ink/nastech-ink/src/ink/events/click-event.ts +38 -0
  48. package/packages/nastech-ink/nastech-ink/src/ink/events/cmd-shortcuts.test.ts +65 -0
  49. package/packages/nastech-ink/nastech-ink/src/ink/events/dispatcher.ts +242 -0
  50. package/packages/nastech-ink/nastech-ink/src/ink/events/emitter.ts +40 -0
  51. package/packages/nastech-ink/nastech-ink/src/ink/events/event-handlers.ts +84 -0
  52. package/packages/nastech-ink/nastech-ink/src/ink/events/event.ts +11 -0
  53. package/packages/nastech-ink/nastech-ink/src/ink/events/focus-event.ts +18 -0
  54. package/packages/nastech-ink/nastech-ink/src/ink/events/input-event.ts +176 -0
  55. package/packages/nastech-ink/nastech-ink/src/ink/events/keyboard-event.ts +57 -0
  56. package/packages/nastech-ink/nastech-ink/src/ink/events/mouse-event.ts +18 -0
  57. package/packages/nastech-ink/nastech-ink/src/ink/events/paste-event.ts +10 -0
  58. package/packages/nastech-ink/nastech-ink/src/ink/events/resize-event.ts +12 -0
  59. package/packages/nastech-ink/nastech-ink/src/ink/events/terminal-event.ts +107 -0
  60. package/packages/nastech-ink/nastech-ink/src/ink/events/terminal-focus-event.ts +19 -0
  61. package/packages/nastech-ink/nastech-ink/src/ink/focus.ts +219 -0
  62. package/packages/nastech-ink/nastech-ink/src/ink/frame.ts +124 -0
  63. package/packages/nastech-ink/nastech-ink/src/ink/get-max-width.ts +27 -0
  64. package/packages/nastech-ink/nastech-ink/src/ink/global.d.ts +1 -0
  65. package/packages/nastech-ink/nastech-ink/src/ink/hit-test.test.ts +38 -0
  66. package/packages/nastech-ink/nastech-ink/src/ink/hit-test.ts +224 -0
  67. package/packages/nastech-ink/nastech-ink/src/ink/hooks/use-animation-frame.ts +62 -0
  68. package/packages/nastech-ink/nastech-ink/src/ink/hooks/use-app.ts +9 -0
  69. package/packages/nastech-ink/nastech-ink/src/ink/hooks/use-cursor-advance.ts +33 -0
  70. package/packages/nastech-ink/nastech-ink/src/ink/hooks/use-declared-cursor.ts +75 -0
  71. package/packages/nastech-ink/nastech-ink/src/ink/hooks/use-external-process.ts +27 -0
  72. package/packages/nastech-ink/nastech-ink/src/ink/hooks/use-input.ts +95 -0
  73. package/packages/nastech-ink/nastech-ink/src/ink/hooks/use-interval.ts +71 -0
  74. package/packages/nastech-ink/package.json +57 -0
  75. package/packages/nastech-ink/src/bootstrap/state.ts +9 -0
  76. package/packages/nastech-ink/src/entry-exports.ts +32 -0
  77. package/packages/nastech-ink/src/hooks/use-stderr.ts +15 -0
  78. package/packages/nastech-ink/src/hooks/use-stdout.ts +15 -0
  79. package/packages/nastech-ink/src/ink/Ansi.tsx +435 -0
  80. package/packages/nastech-ink/src/ink/app-mouse.test.ts +123 -0
  81. package/packages/nastech-ink/src/ink/app-rawmode-mouse.test.ts +91 -0
  82. package/packages/nastech-ink/src/ink/bidi.ts +145 -0
  83. package/packages/nastech-ink/src/ink/cache-eviction.ts +45 -0
  84. package/packages/nastech-ink/src/ink/clearTerminal.ts +68 -0
  85. package/packages/nastech-ink/src/ink/colorize.test.ts +60 -0
  86. package/packages/nastech-ink/src/ink/colorize.ts +277 -0
  87. package/packages/nastech-ink/src/ink/components/AlternateScreen.tsx +133 -0
  88. package/packages/nastech-ink/src/ink/components/App.tsx +855 -0
  89. package/packages/nastech-ink/src/ink/components/AppContext.ts +20 -0
  90. package/packages/nastech-ink/src/ink/components/Box.tsx +294 -0
  91. package/packages/nastech-ink/src/ink/components/Button.tsx +236 -0
  92. package/packages/nastech-ink/src/ink/components/ClockContext.tsx +133 -0
  93. package/packages/nastech-ink/src/ink/components/CursorAdvanceContext.ts +35 -0
  94. package/packages/nastech-ink/src/ink/components/CursorDeclarationContext.ts +28 -0
  95. package/packages/nastech-ink/src/ink/components/ErrorOverview.tsx +130 -0
  96. package/packages/nastech-ink/src/ink/components/Link.tsx +38 -0
  97. package/packages/nastech-ink/src/ink/components/Newline.tsx +43 -0
  98. package/packages/nastech-ink/src/ink/components/NoSelect.tsx +73 -0
  99. package/packages/nastech-ink/src/ink/components/RawAnsi.tsx +61 -0
  100. package/packages/nastech-ink/src/ink/components/ScrollBox.tsx +290 -0
  101. package/packages/nastech-ink/src/ink/components/Spacer.tsx +23 -0
  102. package/packages/nastech-ink/src/ink/components/StdinContext.ts +25 -0
  103. package/packages/nastech-ink/src/ink/components/TerminalFocusContext.tsx +63 -0
  104. package/packages/nastech-ink/src/ink/components/TerminalSizeContext.tsx +7 -0
  105. package/packages/nastech-ink/src/ink/components/Text.test.ts +38 -0
  106. package/packages/nastech-ink/src/ink/components/Text.tsx +336 -0
  107. package/packages/nastech-ink/src/ink/constants.ts +6 -0
  108. package/packages/nastech-ink/src/ink/cursor.ts +5 -0
  109. package/packages/nastech-ink/src/ink/devtools.ts +2 -0
  110. package/packages/nastech-ink/src/ink/dom.ts +495 -0
  111. package/packages/nastech-ink/src/ink/events/click-event.ts +38 -0
  112. package/packages/nastech-ink/src/ink/events/cmd-shortcuts.test.ts +65 -0
  113. package/packages/nastech-ink/src/ink/events/dispatcher.ts +242 -0
  114. package/packages/nastech-ink/src/ink/events/emitter.ts +40 -0
  115. package/packages/nastech-ink/src/ink/events/event-handlers.ts +84 -0
  116. package/packages/nastech-ink/src/ink/events/event.ts +11 -0
  117. package/packages/nastech-ink/src/ink/events/focus-event.ts +18 -0
  118. package/packages/nastech-ink/src/ink/events/input-event.ts +176 -0
  119. package/packages/nastech-ink/src/ink/events/keyboard-event.ts +57 -0
  120. package/packages/nastech-ink/src/ink/events/mouse-event.ts +18 -0
  121. package/packages/nastech-ink/src/ink/events/paste-event.ts +10 -0
  122. package/packages/nastech-ink/src/ink/events/resize-event.ts +12 -0
  123. package/packages/nastech-ink/src/ink/events/terminal-event.ts +107 -0
  124. package/packages/nastech-ink/src/ink/events/terminal-focus-event.ts +19 -0
  125. package/packages/nastech-ink/src/ink/focus.ts +219 -0
  126. package/packages/nastech-ink/src/ink/frame.ts +124 -0
  127. package/packages/nastech-ink/src/ink/get-max-width.ts +27 -0
  128. package/packages/nastech-ink/src/ink/global.d.ts +1 -0
  129. package/packages/nastech-ink/src/ink/hit-test.test.ts +38 -0
  130. package/packages/nastech-ink/src/ink/hit-test.ts +224 -0
  131. package/packages/nastech-ink/src/ink/hooks/use-animation-frame.ts +62 -0
  132. package/packages/nastech-ink/src/ink/hooks/use-app.ts +9 -0
  133. package/packages/nastech-ink/src/ink/hooks/use-cursor-advance.ts +33 -0
  134. package/packages/nastech-ink/src/ink/hooks/use-declared-cursor.ts +75 -0
  135. package/packages/nastech-ink/src/ink/hooks/use-external-process.ts +27 -0
  136. package/packages/nastech-ink/src/ink/hooks/use-input.ts +95 -0
  137. package/packages/nastech-ink/src/ink/hooks/use-interval.ts +71 -0
  138. package/packages/nastech-ink/src/ink/hooks/use-search-highlight.ts +56 -0
  139. package/packages/nastech-ink/src/ink/hooks/use-selection.ts +101 -0
  140. package/packages/nastech-ink/src/ink/hooks/use-stdin.ts +9 -0
  141. package/packages/nastech-ink/src/ink/hooks/use-tab-status.ts +71 -0
  142. package/packages/nastech-ink/src/ink/hooks/use-terminal-focus.ts +18 -0
  143. package/packages/nastech-ink/src/ink/hooks/use-terminal-title.ts +34 -0
  144. package/packages/nastech-ink/src/ink/hooks/use-terminal-viewport.ts +100 -0
  145. package/packages/nastech-ink/src/ink/hyperlinkHover.ts +52 -0
  146. package/packages/nastech-ink/src/ink/ink-cursor-advance.test.ts +234 -0
  147. package/packages/nastech-ink/src/ink/ink-resize.test.ts +50 -0
  148. package/packages/nastech-ink/src/ink/ink.tsx +2705 -0
  149. package/packages/nastech-ink/src/ink/instances.ts +10 -0
  150. package/packages/nastech-ink/src/ink/layout/engine.ts +6 -0
  151. package/packages/nastech-ink/src/ink/layout/geometry.ts +98 -0
  152. package/packages/nastech-ink/src/ink/layout/node.ts +145 -0
  153. package/packages/nastech-ink/src/ink/layout/yoga.ts +313 -0
  154. package/packages/nastech-ink/src/ink/line-width-cache.ts +38 -0
  155. package/packages/nastech-ink/src/ink/log-update.test.ts +223 -0
  156. package/packages/nastech-ink/src/ink/log-update.ts +752 -0
  157. package/packages/nastech-ink/src/ink/lru.ts +14 -0
  158. package/packages/nastech-ink/src/ink/measure-element.ts +23 -0
  159. package/packages/nastech-ink/src/ink/measure-text.ts +50 -0
  160. package/packages/nastech-ink/src/ink/node-cache.ts +53 -0
  161. package/packages/nastech-ink/src/ink/optimizer.ts +99 -0
  162. package/packages/nastech-ink/src/ink/output.ts +845 -0
  163. package/packages/nastech-ink/src/ink/parse-keypress.test.ts +133 -0
  164. package/packages/nastech-ink/src/ink/parse-keypress.ts +848 -0
  165. package/packages/nastech-ink/src/ink/reconciler.ts +382 -0
  166. package/packages/nastech-ink/src/ink/render-border.ts +206 -0
  167. package/packages/nastech-ink/src/ink/render-node-to-output.ts +1582 -0
  168. package/packages/nastech-ink/src/ink/render-to-screen.ts +236 -0
  169. package/packages/nastech-ink/src/ink/renderer.ts +169 -0
  170. package/packages/nastech-ink/src/ink/root.ts +204 -0
  171. package/packages/nastech-ink/src/ink/screen.ts +1590 -0
  172. package/packages/nastech-ink/src/ink/searchHighlight.ts +91 -0
  173. package/packages/nastech-ink/src/ink/selection.test.ts +82 -0
  174. package/packages/nastech-ink/src/ink/selection.ts +1143 -0
  175. package/packages/nastech-ink/src/ink/squash-text-nodes.ts +74 -0
  176. package/packages/nastech-ink/src/ink/stringWidth.ts +341 -0
  177. package/packages/nastech-ink/src/ink/styles.ts +750 -0
  178. package/packages/nastech-ink/src/ink/supports-hyperlinks.ts +51 -0
  179. package/packages/nastech-ink/src/ink/tabstops.ts +44 -0
  180. package/packages/nastech-ink/src/ink/terminal-focus-state.ts +52 -0
  181. package/packages/nastech-ink/src/ink/terminal-querier.ts +222 -0
  182. package/packages/nastech-ink/src/ink/terminal.test.ts +15 -0
  183. package/packages/nastech-ink/src/ink/terminal.ts +299 -0
  184. package/packages/nastech-ink/src/ink/termio/ansi.ts +75 -0
  185. package/packages/nastech-ink/src/ink/termio/csi.ts +334 -0
  186. package/packages/nastech-ink/src/ink/termio/dec.ts +99 -0
  187. package/packages/nastech-ink/src/ink/termio/esc.ts +69 -0
  188. package/packages/nastech-ink/src/ink/termio/osc.test.ts +191 -0
  189. package/packages/nastech-ink/src/ink/termio/osc.ts +724 -0
  190. package/packages/nastech-ink/src/ink/termio/parser.ts +467 -0
  191. package/packages/nastech-ink/src/ink/termio/sgr.ts +362 -0
  192. package/packages/nastech-ink/src/ink/termio/tokenize.test.ts +185 -0
  193. package/packages/nastech-ink/src/ink/termio/tokenize.ts +350 -0
  194. package/packages/nastech-ink/src/ink/termio/types.ts +230 -0
  195. package/packages/nastech-ink/src/ink/termio.ts +42 -0
  196. package/packages/nastech-ink/src/ink/useTerminalNotification.ts +110 -0
  197. package/packages/nastech-ink/src/ink/warn.ts +15 -0
  198. package/packages/nastech-ink/src/ink/widest-line.ts +22 -0
  199. package/packages/nastech-ink/src/ink/wrap-text.test.ts +17 -0
  200. package/packages/nastech-ink/src/ink/wrap-text.ts +144 -0
  201. package/packages/nastech-ink/src/ink/wrapAnsi.ts +13 -0
  202. package/packages/nastech-ink/src/native-ts/yoga-layout/enums.ts +112 -0
  203. package/packages/nastech-ink/src/native-ts/yoga-layout/index.ts +2326 -0
  204. package/packages/nastech-ink/src/utils/debug.ts +6 -0
  205. package/packages/nastech-ink/src/utils/earlyInput.ts +131 -0
  206. package/packages/nastech-ink/src/utils/env.ts +66 -0
  207. package/packages/nastech-ink/src/utils/envUtils.ts +13 -0
  208. package/packages/nastech-ink/src/utils/execFileNoThrow.test.ts +146 -0
  209. package/packages/nastech-ink/src/utils/execFileNoThrow.ts +115 -0
  210. package/packages/nastech-ink/src/utils/fullscreen.ts +3 -0
  211. package/packages/nastech-ink/src/utils/intl.ts +87 -0
  212. package/packages/nastech-ink/src/utils/log.ts +7 -0
  213. package/packages/nastech-ink/src/utils/semver.ts +57 -0
  214. package/packages/nastech-ink/src/utils/sliceAnsi.ts +106 -0
  215. package/packages/nastech-ink/text-input.d.ts +2 -0
  216. package/packages/nastech-ink/text-input.js +1 -0
  217. package/scripts/build.mjs +61 -0
  218. package/scripts/profile-tui.mjs +121 -0
  219. package/src/__tests__/activeSessionSwitcher.test.ts +157 -0
  220. package/src/__tests__/appChromeStatusRule.test.tsx +84 -0
  221. package/src/__tests__/appChromeStatusRuleDevCredits.test.tsx +73 -0
  222. package/src/__tests__/approvalAction.test.ts +50 -0
  223. package/src/__tests__/asCommandDispatch.test.ts +27 -0
  224. package/src/__tests__/blockLayout.test.ts +122 -0
  225. package/src/__tests__/clipboard.test.ts +369 -0
  226. package/src/__tests__/constants.test.ts +53 -0
  227. package/src/__tests__/createGatewayEventHandler.test.ts +1091 -0
  228. package/src/__tests__/createSlashHandler.test.ts +822 -0
  229. package/src/__tests__/creditsCommand.test.ts +144 -0
  230. package/src/__tests__/cursorDriftRegression.test.ts +114 -0
  231. package/src/__tests__/details.test.ts +115 -0
  232. package/src/__tests__/emoji.test.ts +64 -0
  233. package/src/__tests__/externalLink.test.ts +144 -0
  234. package/src/__tests__/forceTruecolor.test.ts +191 -0
  235. package/src/__tests__/gatewayClient.test.ts +394 -0
  236. package/src/__tests__/gatewayRecovery.test.ts +47 -0
  237. package/src/__tests__/markdown.test.ts +331 -0
  238. package/src/__tests__/mathUnicode.test.ts +293 -0
  239. package/src/__tests__/memoryMonitor.test.ts +102 -0
  240. package/src/__tests__/messageLine.test.ts +19 -0
  241. package/src/__tests__/messages.test.ts +92 -0
  242. package/src/__tests__/orchestratorPromptSession.test.ts +64 -0
  243. package/src/__tests__/osc52.test.ts +67 -0
  244. package/src/__tests__/parentLog.test.ts +75 -0
  245. package/src/__tests__/paths.test.ts +70 -0
  246. package/src/__tests__/platform.test.ts +556 -0
  247. package/src/__tests__/precisionWheel.test.ts +44 -0
  248. package/src/__tests__/prompt.test.ts +31 -0
  249. package/src/__tests__/providers.test.ts +65 -0
  250. package/src/__tests__/reasoning.test.ts +76 -0
  251. package/src/__tests__/rpc.test.ts +27 -0
  252. package/src/__tests__/scroll.test.ts +99 -0
  253. package/src/__tests__/slashParity.test.ts +123 -0
  254. package/src/__tests__/spawnHistoryStore.test.ts +46 -0
  255. package/src/__tests__/stateIsolation.test.ts +46 -0
  256. package/src/__tests__/statusBarTicker.test.ts +18 -0
  257. package/src/__tests__/statusRule.test.ts +32 -0
  258. package/src/__tests__/streamingMarkdown.test.ts +121 -0
  259. package/src/__tests__/subagentTree.test.ts +407 -0
  260. package/src/__tests__/syntax.test.ts +45 -0
  261. package/src/__tests__/terminalModes.test.ts +39 -0
  262. package/src/__tests__/terminalParity.test.ts +77 -0
  263. package/src/__tests__/terminalSetup.test.ts +386 -0
  264. package/src/__tests__/termux.test.ts +35 -0
  265. package/src/__tests__/termuxComposerLayout.test.ts +40 -0
  266. package/src/__tests__/text.test.ts +233 -0
  267. package/src/__tests__/textInputBurstInput.test.ts +40 -0
  268. package/src/__tests__/textInputCursorSourceOfTruth.test.ts +50 -0
  269. package/src/__tests__/textInputFastEcho.test.ts +200 -0
  270. package/src/__tests__/textInputLineNav.test.ts +55 -0
  271. package/src/__tests__/textInputPassThrough.test.ts +59 -0
  272. package/src/__tests__/textInputRightClick.test.ts +48 -0
  273. package/src/__tests__/textInputWrap.test.ts +151 -0
  274. package/src/__tests__/theme.test.ts +311 -0
  275. package/src/__tests__/turnControllerNotice.test.ts +43 -0
  276. package/src/__tests__/turnStore.test.ts +66 -0
  277. package/src/__tests__/useCompletion.test.ts +35 -0
  278. package/src/__tests__/useComposerState.test.ts +59 -0
  279. package/src/__tests__/useConfigSync.test.ts +460 -0
  280. package/src/__tests__/useInputHandlers.test.ts +77 -0
  281. package/src/__tests__/useQueue.test.ts +28 -0
  282. package/src/__tests__/useSessionLifecycle.test.ts +60 -0
  283. package/src/__tests__/useVirtualHistoryHeights.test.ts +39 -0
  284. package/src/__tests__/viewport.test.ts +58 -0
  285. package/src/__tests__/viewportStore.test.ts +85 -0
  286. package/src/__tests__/virtualHeights.test.ts +96 -0
  287. package/src/__tests__/virtualHistoryClamp.test.ts +19 -0
  288. package/src/__tests__/virtualHistoryOffsetCache.test.ts +282 -0
  289. package/src/__tests__/wheelAccel.test.ts +138 -0
  290. package/src/app/createGatewayEventHandler.ts +833 -0
  291. package/src/app/createSlashHandler.ts +130 -0
  292. package/src/app/delegationStore.ts +77 -0
  293. package/src/app/gatewayContext.tsx +19 -0
  294. package/src/app/gatewayRecovery.ts +35 -0
  295. package/src/app/inputSelectionStore.ts +15 -0
  296. package/src/app/interfaces.ts +394 -0
  297. package/src/app/overlayStore.ts +53 -0
  298. package/src/app/scroll.ts +71 -0
  299. package/src/app/setupHandoff.ts +54 -0
  300. package/src/app/slash/commands/core.ts +648 -0
  301. package/src/app/slash/commands/credits.ts +57 -0
  302. package/src/app/slash/commands/debug.ts +48 -0
  303. package/src/app/slash/commands/ops.ts +717 -0
  304. package/src/app/slash/commands/session.ts +554 -0
  305. package/src/app/slash/commands/setup.ts +20 -0
  306. package/src/app/slash/registry.ts +20 -0
  307. package/src/app/slash/types.ts +21 -0
  308. package/src/app/spawnHistoryStore.ts +159 -0
  309. package/src/app/turnController.ts +866 -0
  310. package/src/app/turnStore.ts +85 -0
  311. package/src/app/uiStore.ts +44 -0
  312. package/src/app/useComposerState.ts +367 -0
  313. package/src/app/useConfigSync.ts +288 -0
  314. package/src/app/useInputHandlers.ts +576 -0
  315. package/src/app/useLongRunToolCharms.ts +69 -0
  316. package/src/app/useMainApp.ts +1039 -0
  317. package/src/app/useSessionLifecycle.ts +366 -0
  318. package/src/app/useSubmission.ts +429 -0
  319. package/src/app.tsx +25 -0
  320. package/src/banner.ts +93 -0
  321. package/src/components/activeSessionSwitcher.tsx +635 -0
  322. package/src/components/agentsOverlay.tsx +1073 -0
  323. package/src/components/appChrome.tsx +554 -0
  324. package/src/components/appLayout.tsx +444 -0
  325. package/src/components/appOverlays.tsx +254 -0
  326. package/src/components/branding.tsx +466 -0
  327. package/src/components/fpsOverlay.tsx +30 -0
  328. package/src/components/helpHint.tsx +73 -0
  329. package/src/components/markdown.tsx +1119 -0
  330. package/src/components/maskedPrompt.tsx +34 -0
  331. package/src/components/messageLine.tsx +237 -0
  332. package/src/components/modelPicker.tsx +527 -0
  333. package/src/components/overlayControls.tsx +50 -0
  334. package/src/components/pluginsHub.tsx +238 -0
  335. package/src/components/prompts.tsx +276 -0
  336. package/src/components/queuedMessages.tsx +64 -0
  337. package/src/components/sessionPicker.tsx +227 -0
  338. package/src/components/skillsHub.tsx +308 -0
  339. package/src/components/streamingAssistant.tsx +110 -0
  340. package/src/components/streamingMarkdown.tsx +174 -0
  341. package/src/components/textInput.tsx +1340 -0
  342. package/src/components/themed.tsx +30 -0
  343. package/src/components/thinking.tsx +1224 -0
  344. package/src/components/todoPanel.tsx +93 -0
  345. package/src/config/env.ts +64 -0
  346. package/src/config/limits.ts +13 -0
  347. package/src/config/timing.ts +6 -0
  348. package/src/content/charms.ts +1 -0
  349. package/src/content/faces.ts +17 -0
  350. package/src/content/fortunes.ts +30 -0
  351. package/src/content/hotkeys.ts +37 -0
  352. package/src/content/placeholders.ts +13 -0
  353. package/src/content/setup.ts +17 -0
  354. package/src/content/verbs.ts +38 -0
  355. package/src/domain/blockLayout.ts +146 -0
  356. package/src/domain/details.ts +76 -0
  357. package/src/domain/messages.ts +91 -0
  358. package/src/domain/paths.ts +16 -0
  359. package/src/domain/providers.ts +11 -0
  360. package/src/domain/roles.ts +9 -0
  361. package/src/domain/slash.ts +10 -0
  362. package/src/domain/usage.ts +3 -0
  363. package/src/domain/viewport.ts +51 -0
  364. package/src/entry.tsx +104 -0
  365. package/src/gatewayClient.ts +730 -0
  366. package/src/gatewayTypes.ts +568 -0
  367. package/src/hooks/useCompletion.ts +112 -0
  368. package/src/hooks/useGitBranch.ts +72 -0
  369. package/src/hooks/useInputHistory.ts +11 -0
  370. package/src/hooks/useQueue.ts +76 -0
  371. package/src/hooks/useVirtualHistory.ts +554 -0
  372. package/src/lib/circularBuffer.ts +48 -0
  373. package/src/lib/clipboard.ts +182 -0
  374. package/src/lib/editor.test.ts +74 -0
  375. package/src/lib/editor.ts +47 -0
  376. package/src/lib/emoji.ts +55 -0
  377. package/src/lib/externalCli.ts +16 -0
  378. package/src/lib/externalLink.ts +435 -0
  379. package/src/lib/forceTruecolor.ts +60 -0
  380. package/src/lib/fpsStore.ts +51 -0
  381. package/src/lib/fuzzy.test.ts +109 -0
  382. package/src/lib/fuzzy.ts +177 -0
  383. package/src/lib/gracefulExit.ts +47 -0
  384. package/src/lib/history.ts +82 -0
  385. package/src/lib/inputMetrics.ts +203 -0
  386. package/src/lib/liveProgress.test.ts +116 -0
  387. package/src/lib/liveProgress.ts +79 -0
  388. package/src/lib/mathUnicode.ts +770 -0
  389. package/src/lib/memory.test.ts +155 -0
  390. package/src/lib/memory.ts +188 -0
  391. package/src/lib/memoryMonitor.ts +109 -0
  392. package/src/lib/messages.test.ts +29 -0
  393. package/src/lib/messages.ts +8 -0
  394. package/src/lib/openExternalUrl.test.ts +217 -0
  395. package/src/lib/openExternalUrl.ts +158 -0
  396. package/src/lib/osc52.ts +73 -0
  397. package/src/lib/parentLog.ts +57 -0
  398. package/src/lib/perfPane.tsx +107 -0
  399. package/src/lib/platform.ts +409 -0
  400. package/src/lib/precisionWheel.ts +48 -0
  401. package/src/lib/prompt.ts +35 -0
  402. package/src/lib/reasoning.ts +55 -0
  403. package/src/lib/rpc.ts +41 -0
  404. package/src/lib/subagentTree.ts +355 -0
  405. package/src/lib/syntax.ts +117 -0
  406. package/src/lib/terminalModes.ts +51 -0
  407. package/src/lib/terminalParity.ts +78 -0
  408. package/src/lib/terminalSetup.ts +444 -0
  409. package/src/lib/termux.ts +29 -0
  410. package/src/lib/text.test.ts +18 -0
  411. package/src/lib/text.ts +339 -0
  412. package/src/lib/todo.test.ts +21 -0
  413. package/src/lib/todo.ts +9 -0
  414. package/src/lib/viewportStore.ts +124 -0
  415. package/src/lib/virtualHeights.ts +145 -0
  416. package/src/lib/wheelAccel.ts +190 -0
  417. package/src/protocol/interpolation.ts +3 -0
  418. package/src/protocol/paste.ts +1 -0
  419. package/src/theme.ts +589 -0
  420. package/src/types/nastech-ink.d.ts +176 -0
  421. package/src/types.ts +212 -0
  422. package/tsconfig.build.json +9 -0
  423. package/tsconfig.json +19 -0
  424. package/vitest.config.ts +7 -0
@@ -0,0 +1,848 @@
1
+ /**
2
+ * Keyboard input parser - converts terminal input to key events
3
+ *
4
+ * Uses the termio tokenizer for escape sequence boundary detection,
5
+ * then interprets sequences as keypresses.
6
+ */
7
+ import { Buffer } from 'buffer'
8
+
9
+ import { PASTE_END, PASTE_START } from './termio/csi.js'
10
+ import { createTokenizer, type Tokenizer } from './termio/tokenize.js'
11
+
12
+ // eslint-disable-next-line no-control-regex
13
+ const META_KEY_CODE_RE = /^(?:\x1b)([a-zA-Z0-9])$/
14
+
15
+ const FN_KEY_RE =
16
+ // eslint-disable-next-line no-control-regex
17
+ /^(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;)?(\d+)?([a-zA-Z]))/
18
+
19
+ // CSI u (kitty keyboard protocol): ESC [ codepoint [; modifier] u
20
+ // Example: ESC[13;2u = Shift+Enter, ESC[27u = Escape (no modifiers)
21
+ // Modifier is optional - when absent, defaults to 1 (no modifiers)
22
+ // eslint-disable-next-line no-control-regex
23
+ const CSI_U_RE = /^\x1b\[(\d+)(?:;(\d+))?u/
24
+
25
+ // xterm modifyOtherKeys: ESC [ 27 ; modifier ; keycode ~
26
+ // Example: ESC[27;2;13~ = Shift+Enter. Emitted by Ghostty/tmux/xterm when
27
+ // modifyOtherKeys=2 is active or via user keybinds, typically over SSH where
28
+ // TERM sniffing misses Ghostty and we never push Kitty keyboard mode.
29
+ // Note param order is reversed vs CSI u (modifier first, keycode second).
30
+ // eslint-disable-next-line no-control-regex
31
+ const MODIFY_OTHER_KEYS_RE = /^\x1b\[27;(\d+);(\d+)~/
32
+
33
+ // -- Terminal response patterns (inbound sequences from the terminal itself) --
34
+ // DECRPM: CSI ? Ps ; Pm $ y — response to DECRQM (request mode)
35
+ // eslint-disable-next-line no-control-regex
36
+ const DECRPM_RE = /^\x1b\[\?(\d+);(\d+)\$y$/
37
+ // DA1: CSI ? Ps ; ... c — primary device attributes response
38
+ // eslint-disable-next-line no-control-regex
39
+ const DA1_RE = /^\x1b\[\?([\d;]*)c$/
40
+ // DA2: CSI > Ps ; ... c — secondary device attributes response
41
+ // eslint-disable-next-line no-control-regex
42
+ const DA2_RE = /^\x1b\[>([\d;]*)c$/
43
+ // Kitty keyboard flags: CSI ? flags u — response to CSI ? u query
44
+ // (private ? marker distinguishes from CSI u key events)
45
+ // eslint-disable-next-line no-control-regex
46
+ const KITTY_FLAGS_RE = /^\x1b\[\?(\d+)u$/
47
+ // DECXCPR cursor position: CSI ? row ; col R
48
+ // The ? marker disambiguates from modified F3 keys (Shift+F3 = CSI 1;2 R,
49
+ // Ctrl+F3 = CSI 1;5 R, etc.) — plain CSI row;col R is genuinely ambiguous.
50
+ // eslint-disable-next-line no-control-regex
51
+ const CURSOR_POSITION_RE = /^\x1b\[\?(\d+);(\d+)R$/
52
+ // OSC response: OSC code ; data (BEL|ST)
53
+ // eslint-disable-next-line no-control-regex
54
+ const OSC_RESPONSE_RE = /^\x1b\](\d+);(.*?)(?:\x07|\x1b\\)$/s
55
+ // XTVERSION: DCS > | name ST — terminal name/version string (answer to CSI > 0 q).
56
+ // xterm.js replies "xterm.js(X.Y.Z)"; Ghostty, kitty, iTerm2, etc. reply with
57
+ // their own name. Unlike TERM_PROGRAM, this survives SSH since the query/reply
58
+ // goes through the pty, not the environment.
59
+ // eslint-disable-next-line no-control-regex
60
+ const XTVERSION_RE = /^\x1bP>\|(.*?)(?:\x07|\x1b\\)$/s
61
+ // SGR mouse event: CSI < button ; col ; row M (press) or m (release)
62
+ // Button codes: 64=wheel-up, 65=wheel-down (0x40 | wheel-bit).
63
+ // Button 32=left-drag (0x20 | motion-bit). Plain 0/1/2 = left/mid/right click.
64
+ // eslint-disable-next-line no-control-regex
65
+ const SGR_MOUSE_RE = /^\x1b\[<(\d+);(\d+);(\d+)([Mm])$/
66
+
67
+ function createPasteKey(content: string): ParsedKey {
68
+ return {
69
+ kind: 'key',
70
+ name: '',
71
+ fn: false,
72
+ ctrl: false,
73
+ meta: false,
74
+ shift: false,
75
+ option: false,
76
+ super: false,
77
+ sequence: content,
78
+ raw: content,
79
+ isPasted: true
80
+ }
81
+ }
82
+
83
+ /** DECRPM status values (response to DECRQM) */
84
+ export const DECRPM_STATUS = {
85
+ NOT_RECOGNIZED: 0,
86
+ SET: 1,
87
+ RESET: 2,
88
+ PERMANENTLY_SET: 3,
89
+ PERMANENTLY_RESET: 4
90
+ } as const
91
+
92
+ /**
93
+ * A response sequence received from the terminal (not a keypress).
94
+ * Emitted in answer to queries like DECRQM, DA1, OSC 11, etc.
95
+ */
96
+ export type TerminalResponse =
97
+ /** DECRPM: answer to DECRQM (request DEC private mode status) */
98
+ | { type: 'decrpm'; mode: number; status: number }
99
+ /** DA1: primary device attributes (used as a universal sentinel) */
100
+ | { type: 'da1'; params: number[] }
101
+ /** DA2: secondary device attributes (terminal version info) */
102
+ | { type: 'da2'; params: number[] }
103
+ /** Kitty keyboard protocol: current flags (answer to CSI ? u) */
104
+ | { type: 'kittyKeyboard'; flags: number }
105
+ /** DSR: cursor position report (answer to CSI 6 n) */
106
+ | { type: 'cursorPosition'; row: number; col: number }
107
+ /** OSC response: generic operating-system-command reply (e.g. OSC 11 bg color) */
108
+ | { type: 'osc'; code: number; data: string }
109
+ /** XTVERSION: terminal name/version string (answer to CSI > 0 q).
110
+ * Example values: "xterm.js(5.5.0)", "ghostty 1.2.0", "iTerm2 3.6". */
111
+ | { type: 'xtversion'; name: string }
112
+
113
+ /**
114
+ * Try to recognize a sequence token as a terminal response.
115
+ * Returns null if the sequence is not a known response pattern
116
+ * (i.e. it should be treated as a keypress).
117
+ *
118
+ * These patterns are syntactically distinguishable from keyboard input —
119
+ * no physical key produces CSI ? ... c or CSI ? ... $ y, so they can be
120
+ * safely parsed out of the input stream at any time.
121
+ */
122
+ function parseTerminalResponse(s: string): TerminalResponse | null {
123
+ // CSI-prefixed responses
124
+ if (s.startsWith('\x1b[')) {
125
+ let m: RegExpExecArray | null
126
+
127
+ if ((m = DECRPM_RE.exec(s))) {
128
+ return {
129
+ type: 'decrpm',
130
+ mode: parseInt(m[1]!, 10),
131
+ status: parseInt(m[2]!, 10)
132
+ }
133
+ }
134
+
135
+ if ((m = DA1_RE.exec(s))) {
136
+ return { type: 'da1', params: splitNumericParams(m[1]!) }
137
+ }
138
+
139
+ if ((m = DA2_RE.exec(s))) {
140
+ return { type: 'da2', params: splitNumericParams(m[1]!) }
141
+ }
142
+
143
+ if ((m = KITTY_FLAGS_RE.exec(s))) {
144
+ return { type: 'kittyKeyboard', flags: parseInt(m[1]!, 10) }
145
+ }
146
+
147
+ if ((m = CURSOR_POSITION_RE.exec(s))) {
148
+ return {
149
+ type: 'cursorPosition',
150
+ row: parseInt(m[1]!, 10),
151
+ col: parseInt(m[2]!, 10)
152
+ }
153
+ }
154
+
155
+ return null
156
+ }
157
+
158
+ // OSC responses (e.g. OSC 11 ; rgb:... for bg color query)
159
+ if (s.startsWith('\x1b]')) {
160
+ const m = OSC_RESPONSE_RE.exec(s)
161
+
162
+ if (m) {
163
+ return { type: 'osc', code: parseInt(m[1]!, 10), data: m[2]! }
164
+ }
165
+ }
166
+
167
+ // DCS responses (e.g. XTVERSION: DCS > | name ST)
168
+ if (s.startsWith('\x1bP')) {
169
+ const m = XTVERSION_RE.exec(s)
170
+
171
+ if (m) {
172
+ return { type: 'xtversion', name: m[1]! }
173
+ }
174
+ }
175
+
176
+ return null
177
+ }
178
+
179
+ function splitNumericParams(params: string): number[] {
180
+ if (!params) {
181
+ return []
182
+ }
183
+
184
+ return params.split(';').map(p => parseInt(p, 10))
185
+ }
186
+
187
+ export type KeyParseState = {
188
+ mode: 'NORMAL' | 'IN_PASTE'
189
+ incomplete: string
190
+ pasteBuffer: string
191
+ // Internal tokenizer instance
192
+ _tokenizer?: Tokenizer
193
+ }
194
+
195
+ export const INITIAL_STATE: KeyParseState = {
196
+ mode: 'NORMAL',
197
+ incomplete: '',
198
+ pasteBuffer: ''
199
+ }
200
+
201
+ function inputToString(input: Buffer | string): string {
202
+ if (Buffer.isBuffer(input)) {
203
+ if (input[0]! > 127 && input[1] === undefined) {
204
+ ;(input[0] as unknown as number) -= 128
205
+
206
+ return '\x1b' + String(input)
207
+ } else {
208
+ return String(input)
209
+ }
210
+ } else if (input !== undefined && typeof input !== 'string') {
211
+ return String(input)
212
+ } else if (!input) {
213
+ return ''
214
+ } else {
215
+ return input
216
+ }
217
+ }
218
+
219
+ export function parseMultipleKeypresses(
220
+ prevState: KeyParseState,
221
+ input: Buffer | string | null = ''
222
+ ): [ParsedInput[], KeyParseState] {
223
+ const isFlush = input === null
224
+ const inputString = isFlush ? '' : inputToString(input)
225
+
226
+ // Get or create tokenizer
227
+ const tokenizer = prevState._tokenizer ?? createTokenizer({ x10Mouse: true })
228
+
229
+ // Tokenize the input
230
+ const tokens = isFlush ? tokenizer.flush() : tokenizer.feed(inputString)
231
+
232
+ // Convert tokens to parsed keys, handling paste mode
233
+ const keys: ParsedInput[] = []
234
+ let inPaste = prevState.mode === 'IN_PASTE'
235
+ let pasteBuffer = prevState.pasteBuffer
236
+
237
+ for (const token of tokens) {
238
+ if (token.type === 'sequence') {
239
+ if (token.value === PASTE_START) {
240
+ inPaste = true
241
+ pasteBuffer = ''
242
+ } else if (token.value === PASTE_END) {
243
+ // Always emit a paste key, even for empty pastes. This allows
244
+ // downstream handlers to detect empty pastes (e.g., for clipboard
245
+ // image handling on macOS). The paste content may be empty string.
246
+ keys.push(createPasteKey(pasteBuffer))
247
+ inPaste = false
248
+ pasteBuffer = ''
249
+ } else if (inPaste) {
250
+ // Sequences inside paste are treated as literal text
251
+ pasteBuffer += token.value
252
+ } else {
253
+ const response = parseTerminalResponse(token.value)
254
+
255
+ if (response) {
256
+ keys.push({ kind: 'response', sequence: token.value, response })
257
+ } else {
258
+ const mouse = parseMouseEvent(token.value)
259
+
260
+ if (mouse) {
261
+ keys.push(mouse)
262
+ } else {
263
+ keys.push(parseKeypress(token.value))
264
+ }
265
+ }
266
+ }
267
+ } else if (token.type === 'text') {
268
+ if (inPaste) {
269
+ pasteBuffer += token.value
270
+ } else if (/^\[M[\x60-\x7f][\x20-\uffff]{2}$/.test(token.value)) {
271
+ // Orphaned X10 wheel tail (legacy 1000/1002 terminals, fullscreen
272
+ // only). If the buffered ESC was flushed as a lone Escape and the X10
273
+ // payload (`[M` + 3 bytes) arrived as the next text token, re-synthesize
274
+ // with ESC so the scroll event still fires instead of leaking. SGR mouse
275
+ // reports no longer reach this branch — the tokenizer keeps an
276
+ // incomplete CSI buffered across a flush and reassembles it (see
277
+ // termio/tokenize.ts), so the old fragment/burst recovery is gone.
278
+ const resynthesized = '\x1b' + token.value
279
+ keys.push(parseKeypress(resynthesized))
280
+ } else {
281
+ keys.push(parseKeypress(token.value))
282
+ }
283
+ }
284
+ }
285
+
286
+ // If a terminal drops the paste-end marker, the App watchdog flushes the
287
+ // partial paste and returns to normal input instead of swallowing all future
288
+ // keystrokes as paste content.
289
+ if (isFlush && inPaste) {
290
+ if (pasteBuffer) {
291
+ keys.push(createPasteKey(pasteBuffer))
292
+ }
293
+
294
+ inPaste = false
295
+ pasteBuffer = ''
296
+ }
297
+
298
+ // Build new state
299
+ const newState: KeyParseState = {
300
+ mode: inPaste ? 'IN_PASTE' : 'NORMAL',
301
+ incomplete: tokenizer.buffer(),
302
+ pasteBuffer,
303
+ _tokenizer: tokenizer
304
+ }
305
+
306
+ return [keys, newState]
307
+ }
308
+
309
+ const keyName: Record<string, string> = {
310
+ /* xterm/gnome ESC O letter */
311
+ OP: 'f1',
312
+ OQ: 'f2',
313
+ OR: 'f3',
314
+ OS: 'f4',
315
+ /* Application keypad mode (numpad digits 0-9) */
316
+ Op: '0',
317
+ Oq: '1',
318
+ Or: '2',
319
+ Os: '3',
320
+ Ot: '4',
321
+ Ou: '5',
322
+ Ov: '6',
323
+ Ow: '7',
324
+ Ox: '8',
325
+ Oy: '9',
326
+ /* Application keypad mode (numpad operators) */
327
+ Oj: '*',
328
+ Ok: '+',
329
+ Ol: ',',
330
+ Om: '-',
331
+ On: '.',
332
+ Oo: '/',
333
+ OM: 'return',
334
+ /* xterm/rxvt ESC [ number ~ */
335
+ '[11~': 'f1',
336
+ '[12~': 'f2',
337
+ '[13~': 'f3',
338
+ '[14~': 'f4',
339
+ /* from Cygwin and used in libuv */
340
+ '[[A': 'f1',
341
+ '[[B': 'f2',
342
+ '[[C': 'f3',
343
+ '[[D': 'f4',
344
+ '[[E': 'f5',
345
+ /* common */
346
+ '[15~': 'f5',
347
+ '[17~': 'f6',
348
+ '[18~': 'f7',
349
+ '[19~': 'f8',
350
+ '[20~': 'f9',
351
+ '[21~': 'f10',
352
+ '[23~': 'f11',
353
+ '[24~': 'f12',
354
+ /* xterm ESC [ letter */
355
+ '[A': 'up',
356
+ '[B': 'down',
357
+ '[C': 'right',
358
+ '[D': 'left',
359
+ '[E': 'clear',
360
+ '[F': 'end',
361
+ '[H': 'home',
362
+ /* xterm/gnome ESC O letter */
363
+ OA: 'up',
364
+ OB: 'down',
365
+ OC: 'right',
366
+ OD: 'left',
367
+ OE: 'clear',
368
+ OF: 'end',
369
+ OH: 'home',
370
+ /* xterm/rxvt ESC [ number ~ */
371
+ '[1~': 'home',
372
+ '[2~': 'insert',
373
+ '[3~': 'delete',
374
+ '[4~': 'end',
375
+ '[5~': 'pageup',
376
+ '[6~': 'pagedown',
377
+ /* putty */
378
+ '[[5~': 'pageup',
379
+ '[[6~': 'pagedown',
380
+ /* rxvt */
381
+ '[7~': 'home',
382
+ '[8~': 'end',
383
+ /* rxvt keys with modifiers */
384
+ '[a': 'up',
385
+ '[b': 'down',
386
+ '[c': 'right',
387
+ '[d': 'left',
388
+ '[e': 'clear',
389
+
390
+ '[2$': 'insert',
391
+ '[3$': 'delete',
392
+ '[5$': 'pageup',
393
+ '[6$': 'pagedown',
394
+ '[7$': 'home',
395
+ '[8$': 'end',
396
+
397
+ Oa: 'up',
398
+ Ob: 'down',
399
+ Oc: 'right',
400
+ Od: 'left',
401
+ Oe: 'clear',
402
+
403
+ '[2^': 'insert',
404
+ '[3^': 'delete',
405
+ '[5^': 'pageup',
406
+ '[6^': 'pagedown',
407
+ '[7^': 'home',
408
+ '[8^': 'end',
409
+ /* misc. */
410
+ '[Z': 'tab'
411
+ }
412
+
413
+ export const nonAlphanumericKeys = [
414
+ // Filter out single-character values (digits, operators from numpad) since
415
+ // those are printable characters that should produce input
416
+ ...Object.values(keyName).filter(v => v.length > 1),
417
+ // escape and backspace are assigned directly in parseKeypress (not via the
418
+ // keyName map), so the spread above misses them. Without these, ctrl+escape
419
+ // via Kitty/modifyOtherKeys leaks the literal word "escape" as input text
420
+ // (input-event.ts:58 assigns keypress.name when ctrl is set).
421
+ 'escape',
422
+ 'backspace',
423
+ 'wheelup',
424
+ 'wheeldown',
425
+ 'mouse'
426
+ ]
427
+
428
+ const isShiftKey = (code: string): boolean => {
429
+ return ['[a', '[b', '[c', '[d', '[e', '[2$', '[3$', '[5$', '[6$', '[7$', '[8$', '[Z'].includes(code)
430
+ }
431
+
432
+ const isCtrlKey = (code: string): boolean => {
433
+ return ['Oa', 'Ob', 'Oc', 'Od', 'Oe', '[2^', '[3^', '[5^', '[6^', '[7^', '[8^'].includes(code)
434
+ }
435
+
436
+ /**
437
+ * Decode XTerm-style modifier value to individual flags.
438
+ * Modifier encoding: 1 + (shift ? 1 : 0) + (alt ? 2 : 0) + (ctrl ? 4 : 0) + (super ? 8 : 0)
439
+ *
440
+ * Note: `meta` here means Alt/Option (bit 2). `super` is a distinct
441
+ * modifier (bit 8, i.e. Cmd on macOS / Win key). Most legacy terminal
442
+ * sequences can't express super — it only arrives via kitty keyboard
443
+ * protocol (CSI u) or xterm modifyOtherKeys.
444
+ */
445
+ function decodeModifier(modifier: number): {
446
+ shift: boolean
447
+ meta: boolean
448
+ ctrl: boolean
449
+ super: boolean
450
+ } {
451
+ const m = modifier - 1
452
+
453
+ return {
454
+ shift: !!(m & 1),
455
+ meta: !!(m & 2),
456
+ ctrl: !!(m & 4),
457
+ super: !!(m & 8)
458
+ }
459
+ }
460
+
461
+ /**
462
+ * Map keycode to key name for modifyOtherKeys/CSI u sequences.
463
+ * Handles both ASCII keycodes and Kitty keyboard protocol functional keys.
464
+ *
465
+ * Numpad codepoints are from Unicode Private Use Area, defined at:
466
+ * https://sw.kovidgoyal.net/kitty/keyboard-protocol/#functional-key-definitions
467
+ */
468
+ function keycodeToName(keycode: number): string | undefined {
469
+ switch (keycode) {
470
+ case 9:
471
+ return 'tab'
472
+
473
+ case 13:
474
+ return 'return'
475
+
476
+ case 27:
477
+ return 'escape'
478
+
479
+ case 32:
480
+ return 'space'
481
+
482
+ case 127:
483
+ return 'backspace'
484
+
485
+ // Kitty keyboard protocol numpad keys (KP_0 through KP_9)
486
+ case 57399:
487
+ return '0'
488
+
489
+ case 57400:
490
+ return '1'
491
+
492
+ case 57401:
493
+ return '2'
494
+
495
+ case 57402:
496
+ return '3'
497
+
498
+ case 57403:
499
+ return '4'
500
+
501
+ case 57404:
502
+ return '5'
503
+
504
+ case 57405:
505
+ return '6'
506
+
507
+ case 57406:
508
+ return '7'
509
+
510
+ case 57407:
511
+ return '8'
512
+
513
+ case 57408:
514
+ return '9'
515
+
516
+ case 57409: // KP_DECIMAL
517
+ return '.'
518
+
519
+ case 57410: // KP_DIVIDE
520
+ return '/'
521
+
522
+ case 57411: // KP_MULTIPLY
523
+ return '*'
524
+
525
+ case 57412: // KP_SUBTRACT
526
+ return '-'
527
+
528
+ case 57413: // KP_ADD
529
+ return '+'
530
+
531
+ case 57414: // KP_ENTER
532
+ return 'return'
533
+
534
+ case 57415: // KP_EQUAL
535
+ return '='
536
+
537
+ default:
538
+ // Printable ASCII characters
539
+ if (keycode >= 32 && keycode <= 126) {
540
+ return String.fromCharCode(keycode).toLowerCase()
541
+ }
542
+
543
+ return undefined
544
+ }
545
+ }
546
+
547
+ export type ParsedKey = {
548
+ kind: 'key'
549
+ fn: boolean
550
+ name: string | undefined
551
+ ctrl: boolean
552
+ meta: boolean
553
+ shift: boolean
554
+ option: boolean
555
+ super: boolean
556
+ sequence: string | undefined
557
+ raw: string | undefined
558
+ code?: string
559
+ isPasted: boolean
560
+ }
561
+
562
+ /** A terminal response sequence (DECRPM, DA1, OSC reply, etc.) parsed
563
+ * out of the input stream. Not user input — consumers should dispatch
564
+ * to a response handler. */
565
+ export type ParsedResponse = {
566
+ kind: 'response'
567
+ /** Raw escape sequence bytes, for debugging/logging */
568
+ sequence: string
569
+ response: TerminalResponse
570
+ }
571
+
572
+ /** SGR mouse event with coordinates. Emitted for clicks, drags, and
573
+ * releases (wheel events remain ParsedKey). col/row are 1-indexed
574
+ * from the terminal sequence (CSI < btn;col;row M/m). */
575
+ export type ParsedMouse = {
576
+ kind: 'mouse'
577
+ /** Raw SGR button code. Low 2 bits = button (0=left,1=mid,2=right),
578
+ * bit 5 (0x20) = drag/motion, bit 6 (0x40) = wheel. */
579
+ button: number
580
+ /** 'press' for M terminator, 'release' for m terminator */
581
+ action: 'press' | 'release'
582
+ /** 1-indexed column (from terminal) */
583
+ col: number
584
+ /** 1-indexed row (from terminal) */
585
+ row: number
586
+ sequence: string
587
+ }
588
+
589
+ /** Everything that can come out of the input parser: a user keypress/paste,
590
+ * a mouse click/drag event, or a terminal response to a query we sent. */
591
+ export type ParsedInput = ParsedKey | ParsedMouse | ParsedResponse
592
+
593
+ /**
594
+ * Parse an SGR mouse event sequence into a ParsedMouse, or null if not a
595
+ * mouse event or if it's a wheel event (wheel stays as ParsedKey for the
596
+ * keybinding system). Button bit 0x40 = wheel, bit 0x20 = drag/motion.
597
+ */
598
+ function parseMouseEvent(s: string): ParsedMouse | null {
599
+ const match = SGR_MOUSE_RE.exec(s)
600
+
601
+ if (!match) {
602
+ return null
603
+ }
604
+
605
+ const button = parseInt(match[1]!, 10)
606
+
607
+ // Wheel events (bit 6 set, low bits 0/1 for up/down) stay as ParsedKey
608
+ // so the keybinding system can route them to scroll handlers.
609
+ if ((button & 0x40) !== 0) {
610
+ return null
611
+ }
612
+
613
+ return {
614
+ kind: 'mouse',
615
+ button,
616
+ action: match[4] === 'M' ? 'press' : 'release',
617
+ col: parseInt(match[2]!, 10),
618
+ row: parseInt(match[3]!, 10),
619
+ sequence: s
620
+ }
621
+ }
622
+
623
+ function parseKeypress(s: string = ''): ParsedKey {
624
+ let parts
625
+
626
+ const key: ParsedKey = {
627
+ kind: 'key',
628
+ name: '',
629
+ fn: false,
630
+ ctrl: false,
631
+ meta: false,
632
+ shift: false,
633
+ option: false,
634
+ super: false,
635
+ sequence: s,
636
+ raw: s,
637
+ isPasted: false
638
+ }
639
+
640
+ key.sequence = key.sequence || s || key.name
641
+
642
+ // Handle CSI u (kitty keyboard protocol): ESC [ codepoint [; modifier] u
643
+ // Example: ESC[13;2u = Shift+Enter, ESC[27u = Escape (no modifiers)
644
+ let match: RegExpExecArray | null
645
+
646
+ if ((match = CSI_U_RE.exec(s))) {
647
+ const codepoint = parseInt(match[1]!, 10)
648
+ // Modifier defaults to 1 (no modifiers) when not present
649
+ const modifier = match[2] ? parseInt(match[2], 10) : 1
650
+ const mods = decodeModifier(modifier)
651
+ const name = keycodeToName(codepoint)
652
+
653
+ return {
654
+ kind: 'key',
655
+ name,
656
+ fn: false,
657
+ ctrl: mods.ctrl,
658
+ meta: mods.meta,
659
+ shift: mods.shift,
660
+ option: false,
661
+ super: mods.super,
662
+ sequence: s,
663
+ raw: s,
664
+ isPasted: false
665
+ }
666
+ }
667
+
668
+ // Handle xterm modifyOtherKeys: ESC [ 27 ; modifier ; keycode ~
669
+ // Must run before FN_KEY_RE — FN_KEY_RE only allows 2 params before ~ and
670
+ // would leave the tail as garbage if it partially matched.
671
+ if ((match = MODIFY_OTHER_KEYS_RE.exec(s))) {
672
+ const mods = decodeModifier(parseInt(match[1]!, 10))
673
+ const name = keycodeToName(parseInt(match[2]!, 10))
674
+
675
+ return {
676
+ kind: 'key',
677
+ name,
678
+ fn: false,
679
+ ctrl: mods.ctrl,
680
+ meta: mods.meta,
681
+ shift: mods.shift,
682
+ option: false,
683
+ super: mods.super,
684
+ sequence: s,
685
+ raw: s,
686
+ isPasted: false
687
+ }
688
+ }
689
+
690
+ // SGR mouse wheel events. Click/drag/release events are handled
691
+ // earlier by parseMouseEvent and emitted as ParsedMouse, so they
692
+ // never reach here. Mask with 0x43 (bits 6+1+0) to check wheel-flag
693
+ // + direction while ignoring modifier bits (Shift=0x04, Meta=0x08,
694
+ // Ctrl=0x10) — modified wheel events (e.g. Ctrl+scroll, button=80)
695
+ // should still be recognized as wheelup/wheeldown. Preserve those
696
+ // modifier bits for callers that bind modified wheel gestures.
697
+ if ((match = SGR_MOUSE_RE.exec(s))) {
698
+ const button = parseInt(match[1]!, 10)
699
+
700
+ if ((button & 0x43) === 0x40) {
701
+ return createWheelKey(s, 'wheelup', button)
702
+ }
703
+
704
+ if ((button & 0x43) === 0x41) {
705
+ return createWheelKey(s, 'wheeldown', button)
706
+ }
707
+
708
+ // Shouldn't reach here (parseMouseEvent catches non-wheel) but be safe
709
+ return createNavKey(s, 'mouse', false)
710
+ }
711
+
712
+ // X10 mouse: CSI M + 3 raw bytes (Cb+32, Cx+32, Cy+32). Terminals that
713
+ // ignore DECSET 1006 (SGR) but honor 1000/1002 emit this legacy encoding.
714
+ // Button bits match SGR: 0x40 = wheel, low bit = direction. Non-wheel
715
+ // X10 events (clicks/drags) are swallowed here — we only enable mouse
716
+ // tracking in alt-screen and only need wheel for ScrollBox.
717
+ if (s.length === 6 && s.startsWith('\x1b[M')) {
718
+ const button = s.charCodeAt(3) - 32
719
+
720
+ if ((button & 0x43) === 0x40) {
721
+ return createWheelKey(s, 'wheelup', button)
722
+ }
723
+
724
+ if ((button & 0x43) === 0x41) {
725
+ return createWheelKey(s, 'wheeldown', button)
726
+ }
727
+
728
+ return createNavKey(s, 'mouse', false)
729
+ }
730
+
731
+ if (s === '\r' || s === '\n') {
732
+ key.raw = undefined
733
+ key.name = 'return'
734
+ } else if (s === '\t') {
735
+ key.name = 'tab'
736
+ } else if (s === '\b' || s === '\x1b\b') {
737
+ key.name = 'backspace'
738
+ key.meta = s.charAt(0) === '\x1b'
739
+ } else if (s === '\x7f' || s === '\x1b\x7f') {
740
+ key.name = 'backspace'
741
+ key.meta = s.charAt(0) === '\x1b'
742
+ } else if (s === '\x1b' || s === '\x1b\x1b') {
743
+ key.name = 'escape'
744
+ key.meta = s.length === 2
745
+ } else if (s === ' ' || s === '\x1b ') {
746
+ key.name = 'space'
747
+ key.meta = s.length === 2
748
+ } else if (s === '\x1f') {
749
+ key.name = '_'
750
+ key.ctrl = true
751
+ } else if (s <= '\x1a' && s.length === 1) {
752
+ key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1)
753
+ key.ctrl = true
754
+ } else if (s.length === 1 && s >= '0' && s <= '9') {
755
+ key.name = 'number'
756
+ } else if (s.length === 1 && s >= 'a' && s <= 'z') {
757
+ key.name = s
758
+ } else if (s.length === 1 && s >= 'A' && s <= 'Z') {
759
+ key.name = s.toLowerCase()
760
+ key.shift = true
761
+ } else if ((parts = META_KEY_CODE_RE.exec(s))) {
762
+ key.meta = true
763
+ key.shift = /^[A-Z]$/.test(parts[1]!)
764
+ } else if ((parts = FN_KEY_RE.exec(s))) {
765
+ const segs = [...s]
766
+
767
+ if (segs[0] === '\u001b' && segs[1] === '\u001b') {
768
+ key.option = true
769
+ }
770
+
771
+ const code = [parts[1], parts[2], parts[4], parts[6]].filter(Boolean).join('')
772
+
773
+ const modifier = ((parts[3] || parts[5] || 1) as number) - 1
774
+
775
+ key.ctrl = !!(modifier & 4)
776
+ key.meta = !!(modifier & 2)
777
+ key.super = !!(modifier & 8)
778
+ key.shift = !!(modifier & 1)
779
+ key.code = code
780
+
781
+ key.name = keyName[code]
782
+ key.shift = isShiftKey(code) || key.shift
783
+ key.ctrl = isCtrlKey(code) || key.ctrl
784
+ }
785
+
786
+ // iTerm in natural text editing mode
787
+ if (key.raw === '\x1Bb') {
788
+ key.meta = true
789
+ key.name = 'left'
790
+ } else if (key.raw === '\x1Bf') {
791
+ key.meta = true
792
+ key.name = 'right'
793
+ }
794
+
795
+ switch (s) {
796
+ case '\u001b[1~':
797
+ return createNavKey(s, 'home', false)
798
+
799
+ case '\u001b[4~':
800
+ return createNavKey(s, 'end', false)
801
+
802
+ case '\u001b[5~':
803
+ return createNavKey(s, 'pageup', false)
804
+
805
+ case '\u001b[6~':
806
+ return createNavKey(s, 'pagedown', false)
807
+
808
+ case '\u001b[1;5D':
809
+ return createNavKey(s, 'left', true)
810
+
811
+ case '\u001b[1;5C':
812
+ return createNavKey(s, 'right', true)
813
+ }
814
+
815
+ return key
816
+ }
817
+
818
+ function createNavKey(s: string, name: string, ctrl: boolean): ParsedKey {
819
+ return {
820
+ kind: 'key',
821
+ name,
822
+ ctrl,
823
+ meta: false,
824
+ shift: false,
825
+ option: false,
826
+ super: false,
827
+ fn: false,
828
+ sequence: s,
829
+ raw: s,
830
+ isPasted: false
831
+ }
832
+ }
833
+
834
+ function createWheelKey(s: string, name: 'wheelup' | 'wheeldown', button: number): ParsedKey {
835
+ return {
836
+ kind: 'key',
837
+ name,
838
+ ctrl: !!(button & 0x10),
839
+ meta: !!(button & 0x08),
840
+ shift: !!(button & 0x04),
841
+ option: false,
842
+ super: false,
843
+ fn: false,
844
+ sequence: s,
845
+ raw: s,
846
+ isPasted: false
847
+ }
848
+ }