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,752 @@
1
+ import { type AnsiCode, ansiCodesToString, diffAnsiCodes } from '@alcalzone/ansi-tokenize'
2
+
3
+ import { logForDebugging } from '../utils/debug.js'
4
+
5
+ import type { Diff, FlickerReason, Frame } from './frame.js'
6
+ import type { Point } from './layout/geometry.js'
7
+ import {
8
+ type Cell,
9
+ cellAt,
10
+ CellWidth,
11
+ charInCellAt,
12
+ diffEach,
13
+ type Hyperlink,
14
+ isEmptyCellAt,
15
+ type Screen,
16
+ shiftRows,
17
+ type StylePool,
18
+ visibleCellAtIndex
19
+ } from './screen.js'
20
+ import {
21
+ scrollDown as csiScrollDown,
22
+ scrollUp as csiScrollUp,
23
+ CURSOR_HOME,
24
+ RESET_SCROLL_REGION,
25
+ setScrollRegion
26
+ } from './termio/csi.js'
27
+ import { LINK_END, link as oscLink } from './termio/osc.js'
28
+
29
+ type State = {
30
+ previousOutput: string
31
+ }
32
+
33
+ type Options = {
34
+ isTTY: boolean
35
+ stylePool: StylePool
36
+ }
37
+
38
+ const CARRIAGE_RETURN = { type: 'carriageReturn' } as const
39
+ const NEWLINE = { type: 'stdout', content: '\n' } as const
40
+
41
+ export class LogUpdate {
42
+ private state: State
43
+
44
+ constructor(private readonly options: Options) {
45
+ this.state = {
46
+ previousOutput: ''
47
+ }
48
+ }
49
+
50
+ renderPreviousOutput_DEPRECATED(prevFrame: Frame): Diff {
51
+ if (!this.options.isTTY) {
52
+ // Non-TTY output is no longer supported (string output was removed)
53
+ return [NEWLINE]
54
+ }
55
+
56
+ return this.getRenderOpsForDone(prevFrame)
57
+ }
58
+
59
+ // Called when process resumes from suspension (SIGCONT) to prevent clobbering terminal content
60
+ reset(): void {
61
+ this.state.previousOutput = ''
62
+ }
63
+
64
+ private renderFullFrame(frame: Frame): Diff {
65
+ const { screen } = frame
66
+ const lines: string[] = []
67
+ let currentStyles: AnsiCode[] = []
68
+ let currentHyperlink: Hyperlink = undefined
69
+
70
+ for (let y = 0; y < screen.height; y++) {
71
+ let line = ''
72
+
73
+ for (let x = 0; x < screen.width; x++) {
74
+ const cell = cellAt(screen, x, y)
75
+
76
+ if (cell && cell.width !== CellWidth.SpacerTail) {
77
+ // Handle hyperlink transitions
78
+ if (cell.hyperlink !== currentHyperlink) {
79
+ if (currentHyperlink !== undefined) {
80
+ line += LINK_END
81
+ }
82
+
83
+ if (cell.hyperlink !== undefined) {
84
+ line += oscLink(cell.hyperlink)
85
+ }
86
+
87
+ currentHyperlink = cell.hyperlink
88
+ }
89
+
90
+ const cellStyles = this.options.stylePool.get(cell.styleId)
91
+ const styleDiff = diffAnsiCodes(currentStyles, cellStyles)
92
+
93
+ if (styleDiff.length > 0) {
94
+ line += ansiCodesToString(styleDiff)
95
+ currentStyles = cellStyles
96
+ }
97
+
98
+ line += cell.char
99
+ }
100
+ }
101
+
102
+ // Close any open hyperlink before resetting styles
103
+ if (currentHyperlink !== undefined) {
104
+ line += LINK_END
105
+ currentHyperlink = undefined
106
+ }
107
+
108
+ // Reset styles at end of line so trimEnd doesn't leave dangling codes
109
+ const resetCodes = diffAnsiCodes(currentStyles, [])
110
+
111
+ if (resetCodes.length > 0) {
112
+ line += ansiCodesToString(resetCodes)
113
+ currentStyles = []
114
+ }
115
+
116
+ lines.push(line.trimEnd())
117
+ }
118
+
119
+ if (lines.length === 0) {
120
+ return []
121
+ }
122
+
123
+ return [{ type: 'stdout', content: lines.join('\n') }]
124
+ }
125
+
126
+ private getRenderOpsForDone(prev: Frame): Diff {
127
+ this.state.previousOutput = ''
128
+
129
+ if (!prev.cursor.visible) {
130
+ return [{ type: 'cursorShow' }]
131
+ }
132
+
133
+ return []
134
+ }
135
+
136
+ render(prev: Frame, next: Frame, altScreen = false, decstbmSafe = true): Diff {
137
+ if (!this.options.isTTY) {
138
+ return this.renderFullFrame(next)
139
+ }
140
+
141
+ const startTime = performance.now()
142
+ const stylePool = this.options.stylePool
143
+
144
+ // Terminal hosts can reflow/preserve old cells on any resize, including
145
+ // height-only growth. A partial diff can then leave stale transcript rows
146
+ // or cut off bordered content even when our virtual scrollTop is correct.
147
+ // Resizing is rare enough that a full repaint is the safer tradeoff.
148
+ if (
149
+ next.viewport.height !== prev.viewport.height ||
150
+ (prev.viewport.width !== 0 && next.viewport.width !== prev.viewport.width)
151
+ ) {
152
+ return fullResetSequence_CAUSES_FLICKER(next, 'resize', stylePool)
153
+ }
154
+
155
+ // DECSTBM scroll optimization: when a ScrollBox's scrollTop changed,
156
+ // shift content with a hardware scroll (CSI top;bot r + CSI n S/T)
157
+ // instead of rewriting the whole scroll region. The shiftRows on
158
+ // prev.screen simulates the shift so the diff loop below naturally
159
+ // finds only the rows that scrolled IN as diffs. prev.screen is
160
+ // about to become backFrame (reused next render) so mutation is safe.
161
+ // CURSOR_HOME after RESET_SCROLL_REGION is defensive — DECSTBM reset
162
+ // homes cursor per spec but terminal implementations vary.
163
+ //
164
+ // decstbmSafe: caller passes false when the DECSTBM→diff sequence
165
+ // can't be made atomic (no DEC 2026 / BSU/ESU). Without atomicity the
166
+ // outer terminal renders the intermediate state — region scrolled,
167
+ // edge rows not yet painted — a visible vertical jump on every frame
168
+ // where scrollTop moves. Falling through to the diff loop writes all
169
+ // shifted rows: more bytes, no intermediate state. next.screen from
170
+ // render-node-to-output's blit+shift is correct either way.
171
+ let scrollPatch: Diff = []
172
+
173
+ if (altScreen && next.scrollHint && decstbmSafe) {
174
+ const { top, bottom, delta } = next.scrollHint
175
+
176
+ // Keep DECSTBM away from the terminal's last visible row. In alt-screen
177
+ // layouts we reserve that lane for status/cursor parking, and scrolling
178
+ // it can leave transient ghosting/bleed artifacts until a later repaint.
179
+ if (top >= 0 && bottom < prev.screen.height - 1 && bottom < next.screen.height - 1) {
180
+ shiftRows(prev.screen, top, bottom, delta)
181
+ scrollPatch = [
182
+ {
183
+ type: 'stdout',
184
+ content:
185
+ setScrollRegion(top + 1, bottom + 1) +
186
+ (delta > 0 ? csiScrollUp(delta) : csiScrollDown(-delta)) +
187
+ RESET_SCROLL_REGION +
188
+ CURSOR_HOME
189
+ }
190
+ ]
191
+ }
192
+ }
193
+
194
+ // We have to use purely relative operations to manipulate the cursor since
195
+ // we don't know its starting point.
196
+ //
197
+ // When content height >= viewport height AND cursor is at the bottom,
198
+ // the cursor restore at the end of the previous frame caused terminal scroll.
199
+ // viewportY tells us how many rows are in scrollback from content overflow.
200
+ // Additionally, the cursor-restore scroll pushes 1 more row into scrollback.
201
+ // We need fullReset if any changes are to rows that are now in scrollback.
202
+ //
203
+ // This early full-reset check only applies in "steady state" (not growing).
204
+ // For growing, the viewportY calculation below (with cursorRestoreScroll)
205
+ // catches unreachable scrollback rows in the diff loop instead.
206
+ const cursorAtBottom = prev.cursor.y >= prev.screen.height
207
+ const isGrowing = next.screen.height > prev.screen.height
208
+
209
+ // When content fills the viewport exactly (height == viewport) and the
210
+ // cursor is at the bottom, the cursor-restore LF at the end of the
211
+ // previous frame scrolled 1 row into scrollback. Use >= to catch this.
212
+ const prevHadScrollback = cursorAtBottom && prev.screen.height >= prev.viewport.height
213
+
214
+ const isShrinking = next.screen.height < prev.screen.height
215
+ const nextFitsViewport = next.screen.height <= prev.viewport.height
216
+
217
+ // When shrinking from above-viewport to at-or-below-viewport, content that
218
+ // was in scrollback should now be visible. Terminal clear operations can't
219
+ // bring scrollback content into view, so we need a full reset.
220
+ // Use <= (not <) because even when next height equals viewport height, the
221
+ // scrollback depth from the previous render differs from a fresh render.
222
+ if (prevHadScrollback && nextFitsViewport && isShrinking) {
223
+ logForDebugging(
224
+ `Full reset (shrink->below): prevHeight=${prev.screen.height}, nextHeight=${next.screen.height}, viewport=${prev.viewport.height}`
225
+ )
226
+
227
+ return fullResetSequence_CAUSES_FLICKER(next, 'offscreen', stylePool)
228
+ }
229
+
230
+ if (
231
+ altScreen &&
232
+ prev.screen.height >= prev.viewport.height &&
233
+ prev.screen.height > 0 &&
234
+ cursorAtBottom &&
235
+ !isGrowing
236
+ ) {
237
+ // viewportY = rows in scrollback from content overflow
238
+ // +1 for the row pushed by cursor-restore scroll
239
+ const viewportY = prev.screen.height - prev.viewport.height
240
+ const scrollbackRows = viewportY + 1
241
+
242
+ let scrollbackChangeY = -1
243
+ diffEach(prev.screen, next.screen, (_x, y) => {
244
+ if (y < scrollbackRows) {
245
+ scrollbackChangeY = y
246
+
247
+ return true // early exit
248
+ }
249
+ })
250
+
251
+ if (scrollbackChangeY >= 0) {
252
+ const prevLine = readLine(prev.screen, scrollbackChangeY)
253
+ const nextLine = readLine(next.screen, scrollbackChangeY)
254
+
255
+ return fullResetSequence_CAUSES_FLICKER(next, 'offscreen', stylePool, {
256
+ triggerY: scrollbackChangeY,
257
+ prevLine,
258
+ nextLine
259
+ })
260
+ }
261
+ }
262
+
263
+ const screen = new VirtualScreen(prev.cursor, next.viewport.width)
264
+
265
+ // Treat empty screen as height 1 to avoid spurious adjustments on first render
266
+ const heightDelta = Math.max(next.screen.height, 1) - Math.max(prev.screen.height, 1)
267
+
268
+ const shrinking = heightDelta < 0
269
+ const growing = heightDelta > 0
270
+
271
+ // Handle shrinking: clear lines from the bottom
272
+ if (shrinking) {
273
+ const linesToClear = prev.screen.height - next.screen.height
274
+
275
+ // eraseLines only works within the viewport - it can't clear scrollback.
276
+ // If we need to clear more lines than fit in the viewport, some are in
277
+ // scrollback, so we need a full reset.
278
+ if (linesToClear > prev.viewport.height) {
279
+ return fullResetSequence_CAUSES_FLICKER(next, 'offscreen', this.options.stylePool)
280
+ }
281
+
282
+ // clear(N) moves cursor UP by N-1 lines and to column 0
283
+ // This puts us at line prev.screen.height - N = next.screen.height
284
+ // But we want to be at next.screen.height - 1 (bottom of new screen)
285
+ screen.txn(prev => [
286
+ [
287
+ { type: 'clear', count: linesToClear },
288
+ { type: 'cursorMove', x: 0, y: -1 }
289
+ ],
290
+ { dx: -prev.x, dy: -linesToClear }
291
+ ])
292
+ }
293
+
294
+ // viewportY = number of rows in scrollback (not visible on terminal).
295
+ // For shrinking: use max(prev, next) because terminal clears don't scroll.
296
+ // For growing: use prev state because new rows haven't scrolled old ones yet.
297
+ // When prevHadScrollback, add 1 for the cursor-restore LF that scrolled
298
+ // an additional row out of view at the end of the previous frame. Without
299
+ // this, the diff loop treats that row as reachable — but the cursor clamps
300
+ // at viewport top, causing writes to land 1 row off and garbling the output.
301
+ const cursorRestoreScroll = prevHadScrollback ? 1 : 0
302
+
303
+ const viewportY = growing
304
+ ? Math.max(0, prev.screen.height - prev.viewport.height + cursorRestoreScroll)
305
+ : Math.max(prev.screen.height, next.screen.height) - next.viewport.height + cursorRestoreScroll
306
+
307
+ let currentStyleId = stylePool.none
308
+ let currentHyperlink: Hyperlink = undefined
309
+
310
+ // First pass: render changes to existing rows (rows < prev.screen.height)
311
+ let needsFullReset = false
312
+ let resetTriggerY = -1
313
+ diffEach(prev.screen, next.screen, (x, y, removed, added) => {
314
+ // Skip new rows - we'll render them directly after
315
+ if (growing && y >= prev.screen.height) {
316
+ return
317
+ }
318
+
319
+ // Skip spacers during rendering because the terminal will automatically
320
+ // advance 2 columns when we write the wide character itself.
321
+ // SpacerTail: Second cell of a wide character
322
+ // SpacerHead: Marks line-end position where wide char wraps to next line
323
+ if (added && (added.width === CellWidth.SpacerTail || added.width === CellWidth.SpacerHead)) {
324
+ return
325
+ }
326
+
327
+ if (removed && (removed.width === CellWidth.SpacerTail || removed.width === CellWidth.SpacerHead) && !added) {
328
+ return
329
+ }
330
+
331
+ // Skip empty cells that don't need to overwrite existing content.
332
+ // This prevents writing trailing spaces that would cause unnecessary
333
+ // line wrapping at the edge of the screen.
334
+ // Uses isEmptyCellAt to check if both packed words are zero (empty cell).
335
+ if (added && isEmptyCellAt(next.screen, x, y) && !removed) {
336
+ return
337
+ }
338
+
339
+ // If the cell outside the viewport range has changed, we need to reset
340
+ // because we can't move the cursor there to draw. In main-screen mode,
341
+ // those rows are already in terminal scrollback and invisible; resetting
342
+ // on every scrollback-only update can loop when a resize changes the
343
+ // physical buffer. Shrink-to-visible cases are handled above.
344
+ if (y < viewportY) {
345
+ if (!altScreen) {
346
+ return
347
+ }
348
+
349
+ needsFullReset = true
350
+ resetTriggerY = y
351
+
352
+ return true // early exit
353
+ }
354
+
355
+ moveCursorTo(screen, x, y)
356
+
357
+ if (added) {
358
+ const targetHyperlink = added.hyperlink
359
+ currentHyperlink = transitionHyperlink(screen.diff, currentHyperlink, targetHyperlink)
360
+ const styleStr = stylePool.transition(currentStyleId, added.styleId)
361
+
362
+ if (writeCellWithStyleStr(screen, added, styleStr)) {
363
+ currentStyleId = added.styleId
364
+ }
365
+ } else if (removed) {
366
+ // Cell was removed - clear it with a space
367
+ // (This handles shrinking content)
368
+ // Reset any active styles/hyperlinks first to avoid leaking into cleared cells
369
+ const styleIdToReset = currentStyleId
370
+ const hyperlinkToReset = currentHyperlink
371
+ currentStyleId = stylePool.none
372
+ currentHyperlink = undefined
373
+
374
+ screen.txn(() => {
375
+ const patches: Diff = []
376
+ transitionStyle(patches, stylePool, styleIdToReset, stylePool.none)
377
+ transitionHyperlink(patches, hyperlinkToReset, undefined)
378
+ patches.push({ type: 'stdout', content: ' ' })
379
+
380
+ return [patches, { dx: 1, dy: 0 }]
381
+ })
382
+ }
383
+ })
384
+
385
+ if (needsFullReset) {
386
+ return fullResetSequence_CAUSES_FLICKER(next, 'offscreen', stylePool, {
387
+ triggerY: resetTriggerY,
388
+ prevLine: readLine(prev.screen, resetTriggerY),
389
+ nextLine: readLine(next.screen, resetTriggerY)
390
+ })
391
+ }
392
+
393
+ // Reset styles before rendering new rows (they'll set their own styles)
394
+ currentStyleId = transitionStyle(screen.diff, stylePool, currentStyleId, stylePool.none)
395
+ currentHyperlink = transitionHyperlink(screen.diff, currentHyperlink, undefined)
396
+
397
+ // Handle growth: render new rows directly (they naturally scroll the terminal)
398
+ if (growing) {
399
+ renderFrameSlice(screen, next, prev.screen.height, next.screen.height, stylePool)
400
+ }
401
+
402
+ // Restore cursor. Skipped in alt-screen: the cursor is hidden, its
403
+ // position only matters as the starting point for the NEXT frame's
404
+ // relative moves, and in alt-screen the next frame always begins with
405
+ // CSI H (see ink.tsx onRender) which resets to (0,0) regardless. This
406
+ // saves a CR + cursorMove round-trip (~6-10 bytes) every frame.
407
+ //
408
+ // Main screen: if cursor needs to be past the last line of content
409
+ // (typical: cursor.y = screen.height), emit \n to create that line
410
+ // since cursor movement can't create new lines.
411
+ if (altScreen) {
412
+ // no-op; next frame's CSI H anchors cursor
413
+ } else if (next.cursor.y >= next.screen.height) {
414
+ // Move to column 0 of current line, then emit newlines to reach target row
415
+ screen.txn(prev => {
416
+ const rowsToCreate = next.cursor.y - prev.y
417
+
418
+ if (rowsToCreate > 0) {
419
+ // Use CR to resolve pending wrap (if any) without advancing
420
+ // to the next line, then LF to create each new row.
421
+ const patches: Diff = new Array<Diff[number]>(1 + rowsToCreate)
422
+ patches[0] = CARRIAGE_RETURN
423
+
424
+ for (let i = 0; i < rowsToCreate; i++) {
425
+ patches[1 + i] = NEWLINE
426
+ }
427
+
428
+ return [patches, { dx: -prev.x, dy: rowsToCreate }]
429
+ }
430
+
431
+ // At or past target row - need to move cursor to correct position
432
+ const dy = next.cursor.y - prev.y
433
+
434
+ if (dy !== 0 || prev.x !== next.cursor.x) {
435
+ // Use CR to clear pending wrap (if any), then cursor move
436
+ const patches: Diff = [CARRIAGE_RETURN]
437
+ patches.push({ type: 'cursorMove', x: next.cursor.x, y: dy })
438
+
439
+ return [patches, { dx: next.cursor.x - prev.x, dy }]
440
+ }
441
+
442
+ return [[], { dx: 0, dy: 0 }]
443
+ })
444
+ } else {
445
+ moveCursorTo(screen, next.cursor.x, next.cursor.y)
446
+ }
447
+
448
+ const elapsed = performance.now() - startTime
449
+
450
+ if (elapsed > 50) {
451
+ const damage = next.screen.damage
452
+
453
+ const damageInfo = damage ? `${damage.width}x${damage.height} at (${damage.x},${damage.y})` : 'none'
454
+
455
+ logForDebugging(
456
+ `Slow render: ${elapsed.toFixed(1)}ms, screen: ${next.screen.height}x${next.screen.width}, damage: ${damageInfo}, changes: ${screen.diff.length}`
457
+ )
458
+ }
459
+
460
+ return scrollPatch.length > 0 ? [...scrollPatch, ...screen.diff] : screen.diff
461
+ }
462
+ }
463
+
464
+ function transitionHyperlink(diff: Diff, current: Hyperlink, target: Hyperlink): Hyperlink {
465
+ if (current !== target) {
466
+ diff.push({ type: 'hyperlink', uri: target ?? '' })
467
+
468
+ return target
469
+ }
470
+
471
+ return current
472
+ }
473
+
474
+ function transitionStyle(diff: Diff, stylePool: StylePool, currentId: number, targetId: number): number {
475
+ const str = stylePool.transition(currentId, targetId)
476
+
477
+ if (str.length > 0) {
478
+ diff.push({ type: 'styleStr', str })
479
+ }
480
+
481
+ return targetId
482
+ }
483
+
484
+ function readLine(screen: Screen, y: number): string {
485
+ let line = ''
486
+
487
+ for (let x = 0; x < screen.width; x++) {
488
+ line += charInCellAt(screen, x, y) ?? ' '
489
+ }
490
+
491
+ return line.trimEnd()
492
+ }
493
+
494
+ function fullResetSequence_CAUSES_FLICKER(
495
+ frame: Frame,
496
+ reason: FlickerReason,
497
+ stylePool: StylePool,
498
+ debug?: { triggerY: number; prevLine: string; nextLine: string }
499
+ ): Diff {
500
+ // After clearTerminal, cursor is at (0, 0)
501
+ const screen = new VirtualScreen({ x: 0, y: 0 }, frame.viewport.width)
502
+ renderFrame(screen, frame, stylePool)
503
+
504
+ return [{ type: 'clearTerminal', reason, debug }, ...screen.diff]
505
+ }
506
+
507
+ function renderFrame(screen: VirtualScreen, frame: Frame, stylePool: StylePool): void {
508
+ renderFrameSlice(screen, frame, 0, frame.screen.height, stylePool)
509
+ }
510
+
511
+ /**
512
+ * Render a slice of rows from the frame's screen.
513
+ * Each row is rendered followed by a newline. Cursor ends at (0, endY).
514
+ */
515
+ function renderFrameSlice(
516
+ screen: VirtualScreen,
517
+ frame: Frame,
518
+ startY: number,
519
+ endY: number,
520
+ stylePool: StylePool
521
+ ): VirtualScreen {
522
+ let currentStyleId = stylePool.none
523
+ let currentHyperlink: Hyperlink = undefined
524
+ // Track the styleId of the last rendered cell on this line (-1 if none).
525
+ // Passed to visibleCellAtIndex to enable fg-only space optimization.
526
+ let lastRenderedStyleId = -1
527
+
528
+ const { width: screenWidth, cells, charPool, hyperlinkPool } = frame.screen
529
+
530
+ let index = startY * screenWidth
531
+
532
+ for (let y = startY; y < endY; y += 1) {
533
+ // Advance cursor to this row using LF (not CSI CUD / cursor-down).
534
+ // CSI CUD stops at the viewport bottom margin and cannot scroll,
535
+ // but LF scrolls the viewport to create new lines. Without this,
536
+ // when the cursor is at the viewport bottom, moveCursorTo's
537
+ // cursor-down silently fails, creating a permanent off-by-one
538
+ // between the virtual cursor and the real terminal cursor.
539
+ if (screen.cursor.y < y) {
540
+ const rowsToAdvance = y - screen.cursor.y
541
+ screen.txn(prev => {
542
+ const patches: Diff = new Array<Diff[number]>(1 + rowsToAdvance)
543
+ patches[0] = CARRIAGE_RETURN
544
+
545
+ for (let i = 0; i < rowsToAdvance; i++) {
546
+ patches[1 + i] = NEWLINE
547
+ }
548
+
549
+ return [patches, { dx: -prev.x, dy: rowsToAdvance }]
550
+ })
551
+ }
552
+
553
+ // Reset at start of each line — no cell rendered yet
554
+ lastRenderedStyleId = -1
555
+
556
+ for (let x = 0; x < screenWidth; x += 1, index += 1) {
557
+ // Skip spacers, unstyled empty cells, and fg-only styled spaces that
558
+ // match the last rendered style (since cursor-forward produces identical
559
+ // visual result). visibleCellAtIndex handles the optimization internally
560
+ // to avoid allocating Cell objects for skipped cells.
561
+ const cell = visibleCellAtIndex(cells, charPool, hyperlinkPool, index, lastRenderedStyleId)
562
+
563
+ if (!cell) {
564
+ continue
565
+ }
566
+
567
+ moveCursorTo(screen, x, y)
568
+
569
+ // Handle hyperlink
570
+ const targetHyperlink = cell.hyperlink
571
+ currentHyperlink = transitionHyperlink(screen.diff, currentHyperlink, targetHyperlink)
572
+
573
+ // Style transition — cached string, zero allocations after warmup
574
+ const styleStr = stylePool.transition(currentStyleId, cell.styleId)
575
+
576
+ if (writeCellWithStyleStr(screen, cell, styleStr)) {
577
+ currentStyleId = cell.styleId
578
+ lastRenderedStyleId = cell.styleId
579
+ }
580
+ }
581
+
582
+ // Reset styles/hyperlinks before newline so background color doesn't
583
+ // bleed into the next line when the terminal scrolls. The old code
584
+ // reset implicitly by writing trailing unstyled spaces; now that we
585
+ // skip empty cells, we must reset explicitly.
586
+ currentStyleId = transitionStyle(screen.diff, stylePool, currentStyleId, stylePool.none)
587
+ currentHyperlink = transitionHyperlink(screen.diff, currentHyperlink, undefined)
588
+ // CR+LF at end of row — \r resets to column 0, \n moves to next line.
589
+ // Without \r, the terminal cursor stays at whatever column content ended
590
+ // (since we skip trailing spaces, this can be mid-row).
591
+ screen.txn(prev => [[CARRIAGE_RETURN, NEWLINE], { dx: -prev.x, dy: 1 }])
592
+ }
593
+
594
+ // Reset any open style/hyperlink at end of slice
595
+ transitionStyle(screen.diff, stylePool, currentStyleId, stylePool.none)
596
+ transitionHyperlink(screen.diff, currentHyperlink, undefined)
597
+
598
+ return screen
599
+ }
600
+
601
+ type Delta = { dx: number; dy: number }
602
+
603
+ /**
604
+ * Write a cell with a pre-serialized style transition string (from
605
+ * StylePool.transition). Inlines the txn logic to avoid closure/tuple/delta
606
+ * allocations on every cell.
607
+ *
608
+ * Returns true if the cell was written, false if skipped (wide char at
609
+ * viewport edge). Callers MUST gate currentStyleId updates on this — when
610
+ * skipped, styleStr is never pushed and the terminal's style state is
611
+ * unchanged. Updating the virtual tracker anyway desyncs it from the
612
+ * terminal, and the next transition is computed from phantom state.
613
+ */
614
+ function writeCellWithStyleStr(screen: VirtualScreen, cell: Cell, styleStr: string): boolean {
615
+ const cellWidth = cell.width === CellWidth.Wide ? 2 : 1
616
+ const px = screen.cursor.x
617
+ const vw = screen.viewportWidth
618
+
619
+ // Don't write wide chars that would cross the viewport edge.
620
+ // Single-codepoint chars (CJK) at vw-2 are safe; multi-codepoint
621
+ // graphemes (flags, ZWJ emoji) need stricter threshold.
622
+ if (cellWidth === 2 && px < vw) {
623
+ const threshold = cell.char.length > 2 ? vw : vw + 1
624
+
625
+ if (px + 2 >= threshold) {
626
+ return false
627
+ }
628
+ }
629
+
630
+ const diff = screen.diff
631
+
632
+ if (styleStr.length > 0) {
633
+ diff.push({ type: 'styleStr', str: styleStr })
634
+ }
635
+
636
+ const needsCompensation = cellWidth === 2 && needsWidthCompensation(cell.char)
637
+
638
+ // On terminals with old wcwidth tables, a compensated emoji only advances
639
+ // the cursor 1 column, so the CHA below skips column x+1 without painting
640
+ // it. Write a styled space there first — on correct terminals the emoji
641
+ // glyph (width 2) overwrites it harmlessly; on old terminals it fills the
642
+ // gap with the emoji's background. Also clears any stale content at x+1.
643
+ // CHA is 1-based, so column px+1 (0-based) is CHA target px+2.
644
+ if (needsCompensation && px + 1 < vw) {
645
+ diff.push({ type: 'cursorTo', col: px + 2 })
646
+ diff.push({ type: 'stdout', content: ' ' })
647
+ diff.push({ type: 'cursorTo', col: px + 1 })
648
+ }
649
+
650
+ diff.push({ type: 'stdout', content: cell.char })
651
+
652
+ // Force terminal cursor to correct column after the emoji.
653
+ if (needsCompensation) {
654
+ diff.push({ type: 'cursorTo', col: px + cellWidth + 1 })
655
+ }
656
+
657
+ // Update cursor — mutate in place to avoid Point allocation
658
+ if (px >= vw) {
659
+ screen.cursor.x = cellWidth
660
+ screen.cursor.y++
661
+ } else {
662
+ screen.cursor.x = px + cellWidth
663
+ }
664
+
665
+ return true
666
+ }
667
+
668
+ function moveCursorTo(screen: VirtualScreen, targetX: number, targetY: number) {
669
+ screen.txn(prev => {
670
+ const dx = targetX - prev.x
671
+ const dy = targetY - prev.y
672
+ const inPendingWrap = prev.x >= screen.viewportWidth
673
+
674
+ // If we're in pending wrap state (cursor.x >= width), use CR
675
+ // to reset to column 0 on the current line without advancing
676
+ // to the next line, then issue the cursor movement.
677
+ if (inPendingWrap) {
678
+ return [[CARRIAGE_RETURN, { type: 'cursorMove', x: targetX, y: dy }], { dx, dy }]
679
+ }
680
+
681
+ // When moving to a different line, use carriage return (\r) to reset to
682
+ // column 0 first, then cursor move.
683
+ if (dy !== 0) {
684
+ return [[CARRIAGE_RETURN, { type: 'cursorMove', x: targetX, y: dy }], { dx, dy }]
685
+ }
686
+
687
+ // Standard same-line cursor move
688
+ return [[{ type: 'cursorMove', x: dx, y: dy }], { dx, dy }]
689
+ })
690
+ }
691
+
692
+ /**
693
+ * Identify emoji where the terminal's wcwidth may disagree with Unicode.
694
+ * On terminals with correct tables, the CHA we emit is a harmless no-op.
695
+ *
696
+ * Two categories:
697
+ * 1. Newer emoji (Unicode 12.0+) missing from terminal wcwidth tables.
698
+ * 2. Text-by-default emoji + VS16 (U+FE0F): the base codepoint is width 1
699
+ * in wcwidth, but VS16 triggers emoji presentation making it width 2.
700
+ * Examples: ⚔️ (U+2694), ☠️ (U+2620), ❤️ (U+2764).
701
+ */
702
+ function needsWidthCompensation(char: string): boolean {
703
+ const cp = char.codePointAt(0)
704
+
705
+ if (cp === undefined) {
706
+ return false
707
+ }
708
+
709
+ // U+1FA70-U+1FAFF: Symbols and Pictographs Extended-A (Unicode 12.0-15.0)
710
+ // U+1FB00-U+1FBFF: Symbols for Legacy Computing (Unicode 13.0)
711
+ if ((cp >= 0x1fa70 && cp <= 0x1faff) || (cp >= 0x1fb00 && cp <= 0x1fbff)) {
712
+ return true
713
+ }
714
+
715
+ // Text-by-default emoji with VS16: scan for U+FE0F in multi-codepoint
716
+ // graphemes. Single BMP chars (length 1) and surrogate pairs without VS16
717
+ // skip this check. VS16 (0xFE0F) can't collide with surrogates (0xD800-0xDFFF).
718
+ if (char.length >= 2) {
719
+ for (let i = 0; i < char.length; i++) {
720
+ if (char.charCodeAt(i) === 0xfe0f) {
721
+ return true
722
+ }
723
+ }
724
+ }
725
+
726
+ return false
727
+ }
728
+
729
+ class VirtualScreen {
730
+ // Public for direct mutation by writeCellWithStyleStr (avoids txn overhead).
731
+ // File-private class — not exposed outside log-update.ts.
732
+ cursor: Point
733
+ diff: Diff = []
734
+
735
+ constructor(
736
+ origin: Point,
737
+ readonly viewportWidth: number
738
+ ) {
739
+ this.cursor = { ...origin }
740
+ }
741
+
742
+ txn(fn: (prev: Point) => [patches: Diff, next: Delta]): void {
743
+ const [patches, next] = fn(this.cursor)
744
+
745
+ for (const patch of patches) {
746
+ this.diff.push(patch)
747
+ }
748
+
749
+ this.cursor.x += next.dx
750
+ this.cursor.y += next.dy
751
+ }
752
+ }