@deepseek-harness-tui/dsh-tui 0.5.0

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 (638) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +235 -0
  3. package/bin/dsh-tui.js +95 -0
  4. package/cordis.patch.yml +218 -0
  5. package/cordis.yml +185 -0
  6. package/lib/invariant.js +28 -0
  7. package/lib/types/activityPrefs.d.ts +29 -0
  8. package/lib/types/activityPrefs.d.ts.map +1 -0
  9. package/lib/types/activityPrefs.js +60 -0
  10. package/lib/types/bootstrap/state.d.ts +13 -0
  11. package/lib/types/bootstrap/state.d.ts.map +1 -0
  12. package/lib/types/bootstrap/state.js +12 -0
  13. package/lib/types/cc/cliHighlight.d.ts +20 -0
  14. package/lib/types/cc/cliHighlight.d.ts.map +1 -0
  15. package/lib/types/cc/cliHighlight.js +53 -0
  16. package/lib/types/cc/figures.d.ts +59 -0
  17. package/lib/types/cc/figures.d.ts.map +1 -0
  18. package/lib/types/cc/figures.js +69 -0
  19. package/lib/types/cc/format.d.ts +26 -0
  20. package/lib/types/cc/format.d.ts.map +1 -0
  21. package/lib/types/cc/format.js +55 -0
  22. package/lib/types/cc/hash.d.ts +25 -0
  23. package/lib/types/cc/hash.d.ts.map +1 -0
  24. package/lib/types/cc/hash.js +41 -0
  25. package/lib/types/cc/hyperlink.d.ts +21 -0
  26. package/lib/types/cc/hyperlink.d.ts.map +1 -0
  27. package/lib/types/cc/hyperlink.js +31 -0
  28. package/lib/types/cc/markdown.d.ts +43 -0
  29. package/lib/types/cc/markdown.d.ts.map +1 -0
  30. package/lib/types/cc/markdown.js +345 -0
  31. package/lib/types/cc/spinnerVerbs.d.ts +3 -0
  32. package/lib/types/cc/spinnerVerbs.d.ts.map +1 -0
  33. package/lib/types/cc/spinnerVerbs.js +190 -0
  34. package/lib/types/cc/terminal.d.ts +15 -0
  35. package/lib/types/cc/terminal.d.ts.map +1 -0
  36. package/lib/types/cc/terminal.js +84 -0
  37. package/lib/types/channel.d.ts +672 -0
  38. package/lib/types/channel.d.ts.map +1 -0
  39. package/lib/types/channel.js +2427 -0
  40. package/lib/types/childStderr.d.ts +63 -0
  41. package/lib/types/childStderr.d.ts.map +1 -0
  42. package/lib/types/childStderr.js +161 -0
  43. package/lib/types/commands.d.ts +55 -0
  44. package/lib/types/commands.d.ts.map +1 -0
  45. package/lib/types/commands.js +101 -0
  46. package/lib/types/components/ActivityLine.d.ts +29 -0
  47. package/lib/types/components/ActivityLine.d.ts.map +1 -0
  48. package/lib/types/components/ActivityLine.js +47 -0
  49. package/lib/types/components/ActivityPicker.d.ts +13 -0
  50. package/lib/types/components/ActivityPicker.d.ts.map +1 -0
  51. package/lib/types/components/ActivityPicker.js +24 -0
  52. package/lib/types/components/CommandSuggestions.d.ts +14 -0
  53. package/lib/types/components/CommandSuggestions.d.ts.map +1 -0
  54. package/lib/types/components/CommandSuggestions.js +33 -0
  55. package/lib/types/components/FileSuggestions.d.ts +12 -0
  56. package/lib/types/components/FileSuggestions.d.ts.map +1 -0
  57. package/lib/types/components/FileSuggestions.js +26 -0
  58. package/lib/types/components/GoalTodoPanel.d.ts +13 -0
  59. package/lib/types/components/GoalTodoPanel.d.ts.map +1 -0
  60. package/lib/types/components/GoalTodoPanel.js +57 -0
  61. package/lib/types/components/HelpMenu.d.ts +12 -0
  62. package/lib/types/components/HelpMenu.d.ts.map +1 -0
  63. package/lib/types/components/HelpMenu.js +12 -0
  64. package/lib/types/components/HistorySearchDialog.d.ts +16 -0
  65. package/lib/types/components/HistorySearchDialog.d.ts.map +1 -0
  66. package/lib/types/components/HistorySearchDialog.js +31 -0
  67. package/lib/types/components/InterruptedByUser.d.ts +7 -0
  68. package/lib/types/components/InterruptedByUser.d.ts.map +1 -0
  69. package/lib/types/components/InterruptedByUser.js +9 -0
  70. package/lib/types/components/LoadedContextPanel.d.ts +20 -0
  71. package/lib/types/components/LoadedContextPanel.d.ts.map +1 -0
  72. package/lib/types/components/LoadedContextPanel.js +30 -0
  73. package/lib/types/components/LogoV2.d.ts +23 -0
  74. package/lib/types/components/LogoV2.d.ts.map +1 -0
  75. package/lib/types/components/LogoV2.js +95 -0
  76. package/lib/types/components/Markdown.d.ts +24 -0
  77. package/lib/types/components/Markdown.d.ts.map +1 -0
  78. package/lib/types/components/Markdown.js +106 -0
  79. package/lib/types/components/MarkdownTable.d.ts +17 -0
  80. package/lib/types/components/MarkdownTable.d.ts.map +1 -0
  81. package/lib/types/components/MarkdownTable.js +272 -0
  82. package/lib/types/components/MessageList.d.ts +41 -0
  83. package/lib/types/components/MessageList.d.ts.map +1 -0
  84. package/lib/types/components/MessageList.js +303 -0
  85. package/lib/types/components/ModelPicker.d.ts +14 -0
  86. package/lib/types/components/ModelPicker.d.ts.map +1 -0
  87. package/lib/types/components/ModelPicker.js +15 -0
  88. package/lib/types/components/PresetPicker.d.ts +17 -0
  89. package/lib/types/components/PresetPicker.d.ts.map +1 -0
  90. package/lib/types/components/PresetPicker.js +25 -0
  91. package/lib/types/components/PromptInput.d.ts +67 -0
  92. package/lib/types/components/PromptInput.d.ts.map +1 -0
  93. package/lib/types/components/PromptInput.js +778 -0
  94. package/lib/types/components/ResumePicker.d.ts +15 -0
  95. package/lib/types/components/ResumePicker.d.ts.map +1 -0
  96. package/lib/types/components/ResumePicker.js +34 -0
  97. package/lib/types/components/RewindPicker.d.ts +14 -0
  98. package/lib/types/components/RewindPicker.d.ts.map +1 -0
  99. package/lib/types/components/RewindPicker.js +23 -0
  100. package/lib/types/components/SearchBox.d.ts +20 -0
  101. package/lib/types/components/SearchBox.d.ts.map +1 -0
  102. package/lib/types/components/SearchBox.js +33 -0
  103. package/lib/types/components/Select.d.ts +22 -0
  104. package/lib/types/components/Select.d.ts.map +1 -0
  105. package/lib/types/components/Select.js +19 -0
  106. package/lib/types/components/Spinner/GlimmerMessage.d.ts +19 -0
  107. package/lib/types/components/Spinner/GlimmerMessage.d.ts.map +1 -0
  108. package/lib/types/components/Spinner/GlimmerMessage.js +78 -0
  109. package/lib/types/components/Spinner/SpinnerAnimationRow.d.ts +31 -0
  110. package/lib/types/components/Spinner/SpinnerAnimationRow.d.ts.map +1 -0
  111. package/lib/types/components/Spinner/SpinnerAnimationRow.js +162 -0
  112. package/lib/types/components/Spinner/SpinnerGlyph.d.ts +16 -0
  113. package/lib/types/components/Spinner/SpinnerGlyph.d.ts.map +1 -0
  114. package/lib/types/components/Spinner/SpinnerGlyph.js +36 -0
  115. package/lib/types/components/Spinner/spinnerMode.d.ts +6 -0
  116. package/lib/types/components/Spinner/spinnerMode.d.ts.map +1 -0
  117. package/lib/types/components/Spinner/spinnerMode.js +1 -0
  118. package/lib/types/components/Spinner/spinnerUtils.d.ts +41 -0
  119. package/lib/types/components/Spinner/spinnerUtils.d.ts.map +1 -0
  120. package/lib/types/components/Spinner/spinnerUtils.js +104 -0
  121. package/lib/types/components/Spinner/useStalledAnimation.d.ts +16 -0
  122. package/lib/types/components/Spinner/useStalledAnimation.d.ts.map +1 -0
  123. package/lib/types/components/Spinner/useStalledAnimation.js +71 -0
  124. package/lib/types/components/StreamingMarkdown.d.ts +12 -0
  125. package/lib/types/components/StreamingMarkdown.d.ts.map +1 -0
  126. package/lib/types/components/StreamingMarkdown.js +39 -0
  127. package/lib/types/components/ThemePicker.d.ts +22 -0
  128. package/lib/types/components/ThemePicker.d.ts.map +1 -0
  129. package/lib/types/components/ThemePicker.js +51 -0
  130. package/lib/types/components/ThinkingToggle.d.ts +17 -0
  131. package/lib/types/components/ThinkingToggle.d.ts.map +1 -0
  132. package/lib/types/components/ThinkingToggle.js +30 -0
  133. package/lib/types/components/ToolUseLoader.d.ts +13 -0
  134. package/lib/types/components/ToolUseLoader.d.ts.map +1 -0
  135. package/lib/types/components/ToolUseLoader.js +17 -0
  136. package/lib/types/components/Whale.d.ts +22 -0
  137. package/lib/types/components/Whale.d.ts.map +1 -0
  138. package/lib/types/components/Whale.js +78 -0
  139. package/lib/types/components/WorkingSpinner.d.ts +26 -0
  140. package/lib/types/components/WorkingSpinner.d.ts.map +1 -0
  141. package/lib/types/components/WorkingSpinner.js +68 -0
  142. package/lib/types/components/activityFrames.d.ts +31 -0
  143. package/lib/types/components/activityFrames.d.ts.map +1 -0
  144. package/lib/types/components/activityFrames.js +83 -0
  145. package/lib/types/components/bigfont.d.ts +28 -0
  146. package/lib/types/components/bigfont.d.ts.map +1 -0
  147. package/lib/types/components/bigfont.js +91 -0
  148. package/lib/types/components/design-system/Byline.d.ts +14 -0
  149. package/lib/types/components/design-system/Byline.d.ts.map +1 -0
  150. package/lib/types/components/design-system/Byline.js +17 -0
  151. package/lib/types/components/design-system/Divider.d.ts +40 -0
  152. package/lib/types/components/design-system/Divider.d.ts.map +1 -0
  153. package/lib/types/components/design-system/Divider.js +24 -0
  154. package/lib/types/components/design-system/KeyboardShortcutHint.d.ts +19 -0
  155. package/lib/types/components/design-system/KeyboardShortcutHint.d.ts.map +1 -0
  156. package/lib/types/components/design-system/KeyboardShortcutHint.js +14 -0
  157. package/lib/types/components/design-system/ListItem.d.ts +36 -0
  158. package/lib/types/components/design-system/ListItem.d.ts.map +1 -0
  159. package/lib/types/components/design-system/ListItem.js +48 -0
  160. package/lib/types/components/design-system/LoadingState.d.ts +19 -0
  161. package/lib/types/components/design-system/LoadingState.d.ts.map +1 -0
  162. package/lib/types/components/design-system/LoadingState.js +22 -0
  163. package/lib/types/components/design-system/Pane.d.ts +17 -0
  164. package/lib/types/components/design-system/Pane.d.ts.map +1 -0
  165. package/lib/types/components/design-system/Pane.js +15 -0
  166. package/lib/types/components/design-system/ProgressBar.d.ts +22 -0
  167. package/lib/types/components/design-system/ProgressBar.d.ts.map +1 -0
  168. package/lib/types/components/design-system/ProgressBar.js +27 -0
  169. package/lib/types/components/design-system/StatusIcon.d.ts +15 -0
  170. package/lib/types/components/design-system/StatusIcon.d.ts.map +1 -0
  171. package/lib/types/components/design-system/StatusIcon.js +20 -0
  172. package/lib/types/components/design-system/ThemeProvider.d.ts +11 -0
  173. package/lib/types/components/design-system/ThemeProvider.d.ts.map +1 -0
  174. package/lib/types/components/design-system/ThemeProvider.js +135 -0
  175. package/lib/types/components/design-system/ThemedBox.d.ts +37 -0
  176. package/lib/types/components/design-system/ThemedBox.d.ts.map +1 -0
  177. package/lib/types/components/design-system/ThemedBox.js +26 -0
  178. package/lib/types/components/design-system/ThemedText.d.ts +56 -0
  179. package/lib/types/components/design-system/ThemedText.d.ts.map +1 -0
  180. package/lib/types/components/design-system/ThemedText.js +45 -0
  181. package/lib/types/components/messages/AssistantTextMessage.d.ts +18 -0
  182. package/lib/types/components/messages/AssistantTextMessage.d.ts.map +1 -0
  183. package/lib/types/components/messages/AssistantTextMessage.js +15 -0
  184. package/lib/types/components/messages/AssistantThinkingMessage.d.ts +23 -0
  185. package/lib/types/components/messages/AssistantThinkingMessage.d.ts.map +1 -0
  186. package/lib/types/components/messages/AssistantThinkingMessage.js +23 -0
  187. package/lib/types/components/messages/AssistantToolUseMessage.d.ts +23 -0
  188. package/lib/types/components/messages/AssistantToolUseMessage.d.ts.map +1 -0
  189. package/lib/types/components/messages/AssistantToolUseMessage.js +220 -0
  190. package/lib/types/components/messages/MessageMetadata.d.ts +15 -0
  191. package/lib/types/components/messages/MessageMetadata.d.ts.map +1 -0
  192. package/lib/types/components/messages/MessageMetadata.js +20 -0
  193. package/lib/types/components/messages/UserPromptMessage.d.ts +19 -0
  194. package/lib/types/components/messages/UserPromptMessage.d.ts.map +1 -0
  195. package/lib/types/components/messages/UserPromptMessage.js +15 -0
  196. package/lib/types/components/questions/AskUserQuestionPanel.d.ts +39 -0
  197. package/lib/types/components/questions/AskUserQuestionPanel.d.ts.map +1 -0
  198. package/lib/types/components/questions/AskUserQuestionPanel.js +228 -0
  199. package/lib/types/components/shimmer.d.ts +35 -0
  200. package/lib/types/components/shimmer.d.ts.map +1 -0
  201. package/lib/types/components/shimmer.js +43 -0
  202. package/lib/types/components/whaleFrames.d.ts +25 -0
  203. package/lib/types/components/whaleFrames.d.ts.map +1 -0
  204. package/lib/types/components/whaleFrames.js +419 -0
  205. package/lib/types/customTheme.d.ts +104 -0
  206. package/lib/types/customTheme.d.ts.map +1 -0
  207. package/lib/types/customTheme.js +268 -0
  208. package/lib/types/effortPrefs.d.ts +22 -0
  209. package/lib/types/effortPrefs.d.ts.map +1 -0
  210. package/lib/types/effortPrefs.js +45 -0
  211. package/lib/types/history.d.ts +24 -0
  212. package/lib/types/history.d.ts.map +1 -0
  213. package/lib/types/history.js +75 -0
  214. package/lib/types/hooks/useBlink.d.ts +11 -0
  215. package/lib/types/hooks/useBlink.d.ts.map +1 -0
  216. package/lib/types/hooks/useBlink.js +20 -0
  217. package/lib/types/i18n.d.ts +926 -0
  218. package/lib/types/i18n.d.ts.map +1 -0
  219. package/lib/types/i18n.js +356 -0
  220. package/lib/types/index.d.ts +70 -0
  221. package/lib/types/index.d.ts.map +1 -0
  222. package/lib/types/index.js +31 -0
  223. package/lib/types/ink/Ansi.d.ts +11 -0
  224. package/lib/types/ink/Ansi.d.ts.map +1 -0
  225. package/lib/types/ink/Ansi.js +269 -0
  226. package/lib/types/ink/bidi.d.ts +18 -0
  227. package/lib/types/ink/bidi.d.ts.map +1 -0
  228. package/lib/types/ink/bidi.js +119 -0
  229. package/lib/types/ink/clearTerminal.d.ts +22 -0
  230. package/lib/types/ink/clearTerminal.d.ts.map +1 -0
  231. package/lib/types/ink/clearTerminal.js +72 -0
  232. package/lib/types/ink/colorize.d.ts +33 -0
  233. package/lib/types/ink/colorize.d.ts.map +1 -0
  234. package/lib/types/ink/colorize.js +213 -0
  235. package/lib/types/ink/components/AlternateScreen.d.ts +22 -0
  236. package/lib/types/ink/components/AlternateScreen.d.ts.map +1 -0
  237. package/lib/types/ink/components/AlternateScreen.js +74 -0
  238. package/lib/types/ink/components/App.d.ts +73 -0
  239. package/lib/types/ink/components/App.d.ts.map +1 -0
  240. package/lib/types/ink/components/App.js +597 -0
  241. package/lib/types/ink/components/AppContext.d.ts +15 -0
  242. package/lib/types/ink/components/AppContext.d.ts.map +1 -0
  243. package/lib/types/ink/components/AppContext.js +11 -0
  244. package/lib/types/ink/components/Box.d.ts +49 -0
  245. package/lib/types/ink/components/Box.d.ts.map +1 -0
  246. package/lib/types/ink/components/Box.js +153 -0
  247. package/lib/types/ink/components/Button.d.ts +37 -0
  248. package/lib/types/ink/components/Button.d.ts.map +1 -0
  249. package/lib/types/ink/components/Button.js +166 -0
  250. package/lib/types/ink/components/ClockContext.d.ts +10 -0
  251. package/lib/types/ink/components/ClockContext.d.ts.map +1 -0
  252. package/lib/types/ink/components/ClockContext.js +108 -0
  253. package/lib/types/ink/components/CursorDeclarationContext.d.ts +30 -0
  254. package/lib/types/ink/components/CursorDeclarationContext.d.ts.map +1 -0
  255. package/lib/types/ink/components/CursorDeclarationContext.js +6 -0
  256. package/lib/types/ink/components/ErrorOverview.d.ts +7 -0
  257. package/lib/types/ink/components/ErrorOverview.d.ts.map +1 -0
  258. package/lib/types/ink/components/ErrorOverview.js +49 -0
  259. package/lib/types/ink/components/Link.d.ts +8 -0
  260. package/lib/types/ink/components/Link.d.ts.map +1 -0
  261. package/lib/types/ink/components/Link.js +33 -0
  262. package/lib/types/ink/components/Newline.d.ts +13 -0
  263. package/lib/types/ink/components/Newline.d.ts.map +1 -0
  264. package/lib/types/ink/components/Newline.js +29 -0
  265. package/lib/types/ink/components/NoSelect.d.ts +20 -0
  266. package/lib/types/ink/components/NoSelect.d.ts.map +1 -0
  267. package/lib/types/ink/components/NoSelect.js +56 -0
  268. package/lib/types/ink/components/RawAnsi.d.ts +17 -0
  269. package/lib/types/ink/components/RawAnsi.d.ts.map +1 -0
  270. package/lib/types/ink/components/RawAnsi.js +45 -0
  271. package/lib/types/ink/components/ScrollBox.d.ts +78 -0
  272. package/lib/types/ink/components/ScrollBox.d.ts.map +1 -0
  273. package/lib/types/ink/components/ScrollBox.js +174 -0
  274. package/lib/types/ink/components/Spacer.d.ts +6 -0
  275. package/lib/types/ink/components/Spacer.d.ts.map +1 -0
  276. package/lib/types/ink/components/Spacer.js +19 -0
  277. package/lib/types/ink/components/StdinContext.d.ts +32 -0
  278. package/lib/types/ink/components/StdinContext.d.ts.map +1 -0
  279. package/lib/types/ink/components/StdinContext.js +16 -0
  280. package/lib/types/ink/components/TerminalFocusContext.d.ts +11 -0
  281. package/lib/types/ink/components/TerminalFocusContext.d.ts.map +1 -0
  282. package/lib/types/ink/components/TerminalFocusContext.js +45 -0
  283. package/lib/types/ink/components/TerminalSizeContext.d.ts +6 -0
  284. package/lib/types/ink/components/TerminalSizeContext.d.ts.map +1 -0
  285. package/lib/types/ink/components/TerminalSizeContext.js +3 -0
  286. package/lib/types/ink/components/Text.d.ts +56 -0
  287. package/lib/types/ink/components/Text.d.ts.map +1 -0
  288. package/lib/types/ink/components/Text.js +194 -0
  289. package/lib/types/ink/constants.d.ts +3 -0
  290. package/lib/types/ink/constants.d.ts.map +1 -0
  291. package/lib/types/ink/constants.js +2 -0
  292. package/lib/types/ink/cursor.d.ts +11 -0
  293. package/lib/types/ink/cursor.d.ts.map +1 -0
  294. package/lib/types/ink/cursor.js +1 -0
  295. package/lib/types/ink/devtools.d.ts +7 -0
  296. package/lib/types/ink/devtools.d.ts.map +1 -0
  297. package/lib/types/ink/devtools.js +1 -0
  298. package/lib/types/ink/dom.d.ts +178 -0
  299. package/lib/types/ink/dom.d.ts.map +1 -0
  300. package/lib/types/ink/dom.js +424 -0
  301. package/lib/types/ink/events/click-event.d.ts +32 -0
  302. package/lib/types/ink/events/click-event.d.ts.map +1 -0
  303. package/lib/types/ink/events/click-event.js +36 -0
  304. package/lib/types/ink/events/dispatcher.d.ts +63 -0
  305. package/lib/types/ink/events/dispatcher.d.ts.map +1 -0
  306. package/lib/types/ink/events/dispatcher.js +194 -0
  307. package/lib/types/ink/events/emitter.d.ts +20 -0
  308. package/lib/types/ink/events/emitter.d.ts.map +1 -0
  309. package/lib/types/ink/events/emitter.js +43 -0
  310. package/lib/types/ink/events/event-handlers.d.ts +47 -0
  311. package/lib/types/ink/events/event-handlers.d.ts.map +1 -0
  312. package/lib/types/ink/events/event-handlers.js +30 -0
  313. package/lib/types/ink/events/event.d.ts +19 -0
  314. package/lib/types/ink/events/event.d.ts.map +1 -0
  315. package/lib/types/ink/events/event.js +22 -0
  316. package/lib/types/ink/events/focus-event.d.ts +19 -0
  317. package/lib/types/ink/events/focus-event.d.ts.map +1 -0
  318. package/lib/types/ink/events/focus-event.js +21 -0
  319. package/lib/types/ink/events/input-event.d.ts +53 -0
  320. package/lib/types/ink/events/input-event.d.ts.map +1 -0
  321. package/lib/types/ink/events/input-event.js +180 -0
  322. package/lib/types/ink/events/keyboard-event.d.ts +40 -0
  323. package/lib/types/ink/events/keyboard-event.d.ts.map +1 -0
  324. package/lib/types/ink/events/keyboard-event.js +65 -0
  325. package/lib/types/ink/events/paste-event.d.ts +11 -0
  326. package/lib/types/ink/events/paste-event.d.ts.map +1 -0
  327. package/lib/types/ink/events/paste-event.js +13 -0
  328. package/lib/types/ink/events/resize-event.d.ts +13 -0
  329. package/lib/types/ink/events/resize-event.d.ts.map +1 -0
  330. package/lib/types/ink/events/resize-event.js +16 -0
  331. package/lib/types/ink/events/terminal-event.d.ts +112 -0
  332. package/lib/types/ink/events/terminal-event.d.ts.map +1 -0
  333. package/lib/types/ink/events/terminal-event.js +131 -0
  334. package/lib/types/ink/events/terminal-focus-event.d.ts +21 -0
  335. package/lib/types/ink/events/terminal-focus-event.d.ts.map +1 -0
  336. package/lib/types/ink/events/terminal-focus-event.js +18 -0
  337. package/lib/types/ink/focus.d.ts +74 -0
  338. package/lib/types/ink/focus.d.ts.map +1 -0
  339. package/lib/types/ink/focus.js +188 -0
  340. package/lib/types/ink/frame.d.ts +119 -0
  341. package/lib/types/ink/frame.d.ts.map +1 -0
  342. package/lib/types/ink/frame.js +43 -0
  343. package/lib/types/ink/get-max-width.d.ts +20 -0
  344. package/lib/types/ink/get-max-width.d.ts.map +1 -0
  345. package/lib/types/ink/get-max-width.js +25 -0
  346. package/lib/types/ink/hit-test.d.ts +47 -0
  347. package/lib/types/ink/hit-test.d.ts.map +1 -0
  348. package/lib/types/ink/hit-test.js +126 -0
  349. package/lib/types/ink/hooks/use-animation-frame.d.ts +27 -0
  350. package/lib/types/ink/hooks/use-animation-frame.d.ts.map +1 -0
  351. package/lib/types/ink/hooks/use-animation-frame.js +48 -0
  352. package/lib/types/ink/hooks/use-app.d.ts +8 -0
  353. package/lib/types/ink/hooks/use-app.d.ts.map +1 -0
  354. package/lib/types/ink/hooks/use-app.js +8 -0
  355. package/lib/types/ink/hooks/use-copy-on-select.d.ts +22 -0
  356. package/lib/types/ink/hooks/use-copy-on-select.d.ts.map +1 -0
  357. package/lib/types/ink/hooks/use-copy-on-select.js +40 -0
  358. package/lib/types/ink/hooks/use-declared-cursor.d.ts +31 -0
  359. package/lib/types/ink/hooks/use-declared-cursor.d.ts.map +1 -0
  360. package/lib/types/ink/hooks/use-declared-cursor.js +65 -0
  361. package/lib/types/ink/hooks/use-input.d.ts +41 -0
  362. package/lib/types/ink/hooks/use-input.d.ts.map +1 -0
  363. package/lib/types/ink/hooks/use-input.js +73 -0
  364. package/lib/types/ink/hooks/use-interval.d.ts +23 -0
  365. package/lib/types/ink/hooks/use-interval.d.ts.map +1 -0
  366. package/lib/types/ink/hooks/use-interval.js +58 -0
  367. package/lib/types/ink/hooks/use-search-highlight.d.ts +34 -0
  368. package/lib/types/ink/hooks/use-search-highlight.d.ts.map +1 -0
  369. package/lib/types/ink/hooks/use-search-highlight.js +35 -0
  370. package/lib/types/ink/hooks/use-selection.d.ts +44 -0
  371. package/lib/types/ink/hooks/use-selection.d.ts.map +1 -0
  372. package/lib/types/ink/hooks/use-selection.js +64 -0
  373. package/lib/types/ink/hooks/use-stdin.d.ts +8 -0
  374. package/lib/types/ink/hooks/use-stdin.d.ts.map +1 -0
  375. package/lib/types/ink/hooks/use-stdin.js +8 -0
  376. package/lib/types/ink/hooks/use-tab-status.d.ts +18 -0
  377. package/lib/types/ink/hooks/use-tab-status.d.ts.map +1 -0
  378. package/lib/types/ink/hooks/use-tab-status.js +58 -0
  379. package/lib/types/ink/hooks/use-terminal-focus.d.ts +11 -0
  380. package/lib/types/ink/hooks/use-terminal-focus.d.ts.map +1 -0
  381. package/lib/types/ink/hooks/use-terminal-focus.js +15 -0
  382. package/lib/types/ink/hooks/use-terminal-size.d.ts +6 -0
  383. package/lib/types/ink/hooks/use-terminal-size.d.ts.map +1 -0
  384. package/lib/types/ink/hooks/use-terminal-size.js +12 -0
  385. package/lib/types/ink/hooks/use-terminal-title.d.ts +14 -0
  386. package/lib/types/ink/hooks/use-terminal-title.d.ts.map +1 -0
  387. package/lib/types/ink/hooks/use-terminal-title.js +30 -0
  388. package/lib/types/ink/hooks/use-terminal-viewport.d.ts +32 -0
  389. package/lib/types/ink/hooks/use-terminal-viewport.d.ts.map +1 -0
  390. package/lib/types/ink/hooks/use-terminal-viewport.js +79 -0
  391. package/lib/types/ink/ink.d.ts +370 -0
  392. package/lib/types/ink/ink.d.ts.map +1 -0
  393. package/lib/types/ink/ink.js +1665 -0
  394. package/lib/types/ink/instances.d.ts +10 -0
  395. package/lib/types/ink/instances.d.ts.map +1 -0
  396. package/lib/types/ink/instances.js +13 -0
  397. package/lib/types/ink/layout/engine.d.ts +7 -0
  398. package/lib/types/ink/layout/engine.d.ts.map +1 -0
  399. package/lib/types/ink/layout/engine.js +8 -0
  400. package/lib/types/ink/layout/geometry.d.ts +86 -0
  401. package/lib/types/ink/layout/geometry.d.ts.map +1 -0
  402. package/lib/types/ink/layout/geometry.js +95 -0
  403. package/lib/types/ink/layout/node.d.ts +151 -0
  404. package/lib/types/ink/layout/node.d.ts.map +1 -0
  405. package/lib/types/ink/layout/node.js +72 -0
  406. package/lib/types/ink/layout/yoga.d.ts +69 -0
  407. package/lib/types/ink/layout/yoga.d.ts.map +1 -0
  408. package/lib/types/ink/layout/yoga.js +249 -0
  409. package/lib/types/ink/line-width-cache.d.ts +9 -0
  410. package/lib/types/ink/line-width-cache.d.ts.map +1 -0
  411. package/lib/types/ink/line-width-cache.js +45 -0
  412. package/lib/types/ink/log-update.d.ts +57 -0
  413. package/lib/types/ink/log-update.d.ts.map +1 -0
  414. package/lib/types/ink/log-update.js +710 -0
  415. package/lib/types/ink/measure-element.d.ts +19 -0
  416. package/lib/types/ink/measure-element.d.ts.map +1 -0
  417. package/lib/types/ink/measure-element.js +10 -0
  418. package/lib/types/ink/measure-text.d.ts +13 -0
  419. package/lib/types/ink/measure-text.d.ts.map +1 -0
  420. package/lib/types/ink/measure-text.js +38 -0
  421. package/lib/types/ink/node-cache.d.ts +33 -0
  422. package/lib/types/ink/node-cache.d.ts.map +1 -0
  423. package/lib/types/ink/node-cache.js +42 -0
  424. package/lib/types/ink/optimizer.d.ts +18 -0
  425. package/lib/types/ink/optimizer.d.ts.map +1 -0
  426. package/lib/types/ink/optimizer.js +83 -0
  427. package/lib/types/ink/output.d.ts +200 -0
  428. package/lib/types/ink/output.d.ts.map +1 -0
  429. package/lib/types/ink/output.js +663 -0
  430. package/lib/types/ink/parse-keypress.d.ts +132 -0
  431. package/lib/types/ink/parse-keypress.d.ts.map +1 -0
  432. package/lib/types/ink/parse-keypress.js +707 -0
  433. package/lib/types/ink/reconciler.d.ts +43 -0
  434. package/lib/types/ink/reconciler.d.ts.map +1 -0
  435. package/lib/types/ink/reconciler.js +413 -0
  436. package/lib/types/ink/render-border.d.ts +36 -0
  437. package/lib/types/ink/render-border.d.ts.map +1 -0
  438. package/lib/types/ink/render-border.js +143 -0
  439. package/lib/types/ink/render-node-to-output.d.ts +93 -0
  440. package/lib/types/ink/render-node-to-output.d.ts.map +1 -0
  441. package/lib/types/ink/render-node-to-output.js +1290 -0
  442. package/lib/types/ink/render-to-screen.d.ts +65 -0
  443. package/lib/types/ink/render-to-screen.d.ts.map +1 -0
  444. package/lib/types/ink/render-to-screen.js +189 -0
  445. package/lib/types/ink/renderer.d.ts +24 -0
  446. package/lib/types/ink/renderer.d.ts.map +1 -0
  447. package/lib/types/ink/renderer.js +136 -0
  448. package/lib/types/ink/root.d.ts +91 -0
  449. package/lib/types/ink/root.d.ts.map +1 -0
  450. package/lib/types/ink/root.js +93 -0
  451. package/lib/types/ink/screen.d.ts +443 -0
  452. package/lib/types/ink/screen.d.ts.map +1 -0
  453. package/lib/types/ink/screen.js +1295 -0
  454. package/lib/types/ink/searchHighlight.d.ts +25 -0
  455. package/lib/types/ink/searchHighlight.d.ts.map +1 -0
  456. package/lib/types/ink/searchHighlight.js +82 -0
  457. package/lib/types/ink/selection.d.ts +323 -0
  458. package/lib/types/ink/selection.d.ts.map +1 -0
  459. package/lib/types/ink/selection.js +881 -0
  460. package/lib/types/ink/squash-text-nodes.d.ts +30 -0
  461. package/lib/types/ink/squash-text-nodes.d.ts.map +1 -0
  462. package/lib/types/ink/squash-text-nodes.js +63 -0
  463. package/lib/types/ink/stringWidth.d.ts +11 -0
  464. package/lib/types/ink/stringWidth.d.ts.map +1 -0
  465. package/lib/types/ink/stringWidth.js +209 -0
  466. package/lib/types/ink/styles.d.ts +324 -0
  467. package/lib/types/ink/styles.d.ts.map +1 -0
  468. package/lib/types/ink/styles.js +307 -0
  469. package/lib/types/ink/supports-hyperlinks.d.ts +20 -0
  470. package/lib/types/ink/supports-hyperlinks.d.ts.map +1 -0
  471. package/lib/types/ink/supports-hyperlinks.js +44 -0
  472. package/lib/types/ink/tabstops.d.ts +9 -0
  473. package/lib/types/ink/tabstops.d.ts.map +1 -0
  474. package/lib/types/ink/tabstops.js +46 -0
  475. package/lib/types/ink/terminal-focus-state.d.ts +35 -0
  476. package/lib/types/ink/terminal-focus-state.d.ts.map +1 -0
  477. package/lib/types/ink/terminal-focus-state.js +57 -0
  478. package/lib/types/ink/terminal-querier.d.ts +161 -0
  479. package/lib/types/ink/terminal-querier.d.ts.map +1 -0
  480. package/lib/types/ink/terminal-querier.js +204 -0
  481. package/lib/types/ink/terminal.d.ts +80 -0
  482. package/lib/types/ink/terminal.d.ts.map +1 -0
  483. package/lib/types/ink/terminal.js +268 -0
  484. package/lib/types/ink/termio/ansi.d.ts +75 -0
  485. package/lib/types/ink/termio/ansi.d.ts.map +1 -0
  486. package/lib/types/ink/termio/ansi.js +79 -0
  487. package/lib/types/ink/termio/csi.d.ts +253 -0
  488. package/lib/types/ink/termio/csi.d.ts.map +1 -0
  489. package/lib/types/ink/termio/csi.js +347 -0
  490. package/lib/types/ink/termio/dec.d.ts +61 -0
  491. package/lib/types/ink/termio/dec.d.ts.map +1 -0
  492. package/lib/types/ink/termio/dec.js +76 -0
  493. package/lib/types/ink/termio/esc.d.ts +15 -0
  494. package/lib/types/ink/termio/esc.d.ts.map +1 -0
  495. package/lib/types/ink/termio/esc.js +57 -0
  496. package/lib/types/ink/termio/osc.d.ts +208 -0
  497. package/lib/types/ink/termio/osc.d.ts.map +1 -0
  498. package/lib/types/ink/termio/osc.js +466 -0
  499. package/lib/types/ink/termio/parser.d.ts +46 -0
  500. package/lib/types/ink/termio/parser.d.ts.map +1 -0
  501. package/lib/types/ink/termio/parser.js +366 -0
  502. package/lib/types/ink/termio/sgr.d.ts +17 -0
  503. package/lib/types/ink/termio/sgr.d.ts.map +1 -0
  504. package/lib/types/ink/termio/sgr.js +300 -0
  505. package/lib/types/ink/termio/tokenize.d.ts +50 -0
  506. package/lib/types/ink/termio/tokenize.d.ts.map +1 -0
  507. package/lib/types/ink/termio/tokenize.js +266 -0
  508. package/lib/types/ink/termio/types.d.ts +211 -0
  509. package/lib/types/ink/termio/types.d.ts.map +1 -0
  510. package/lib/types/ink/termio/types.js +68 -0
  511. package/lib/types/ink/termio.d.ts +25 -0
  512. package/lib/types/ink/termio.d.ts.map +1 -0
  513. package/lib/types/ink/termio.js +24 -0
  514. package/lib/types/ink/truncateToWidth.d.ts +7 -0
  515. package/lib/types/ink/truncateToWidth.d.ts.map +1 -0
  516. package/lib/types/ink/truncateToWidth.js +18 -0
  517. package/lib/types/ink/useTerminalNotification.d.ts +45 -0
  518. package/lib/types/ink/useTerminalNotification.d.ts.map +1 -0
  519. package/lib/types/ink/useTerminalNotification.js +70 -0
  520. package/lib/types/ink/warn.d.ts +7 -0
  521. package/lib/types/ink/warn.d.ts.map +1 -0
  522. package/lib/types/ink/warn.js +15 -0
  523. package/lib/types/ink/widest-line.d.ts +7 -0
  524. package/lib/types/ink/widest-line.d.ts.map +1 -0
  525. package/lib/types/ink/widest-line.js +19 -0
  526. package/lib/types/ink/wrap-text.d.ts +10 -0
  527. package/lib/types/ink/wrap-text.d.ts.map +1 -0
  528. package/lib/types/ink/wrap-text.js +61 -0
  529. package/lib/types/ink/wrapAnsi.d.ts +18 -0
  530. package/lib/types/ink/wrapAnsi.d.ts.map +1 -0
  531. package/lib/types/ink/wrapAnsi.js +16 -0
  532. package/lib/types/invariant.d.ts +20 -0
  533. package/lib/types/invariant.d.ts.map +1 -0
  534. package/lib/types/invariant.js +26 -0
  535. package/lib/types/modelPrefs.d.ts +37 -0
  536. package/lib/types/modelPrefs.d.ts.map +1 -0
  537. package/lib/types/modelPrefs.js +66 -0
  538. package/lib/types/modelRoute.d.ts +79 -0
  539. package/lib/types/modelRoute.d.ts.map +1 -0
  540. package/lib/types/modelRoute.js +96 -0
  541. package/lib/types/native-ts/yoga-layout/enums.d.ts +183 -0
  542. package/lib/types/native-ts/yoga-layout/enums.d.ts.map +1 -0
  543. package/lib/types/native-ts/yoga-layout/enums.js +150 -0
  544. package/lib/types/native-ts/yoga-layout/index.d.ts +788 -0
  545. package/lib/types/native-ts/yoga-layout/index.d.ts.map +1 -0
  546. package/lib/types/native-ts/yoga-layout/index.js +2571 -0
  547. package/lib/types/packaged-skills.d.ts +10 -0
  548. package/lib/types/packaged-skills.d.ts.map +1 -0
  549. package/lib/types/packaged-skills.js +63 -0
  550. package/lib/types/plugin.d.ts +33 -0
  551. package/lib/types/plugin.d.ts.map +1 -0
  552. package/lib/types/plugin.js +454 -0
  553. package/lib/types/presetPrefs.d.ts +29 -0
  554. package/lib/types/presetPrefs.d.ts.map +1 -0
  555. package/lib/types/presetPrefs.js +61 -0
  556. package/lib/types/presets.d.ts +110 -0
  557. package/lib/types/presets.d.ts.map +1 -0
  558. package/lib/types/presets.js +114 -0
  559. package/lib/types/questions.d.ts +97 -0
  560. package/lib/types/questions.d.ts.map +1 -0
  561. package/lib/types/questions.js +201 -0
  562. package/lib/types/screens/Chat.d.ts +23 -0
  563. package/lib/types/screens/Chat.d.ts.map +1 -0
  564. package/lib/types/screens/Chat.js +1263 -0
  565. package/lib/types/screens/StatusLine.d.ts +16 -0
  566. package/lib/types/screens/StatusLine.d.ts.map +1 -0
  567. package/lib/types/screens/StatusLine.js +105 -0
  568. package/lib/types/screens/StatusMetrics.d.ts +87 -0
  569. package/lib/types/screens/StatusMetrics.d.ts.map +1 -0
  570. package/lib/types/screens/StatusMetrics.js +252 -0
  571. package/lib/types/sessionHistory.d.ts +32 -0
  572. package/lib/types/sessionHistory.d.ts.map +1 -0
  573. package/lib/types/sessionHistory.js +87 -0
  574. package/lib/types/theme.d.ts +115 -0
  575. package/lib/types/theme.d.ts.map +1 -0
  576. package/lib/types/theme.js +300 -0
  577. package/lib/types/themePrefs.d.ts +28 -0
  578. package/lib/types/themePrefs.d.ts.map +1 -0
  579. package/lib/types/themePrefs.js +59 -0
  580. package/lib/types/ui.d.ts +28 -0
  581. package/lib/types/ui.d.ts.map +1 -0
  582. package/lib/types/ui.js +25 -0
  583. package/lib/types/update.d.ts +76 -0
  584. package/lib/types/update.d.ts.map +1 -0
  585. package/lib/types/update.js +203 -0
  586. package/lib/types/utils/clipboard.d.ts +43 -0
  587. package/lib/types/utils/clipboard.d.ts.map +1 -0
  588. package/lib/types/utils/clipboard.js +82 -0
  589. package/lib/types/utils/debug.d.ts +8 -0
  590. package/lib/types/utils/debug.d.ts.map +1 -0
  591. package/lib/types/utils/debug.js +12 -0
  592. package/lib/types/utils/earlyInput.d.ts +7 -0
  593. package/lib/types/utils/earlyInput.d.ts.map +1 -0
  594. package/lib/types/utils/earlyInput.js +6 -0
  595. package/lib/types/utils/env.d.ts +10 -0
  596. package/lib/types/utils/env.d.ts.map +1 -0
  597. package/lib/types/utils/env.js +9 -0
  598. package/lib/types/utils/envUtils.d.ts +8 -0
  599. package/lib/types/utils/envUtils.d.ts.map +1 -0
  600. package/lib/types/utils/envUtils.js +13 -0
  601. package/lib/types/utils/execFileNoThrow.d.ts +34 -0
  602. package/lib/types/utils/execFileNoThrow.d.ts.map +1 -0
  603. package/lib/types/utils/execFileNoThrow.js +31 -0
  604. package/lib/types/utils/fullscreen.d.ts +8 -0
  605. package/lib/types/utils/fullscreen.d.ts.map +1 -0
  606. package/lib/types/utils/fullscreen.js +10 -0
  607. package/lib/types/utils/intl.d.ts +7 -0
  608. package/lib/types/utils/intl.d.ts.map +1 -0
  609. package/lib/types/utils/intl.js +9 -0
  610. package/lib/types/utils/loaded-context.d.ts +20 -0
  611. package/lib/types/utils/loaded-context.d.ts.map +1 -0
  612. package/lib/types/utils/loaded-context.js +36 -0
  613. package/lib/types/utils/log.d.ts +7 -0
  614. package/lib/types/utils/log.d.ts.map +1 -0
  615. package/lib/types/utils/log.js +9 -0
  616. package/lib/types/utils/mentions.d.ts +30 -0
  617. package/lib/types/utils/mentions.d.ts.map +1 -0
  618. package/lib/types/utils/mentions.js +72 -0
  619. package/lib/types/utils/narration.d.ts +11 -0
  620. package/lib/types/utils/narration.d.ts.map +1 -0
  621. package/lib/types/utils/narration.js +16 -0
  622. package/lib/types/utils/semver.d.ts +9 -0
  623. package/lib/types/utils/semver.d.ts.map +1 -0
  624. package/lib/types/utils/semver.js +24 -0
  625. package/lib/types/utils/sliceAnsi.d.ts +12 -0
  626. package/lib/types/utils/sliceAnsi.d.ts.map +1 -0
  627. package/lib/types/utils/sliceAnsi.js +79 -0
  628. package/lib/types/working-activity.d.ts +19 -0
  629. package/lib/types/working-activity.d.ts.map +1 -0
  630. package/lib/types/working-activity.js +18 -0
  631. package/package.json +141 -0
  632. package/skills/audit/SKILL.md +24 -0
  633. package/skills/bug/SKILL.md +27 -0
  634. package/skills/pr-comments/SKILL.md +20 -0
  635. package/skills/practice/SKILL.md +22 -0
  636. package/skills/release-notes/SKILL.md +25 -0
  637. package/skills/review/SKILL.md +25 -0
  638. package/skills/vuln-check/SKILL.md +25 -0
@@ -0,0 +1,1295 @@
1
+ import { ansiCodesToString, diffAnsiCodes, } from '@alcalzone/ansi-tokenize';
2
+ import { unionRect, } from './layout/geometry.js';
3
+ import { BEL, ESC, SEP } from './termio/ansi.js';
4
+ import * as warn from './warn.js';
5
+ // --- Shared Pools (interning for memory efficiency) ---
6
+ /**
7
+ * Pool of character strings shared across all screens.
8
+ * With a shared pool, interned char IDs are valid across screens, so
9
+ * blitRegion can copy IDs directly (no re-interning) and diffEach can
10
+ * compare IDs as integers (no string lookup).
11
+ */
12
+ export class CharPool {
13
+ strings = [' ', '']; // Index 0 = space, 1 = empty (spacer)
14
+ stringMap = new Map([
15
+ [' ', 0],
16
+ ['', 1],
17
+ ]);
18
+ ascii = initCharAscii(); // charCode → index, -1 = not interned
19
+ /**
20
+ * Intern a character string and return its stable pool index.
21
+ * @param char - the character string to intern.
22
+ * @returns the pool index for the string.
23
+ */
24
+ intern(char) {
25
+ // ASCII fast-path: direct array lookup instead of Map.get
26
+ if (char.length === 1) {
27
+ const code = char.charCodeAt(0);
28
+ if (code < 128) {
29
+ const cached = this.ascii[code];
30
+ if (cached !== -1)
31
+ return cached;
32
+ const index = this.strings.length;
33
+ this.strings.push(char);
34
+ this.ascii[code] = index;
35
+ return index;
36
+ }
37
+ }
38
+ const existing = this.stringMap.get(char);
39
+ if (existing !== undefined)
40
+ return existing;
41
+ const index = this.strings.length;
42
+ this.strings.push(char);
43
+ this.stringMap.set(char, index);
44
+ return index;
45
+ }
46
+ /**
47
+ * Return the character string for a pool index, or a space for an
48
+ * out-of-range index.
49
+ * @param index - the pool index to look up.
50
+ * @returns the interned string.
51
+ */
52
+ get(index) {
53
+ return this.strings[index] ?? ' ';
54
+ }
55
+ }
56
+ /**
57
+ * Pool of hyperlink strings shared across all screens.
58
+ * Index 0 represents "no hyperlink".
59
+ */
60
+ export class HyperlinkPool {
61
+ strings = ['']; // Index 0 = no hyperlink
62
+ stringMap = new Map();
63
+ /**
64
+ * Intern a hyperlink string and return its stable pool index.
65
+ * @param hyperlink - the hyperlink to intern; undefined or empty maps to 0.
66
+ * @returns the pool index for the hyperlink.
67
+ */
68
+ intern(hyperlink) {
69
+ if (!hyperlink)
70
+ return 0;
71
+ let id = this.stringMap.get(hyperlink);
72
+ if (id === undefined) {
73
+ id = this.strings.length;
74
+ this.strings.push(hyperlink);
75
+ this.stringMap.set(hyperlink, id);
76
+ }
77
+ return id;
78
+ }
79
+ /**
80
+ * Return the hyperlink string for a pool index.
81
+ * @param id - the pool index to look up.
82
+ * @returns the interned hyperlink, or undefined for index 0.
83
+ */
84
+ get(id) {
85
+ return id === 0 ? undefined : this.strings[id];
86
+ }
87
+ }
88
+ // SGR 7 (inverse) as an AnsiCode. endCode '\x1b[27m' flags VISIBLE_ON_SPACE
89
+ // so bit 0 of the resulting styleId is set → renderer won't skip inverted
90
+ // spaces as invisible.
91
+ const INVERSE_CODE = {
92
+ type: 'ansi',
93
+ code: '\x1b[7m',
94
+ endCode: '\x1b[27m',
95
+ };
96
+ // Bold (SGR 1) — stacks cleanly, no reflow in monospace. endCode 22
97
+ // also cancels dim (SGR 2); harmless here since we never add dim.
98
+ const BOLD_CODE = {
99
+ type: 'ansi',
100
+ code: '\x1b[1m',
101
+ endCode: '\x1b[22m',
102
+ };
103
+ // Underline (SGR 4). Kept alongside yellow+bold — the underline is the
104
+ // unambiguous visible-on-any-theme marker. Yellow-bg-via-inverse can
105
+ // clash with existing bg colors (user-prompt style, tool chrome, syntax
106
+ // bg). If you see underline but no yellow, the yellow is being lost in
107
+ // the existing cell styling — the overlay IS finding the match.
108
+ const UNDERLINE_CODE = {
109
+ type: 'ansi',
110
+ code: '\x1b[4m',
111
+ endCode: '\x1b[24m',
112
+ };
113
+ // fg→yellow (SGR 33). With inverse already in the stack, the terminal
114
+ // swaps fg↔bg at render — so yellow-fg becomes yellow-BG. Original bg
115
+ // becomes fg (readable on most themes: dark-bg → dark-text on yellow).
116
+ // endCode 39 is 'default fg' — cancels any prior fg color cleanly.
117
+ const YELLOW_FG_CODE = {
118
+ type: 'ansi',
119
+ code: '\x1b[33m',
120
+ endCode: '\x1b[39m',
121
+ };
122
+ /**
123
+ * Pool of interned ANSI style stacks. Each style maps to an ID whose bit 0
124
+ * encodes whether the style is visible on space characters; IDs are valid
125
+ * across all screens sharing this pool.
126
+ */
127
+ export class StylePool {
128
+ ids = new Map();
129
+ styles = [];
130
+ transitionCache = new Map();
131
+ /** Style ID of the empty style stack, used as the empty-cell style. */
132
+ none;
133
+ constructor() {
134
+ this.none = this.intern([]);
135
+ }
136
+ /**
137
+ * Intern a style and return its ID. Bit 0 of the ID encodes whether the
138
+ * style has a visible effect on space characters (background, inverse,
139
+ * underline, etc.). Foreground-only styles get even IDs; styles visible
140
+ * on spaces get odd IDs. This lets the renderer skip invisible spaces
141
+ * with a single bitmask check on the packed word.
142
+ * @param styles - the ANSI code stack to intern.
143
+ * @returns the interned style ID.
144
+ */
145
+ intern(styles) {
146
+ const key = styles.length === 0 ? '' : styles.map(s => s.code).join('\0');
147
+ let id = this.ids.get(key);
148
+ if (id === undefined) {
149
+ const rawId = this.styles.length;
150
+ this.styles.push(styles.length === 0 ? [] : styles);
151
+ id =
152
+ (rawId << 1) |
153
+ (styles.length > 0 && hasVisibleSpaceEffect(styles) ? 1 : 0);
154
+ this.ids.set(key, id);
155
+ }
156
+ return id;
157
+ }
158
+ /**
159
+ * Recover the style stack from an encoded ID. Strips the bit-0 flag via
160
+ * `>>> 1`.
161
+ * @param id - the encoded style ID.
162
+ * @returns the interned ANSI code stack, or an empty array for an unknown ID.
163
+ */
164
+ get(id) {
165
+ return this.styles[id >>> 1] ?? [];
166
+ }
167
+ /**
168
+ * Returns the pre-serialized ANSI string to transition from one style to
169
+ * another. Cached by (fromId, toId) — zero allocations after first call
170
+ * for a given pair.
171
+ * @param fromId - the style ID to transition from.
172
+ * @param toId - the style ID to transition to.
173
+ * @returns the ANSI escape sequence applying the difference, or an empty
174
+ * string when both styles are equal.
175
+ */
176
+ transition(fromId, toId) {
177
+ if (fromId === toId)
178
+ return '';
179
+ const key = fromId * 0x100000 + toId;
180
+ let str = this.transitionCache.get(key);
181
+ if (str === undefined) {
182
+ str = ansiCodesToString(diffAnsiCodes(this.get(fromId), this.get(toId)));
183
+ this.transitionCache.set(key, str);
184
+ }
185
+ return str;
186
+ }
187
+ /**
188
+ * Intern a style that is `base + inverse`. Cached by base ID so
189
+ * repeated calls for the same underlying style don't re-scan the
190
+ * AnsiCode[] array. Used by the selection overlay.
191
+ */
192
+ inverseCache = new Map();
193
+ /**
194
+ * Intern a style that is `base + inverse`, reusing the base style when it
195
+ * is already inverted to avoid stacking SGR 7.
196
+ * @param baseId - the style ID to overlay inverse onto.
197
+ * @returns the ID of the inverted style.
198
+ */
199
+ withInverse(baseId) {
200
+ let id = this.inverseCache.get(baseId);
201
+ if (id === undefined) {
202
+ const baseCodes = this.get(baseId);
203
+ // If already inverted, use as-is (avoids SGR 7 stacking)
204
+ const hasInverse = baseCodes.some(c => c.endCode === '\x1b[27m');
205
+ id = hasInverse ? baseId : this.intern([...baseCodes, INVERSE_CODE]);
206
+ this.inverseCache.set(baseId, id);
207
+ }
208
+ return id;
209
+ }
210
+ /** Inverse + bold + yellow-bg-via-fg-swap for the CURRENT search match.
211
+ * OTHER matches are plain inverse — bg inherits from the theme. Current
212
+ * gets a distinct yellow bg (via fg-then-inverse swap) plus bold weight
213
+ * so it stands out in a sea of inverse. Underline was too subtle. Zero
214
+ * reflow risk: all pure SGR overlays, per-cell, post-layout. The yellow
215
+ * overrides any existing fg (syntax highlighting) on those cells — fine,
216
+ * the "you are here" signal IS the point, syntax color can yield. */
217
+ currentMatchCache = new Map();
218
+ /**
219
+ * Intern a style marking the current search match: inverse plus bold and
220
+ * a yellow background (via fg-then-inverse swap), distinct from the plain
221
+ * inverse used for other matches.
222
+ * @param baseId - the style ID to overlay match markers onto.
223
+ * @returns the ID of the match style.
224
+ */
225
+ withCurrentMatch(baseId) {
226
+ let id = this.currentMatchCache.get(baseId);
227
+ if (id === undefined) {
228
+ const baseCodes = this.get(baseId);
229
+ // Filter BOTH fg + bg so yellow-via-inverse is unambiguous.
230
+ // User-prompt cells have an explicit bg (grey box); with that bg
231
+ // still set, inverse swaps yellow-fg↔grey-bg → grey-on-yellow on
232
+ // SOME terminals, yellow-on-grey on others (inverse semantics vary
233
+ // when both colors are explicit). Filtering both gives clean
234
+ // yellow-bg + terminal-default-fg everywhere. Bold/dim/italic
235
+ // coexist — keep those.
236
+ const codes = baseCodes.filter(c => c.endCode !== '\x1b[39m' && c.endCode !== '\x1b[49m');
237
+ // fg-yellow FIRST so inverse swaps it to bg. Bold after inverse is
238
+ // fine — SGR 1 is fg-attribute-only, order-independent vs 7.
239
+ codes.push(YELLOW_FG_CODE);
240
+ if (!baseCodes.some(c => c.endCode === '\x1b[27m'))
241
+ codes.push(INVERSE_CODE);
242
+ if (!baseCodes.some(c => c.endCode === '\x1b[22m'))
243
+ codes.push(BOLD_CODE);
244
+ // Underline as the unambiguous marker — yellow-bg can clash with
245
+ // existing bg styling (user-prompt bg, syntax bg). If you see
246
+ // underline but no yellow on a match, the overlay IS finding it;
247
+ // the yellow is just losing a styling fight.
248
+ if (!baseCodes.some(c => c.endCode === '\x1b[24m'))
249
+ codes.push(UNDERLINE_CODE);
250
+ id = this.intern(codes);
251
+ this.currentMatchCache.set(baseId, id);
252
+ }
253
+ return id;
254
+ }
255
+ /**
256
+ * Selection overlay: REPLACE the cell's background with a solid color
257
+ * while preserving its foreground (color, bold, italic, dim, underline).
258
+ * Matches native terminal selection — a dedicated bg color, not SGR-7
259
+ * inverse. Inverse swaps fg/bg per-cell, which fragments visually over
260
+ * syntax-highlighted text (every fg color becomes a different bg stripe).
261
+ *
262
+ * Strips any existing bg (endCode 49m — REPLACES, so diff-added green
263
+ * etc. don't bleed through) and any existing inverse (endCode 27m —
264
+ * inverse on top of a solid bg would re-swap and look wrong).
265
+ *
266
+ * bg is set via setSelectionBg(); null → fallback to withInverse() so the
267
+ * overlay still works before theme wiring sets a color (tests, first frame).
268
+ * Cache is keyed by baseId only — setSelectionBg() clears it on change.
269
+ */
270
+ selectionBgCode = null;
271
+ selectionBgCache = new Map();
272
+ /**
273
+ * Set the background color used by the selection overlay and clear the
274
+ * overlay cache so the new color takes effect.
275
+ * @param bg - the background ANSI code, or null to fall back to inverse.
276
+ */
277
+ setSelectionBg(bg) {
278
+ if (this.selectionBgCode?.code === bg?.code)
279
+ return;
280
+ this.selectionBgCode = bg;
281
+ this.selectionBgCache.clear();
282
+ }
283
+ /**
284
+ * Intern a style with the selection background replacing the cell's own
285
+ * background and inverse, preserving foreground attributes. Falls back to
286
+ * withInverse() when no selection background is set.
287
+ * @param baseId - the style ID to overlay the selection background onto.
288
+ * @returns the ID of the selection style.
289
+ */
290
+ withSelectionBg(baseId) {
291
+ const bg = this.selectionBgCode;
292
+ if (bg === null)
293
+ return this.withInverse(baseId);
294
+ let id = this.selectionBgCache.get(baseId);
295
+ if (id === undefined) {
296
+ // Keep everything except bg (49m) and inverse (27m). Fg, bold, dim,
297
+ // italic, underline, strikethrough all preserved.
298
+ const kept = this.get(baseId).filter(c => c.endCode !== '\x1b[49m' && c.endCode !== '\x1b[27m');
299
+ kept.push(bg);
300
+ id = this.intern(kept);
301
+ this.selectionBgCache.set(baseId, id);
302
+ }
303
+ return id;
304
+ }
305
+ }
306
+ // endCodes that produce visible effects on space characters
307
+ const VISIBLE_ON_SPACE = new Set([
308
+ '\x1b[49m', // background color
309
+ '\x1b[27m', // inverse
310
+ '\x1b[24m', // underline
311
+ '\x1b[29m', // strikethrough
312
+ '\x1b[55m', // overline
313
+ ]);
314
+ function hasVisibleSpaceEffect(styles) {
315
+ for (const style of styles) {
316
+ if (VISIBLE_ON_SPACE.has(style.endCode))
317
+ return true;
318
+ }
319
+ return false;
320
+ }
321
+ // Constants for empty/spacer cells to enable fast comparisons
322
+ // These are indices into the charStrings table, not codepoints
323
+ const EMPTY_CHAR_INDEX = 0; // ' ' (space)
324
+ const SPACER_CHAR_INDEX = 1; // '' (empty string for spacer cells)
325
+ // Unwritten cells are [EMPTY_CHAR_INDEX=0, packWord1(emptyStyleId=0,0,0)=0].
326
+ // Since StylePool.none is always 0 (first intern), unwritten cells are
327
+ // indistinguishable from explicitly-cleared cells in the packed array.
328
+ // This is intentional: diffEach can compare raw ints with zero normalization.
329
+ // isEmptyCellByIndex checks if both words are 0 to identify "never visually written" cells.
330
+ function initCharAscii() {
331
+ const table = new Int32Array(128);
332
+ table.fill(-1);
333
+ table[32] = EMPTY_CHAR_INDEX; // ' ' (space)
334
+ return table;
335
+ }
336
+ // --- Packed cell layout ---
337
+ // Each cell is 2 consecutive Int32 elements in the cells array:
338
+ // word0 (cells[ci]): charId (full 32 bits)
339
+ // word1 (cells[ci + 1]): styleId[31:17] | hyperlinkId[16:2] | width[1:0]
340
+ const STYLE_SHIFT = 17;
341
+ const HYPERLINK_SHIFT = 2;
342
+ const HYPERLINK_MASK = 0x7fff; // 15 bits
343
+ const WIDTH_MASK = 3; // 2 bits
344
+ // Pack styleId, hyperlinkId, and width into a single Int32
345
+ function packWord1(styleId, hyperlinkId, width) {
346
+ return (styleId << STYLE_SHIFT) | (hyperlinkId << HYPERLINK_SHIFT) | width;
347
+ }
348
+ // Unwritten cell as BigInt64 — both words are 0, so the 64-bit value is 0n.
349
+ // Used by BigInt64Array.fill() for bulk clears (resetScreen, clearRegion).
350
+ // Not used for comparison — BigInt element reads cause heap allocation.
351
+ const EMPTY_CELL_VALUE = 0n;
352
+ function isEmptyCellByIndex(screen, index) {
353
+ // An empty/unwritten cell has both words === 0:
354
+ // word0 = EMPTY_CHAR_INDEX (0), word1 = packWord1(emptyStyleId=0, 0, 0) = 0.
355
+ const ci = index << 1;
356
+ return screen.cells[ci] === 0 && screen.cells[ci | 1] === 0;
357
+ }
358
+ /**
359
+ * Return whether the cell at a position is empty or unwritten, treating
360
+ * out-of-bounds positions as empty.
361
+ * @param screen - the screen to read.
362
+ * @param x - the cell column.
363
+ * @param y - the cell row.
364
+ * @returns true when the cell is empty or out of bounds.
365
+ */
366
+ export function isEmptyCellAt(screen, x, y) {
367
+ if (x < 0 || y < 0 || x >= screen.width || y >= screen.height)
368
+ return true;
369
+ return isEmptyCellByIndex(screen, y * screen.width + x);
370
+ }
371
+ /**
372
+ * Check if a Cell (view object) represents an empty cell.
373
+ * @param screen - the screen the cell belongs to, for its empty style ID.
374
+ * @param cell - the cell view to inspect.
375
+ * @returns true when the cell is a space with the empty style, narrow
376
+ * width, and no hyperlink.
377
+ */
378
+ export function isCellEmpty(screen, cell) {
379
+ // Check if cell looks like an empty cell (space, empty style, narrow, no link).
380
+ // Note: After cellAt mapping, unwritten cells have emptyStyleId, so this
381
+ // returns true for both unwritten AND cleared cells. Use isEmptyCellAt
382
+ // for the internal distinction.
383
+ return (cell.char === ' ' &&
384
+ cell.styleId === screen.emptyStyleId &&
385
+ cell.width === 0 /* CellWidth.Narrow */ &&
386
+ !cell.hyperlink);
387
+ }
388
+ // Intern a hyperlink string and return its ID (0 = no hyperlink)
389
+ function internHyperlink(screen, hyperlink) {
390
+ return screen.hyperlinkPool.intern(hyperlink);
391
+ }
392
+ // ---
393
+ /**
394
+ * Create a new screen with packed cell storage backed by the given shared
395
+ * pools. Non-integer or negative dimensions are clamped to valid integers.
396
+ * @param width - the screen width in cells.
397
+ * @param height - the screen height in rows.
398
+ * @param styles - the shared style pool; its `none` ID becomes the empty style.
399
+ * @param charPool - the shared character pool.
400
+ * @param hyperlinkPool - the shared hyperlink pool.
401
+ * @returns the new empty screen.
402
+ */
403
+ export function createScreen(width, height, styles, charPool, hyperlinkPool) {
404
+ // Warn if dimensions are not valid integers (likely bad yoga layout output)
405
+ warn.ifNotInteger(width, 'createScreen width');
406
+ warn.ifNotInteger(height, 'createScreen height');
407
+ // Ensure width and height are valid integers to prevent crashes
408
+ if (!Number.isInteger(width) || width < 0) {
409
+ width = Math.max(0, Math.floor(width) || 0);
410
+ }
411
+ if (!Number.isInteger(height) || height < 0) {
412
+ height = Math.max(0, Math.floor(height) || 0);
413
+ }
414
+ const size = width * height;
415
+ // Allocate one buffer, two views: Int32Array for per-word access,
416
+ // BigInt64Array for bulk fill in resetScreen/clearRegion.
417
+ // ArrayBuffer is zero-filled, which is exactly the empty cell value:
418
+ // [EMPTY_CHAR_INDEX=0, packWord1(emptyStyleId=0,0,0)=0].
419
+ const buf = new ArrayBuffer(size << 3); // 8 bytes per cell
420
+ const cells = new Int32Array(buf);
421
+ const cells64 = new BigInt64Array(buf);
422
+ return {
423
+ width,
424
+ height,
425
+ cells,
426
+ cells64,
427
+ charPool,
428
+ hyperlinkPool,
429
+ emptyStyleId: styles.none,
430
+ damage: undefined,
431
+ noSelect: new Uint8Array(size),
432
+ softWrap: new Int32Array(height),
433
+ };
434
+ }
435
+ /**
436
+ * Reset an existing screen for reuse, avoiding allocation of new typed arrays.
437
+ * Resizes if needed and clears all cells to empty/unwritten state.
438
+ *
439
+ * For double-buffering, this allows swapping between front and back buffers
440
+ * without allocating new Screen objects each frame.
441
+ * @param screen - the screen to reset and resize.
442
+ * @param width - the new width in cells.
443
+ * @param height - the new height in rows.
444
+ */
445
+ export function resetScreen(screen, width, height) {
446
+ // Warn if dimensions are not valid integers
447
+ warn.ifNotInteger(width, 'resetScreen width');
448
+ warn.ifNotInteger(height, 'resetScreen height');
449
+ // Ensure width and height are valid integers to prevent crashes
450
+ if (!Number.isInteger(width) || width < 0) {
451
+ width = Math.max(0, Math.floor(width) || 0);
452
+ }
453
+ if (!Number.isInteger(height) || height < 0) {
454
+ height = Math.max(0, Math.floor(height) || 0);
455
+ }
456
+ const size = width * height;
457
+ // Resize if needed (only grow, to avoid reallocations)
458
+ if (screen.cells64.length < size) {
459
+ const buf = new ArrayBuffer(size << 3);
460
+ screen.cells = new Int32Array(buf);
461
+ screen.cells64 = new BigInt64Array(buf);
462
+ screen.noSelect = new Uint8Array(size);
463
+ }
464
+ if (screen.softWrap.length < height) {
465
+ screen.softWrap = new Int32Array(height);
466
+ }
467
+ // Reset all cells — single fill call, no loop
468
+ screen.cells64.fill(EMPTY_CELL_VALUE, 0, size);
469
+ screen.noSelect.fill(0, 0, size);
470
+ screen.softWrap.fill(0, 0, height);
471
+ // Update dimensions
472
+ screen.width = width;
473
+ screen.height = height;
474
+ // Shared pools accumulate — no clearing needed. Unique char/hyperlink sets are bounded.
475
+ // Clear damage tracking
476
+ screen.damage = undefined;
477
+ }
478
+ /**
479
+ * Re-intern a screen's char and hyperlink IDs into new pools.
480
+ * Used for generational pool reset — after migrating, the screen's
481
+ * typed arrays contain valid IDs for the new pools, and the old pools
482
+ * can be GC'd.
483
+ *
484
+ * O(width * height) but only called occasionally (e.g., between conversation turns).
485
+ * @param screen - the screen whose char and hyperlink IDs to migrate.
486
+ * @param charPool - the new character pool.
487
+ * @param hyperlinkPool - the new hyperlink pool.
488
+ */
489
+ export function migrateScreenPools(screen, charPool, hyperlinkPool) {
490
+ const oldCharPool = screen.charPool;
491
+ const oldHyperlinkPool = screen.hyperlinkPool;
492
+ if (oldCharPool === charPool && oldHyperlinkPool === hyperlinkPool)
493
+ return;
494
+ const size = screen.width * screen.height;
495
+ const cells = screen.cells;
496
+ // Re-intern chars and hyperlinks in a single pass, stride by 2
497
+ for (let ci = 0; ci < size << 1; ci += 2) {
498
+ // Re-intern charId (word0)
499
+ const oldCharId = cells[ci];
500
+ cells[ci] = charPool.intern(oldCharPool.get(oldCharId));
501
+ // Re-intern hyperlinkId (packed in word1)
502
+ const word1 = cells[ci + 1];
503
+ const oldHyperlinkId = (word1 >>> HYPERLINK_SHIFT) & HYPERLINK_MASK;
504
+ if (oldHyperlinkId !== 0) {
505
+ const oldStr = oldHyperlinkPool.get(oldHyperlinkId);
506
+ const newHyperlinkId = hyperlinkPool.intern(oldStr);
507
+ // Repack word1 with new hyperlinkId, preserving styleId and width
508
+ const styleId = word1 >>> STYLE_SHIFT;
509
+ const width = word1 & WIDTH_MASK;
510
+ cells[ci + 1] = packWord1(styleId, newHyperlinkId, width);
511
+ }
512
+ }
513
+ screen.charPool = charPool;
514
+ screen.hyperlinkPool = hyperlinkPool;
515
+ }
516
+ /**
517
+ * Get a Cell view at the given position. Returns a new object each call -
518
+ * this is intentional as cells are stored packed, not as objects.
519
+ * @param screen - the screen to read.
520
+ * @param x - the cell column.
521
+ * @param y - the cell row.
522
+ * @returns the cell view, or undefined when the position is out of bounds.
523
+ */
524
+ export function cellAt(screen, x, y) {
525
+ if (x < 0 || y < 0 || x >= screen.width || y >= screen.height)
526
+ return undefined;
527
+ return cellAtIndex(screen, y * screen.width + x);
528
+ }
529
+ /**
530
+ * Get a Cell view by pre-computed array index. Skips bounds checks and
531
+ * index computation — caller must ensure index is valid.
532
+ * @param screen - the screen to read.
533
+ * @param index - the linear cell index (row * width + column).
534
+ * @returns the cell view.
535
+ */
536
+ export function cellAtIndex(screen, index) {
537
+ const ci = index << 1;
538
+ const word1 = screen.cells[ci + 1];
539
+ const hid = (word1 >>> HYPERLINK_SHIFT) & HYPERLINK_MASK;
540
+ return {
541
+ // Unwritten cells have charIndex=0 (EMPTY_CHAR_INDEX); charPool.get(0) returns ' '
542
+ char: screen.charPool.get(screen.cells[ci]),
543
+ styleId: word1 >>> STYLE_SHIFT,
544
+ width: word1 & WIDTH_MASK,
545
+ hyperlink: hid === 0 ? undefined : screen.hyperlinkPool.get(hid),
546
+ };
547
+ }
548
+ /**
549
+ * Get a Cell at the given index, or undefined if it has no visible content.
550
+ * Returns undefined for spacer cells (charId 1), empty unstyled spaces, and
551
+ * fg-only styled spaces that match lastRenderedStyleId (cursor-forward
552
+ * produces an identical visual result, avoiding a Cell allocation).
553
+ *
554
+ * @param cells - the packed cell words to read (2 Int32s per cell).
555
+ * @param charPool - the character pool to resolve char IDs.
556
+ * @param hyperlinkPool - the hyperlink pool to resolve hyperlink IDs.
557
+ * @param index - the linear cell index (row * width + column).
558
+ * @param lastRenderedStyleId - styleId of the last rendered cell on this
559
+ * line, or -1 if none yet.
560
+ * @returns the cell view, or undefined when the cell has no visible content.
561
+ */
562
+ export function visibleCellAtIndex(cells, charPool, hyperlinkPool, index, lastRenderedStyleId) {
563
+ const ci = index << 1;
564
+ const charId = cells[ci];
565
+ if (charId === 1)
566
+ return undefined; // spacer
567
+ const word1 = cells[ci + 1];
568
+ // For spaces: 0x3fffc masks bits 2-17 (hyperlinkId + styleId visibility
569
+ // bit). If zero, the space has no hyperlink and at most a fg-only style.
570
+ // Then word1 >>> STYLE_SHIFT is the foreground style — skip if it's zero
571
+ // (truly invisible) or matches the last rendered style on this line.
572
+ if (charId === 0 && (word1 & 0x3fffc) === 0) {
573
+ const fgStyle = word1 >>> STYLE_SHIFT;
574
+ if (fgStyle === 0 || fgStyle === lastRenderedStyleId)
575
+ return undefined;
576
+ }
577
+ const hid = (word1 >>> HYPERLINK_SHIFT) & HYPERLINK_MASK;
578
+ return {
579
+ char: charPool.get(charId),
580
+ styleId: word1 >>> STYLE_SHIFT,
581
+ width: word1 & WIDTH_MASK,
582
+ hyperlink: hid === 0 ? undefined : hyperlinkPool.get(hid),
583
+ };
584
+ }
585
+ /**
586
+ * Write cell data into an existing Cell object to avoid allocation.
587
+ * Caller must ensure index is valid.
588
+ */
589
+ function cellAtCI(screen, ci, out) {
590
+ const w1 = ci | 1;
591
+ const word1 = screen.cells[w1];
592
+ out.char = screen.charPool.get(screen.cells[ci]);
593
+ out.styleId = word1 >>> STYLE_SHIFT;
594
+ out.width = word1 & WIDTH_MASK;
595
+ const hid = (word1 >>> HYPERLINK_SHIFT) & HYPERLINK_MASK;
596
+ out.hyperlink = hid === 0 ? undefined : screen.hyperlinkPool.get(hid);
597
+ }
598
+ /**
599
+ * Return the character at a position without building a Cell view.
600
+ * @param screen - the screen to read.
601
+ * @param x - the cell column.
602
+ * @param y - the cell row.
603
+ * @returns the character string, or undefined when out of bounds.
604
+ */
605
+ export function charInCellAt(screen, x, y) {
606
+ if (x < 0 || y < 0 || x >= screen.width || y >= screen.height)
607
+ return undefined;
608
+ const ci = (y * screen.width + x) << 1;
609
+ return screen.charPool.get(screen.cells[ci]);
610
+ }
611
+ /**
612
+ * Set a cell, optionally creating a spacer for wide characters.
613
+ *
614
+ * Wide characters (CJK, emoji) occupy 2 cells in the buffer:
615
+ * 1. First cell: Contains the actual character with width = Wide
616
+ * 2. Second cell: Spacer cell with width = SpacerTail (empty, not rendered)
617
+ *
618
+ * If the cell has width = Wide, this function automatically creates the
619
+ * corresponding SpacerTail in the next column. This two-cell model keeps
620
+ * the buffer aligned to visual columns, making cursor positioning
621
+ * straightforward.
622
+ *
623
+ * TODO: When soft-wrapping is implemented, SpacerHead cells will be explicitly
624
+ * placed by the wrapping logic at line-end positions where wide characters
625
+ * wrap to the next line. This function doesn't need to handle SpacerHead
626
+ * automatically - it will be set directly by the wrapping code.
627
+ * @param screen - the screen to write into.
628
+ * @param x - the cell column.
629
+ * @param y - the cell row.
630
+ * @param cell - the cell data to write.
631
+ */
632
+ export function setCellAt(screen, x, y, cell) {
633
+ if (x < 0 || y < 0 || x >= screen.width || y >= screen.height)
634
+ return;
635
+ const ci = (y * screen.width + x) << 1;
636
+ const cells = screen.cells;
637
+ // When a Wide char is overwritten by a Narrow char, its SpacerTail remains
638
+ // as a ghost cell that the diff/render pipeline skips, causing stale content
639
+ // to leak through from previous frames.
640
+ const prevWidth = cells[ci + 1] & WIDTH_MASK;
641
+ if (prevWidth === 1 /* CellWidth.Wide */ && cell.width !== 1 /* CellWidth.Wide */) {
642
+ const spacerX = x + 1;
643
+ if (spacerX < screen.width) {
644
+ const spacerCI = ci + 2;
645
+ if ((cells[spacerCI + 1] & WIDTH_MASK) === 2 /* CellWidth.SpacerTail */) {
646
+ cells[spacerCI] = EMPTY_CHAR_INDEX;
647
+ cells[spacerCI + 1] = packWord1(screen.emptyStyleId, 0, 0 /* CellWidth.Narrow */);
648
+ }
649
+ }
650
+ }
651
+ // Track cleared Wide position for damage expansion below
652
+ let clearedWideX = -1;
653
+ if (prevWidth === 2 /* CellWidth.SpacerTail */ &&
654
+ cell.width !== 2 /* CellWidth.SpacerTail */) {
655
+ // Overwriting a SpacerTail: clear the orphaned Wide char at (x-1).
656
+ // Keeping the wide character with Narrow width would cause the terminal
657
+ // to still render it with width 2, desyncing the cursor model.
658
+ if (x > 0) {
659
+ const wideCI = ci - 2;
660
+ if ((cells[wideCI + 1] & WIDTH_MASK) === 1 /* CellWidth.Wide */) {
661
+ cells[wideCI] = EMPTY_CHAR_INDEX;
662
+ cells[wideCI + 1] = packWord1(screen.emptyStyleId, 0, 0 /* CellWidth.Narrow */);
663
+ clearedWideX = x - 1;
664
+ }
665
+ }
666
+ }
667
+ // Pack cell data into cells array
668
+ cells[ci] = internCharString(screen, cell.char);
669
+ cells[ci + 1] = packWord1(cell.styleId, internHyperlink(screen, cell.hyperlink), cell.width);
670
+ // Track damage - expand bounds in place instead of allocating new objects
671
+ // Include the main cell position and any cleared orphan cells
672
+ const minX = clearedWideX >= 0 ? Math.min(x, clearedWideX) : x;
673
+ const damage = screen.damage;
674
+ if (damage) {
675
+ const right = damage.x + damage.width;
676
+ const bottom = damage.y + damage.height;
677
+ if (minX < damage.x) {
678
+ damage.width += damage.x - minX;
679
+ damage.x = minX;
680
+ }
681
+ else if (x >= right) {
682
+ damage.width = x - damage.x + 1;
683
+ }
684
+ if (y < damage.y) {
685
+ damage.height += damage.y - y;
686
+ damage.y = y;
687
+ }
688
+ else if (y >= bottom) {
689
+ damage.height = y - damage.y + 1;
690
+ }
691
+ }
692
+ else {
693
+ screen.damage = { x: minX, y, width: x - minX + 1, height: 1 };
694
+ }
695
+ // If this is a wide character, create a spacer in the next column
696
+ if (cell.width === 1 /* CellWidth.Wide */) {
697
+ const spacerX = x + 1;
698
+ if (spacerX < screen.width) {
699
+ const spacerCI = ci + 2;
700
+ // If the cell we're overwriting with our SpacerTail is itself Wide,
701
+ // clear ITS SpacerTail at x+2 too. Otherwise the orphan SpacerTail
702
+ // makes diffEach report it as `added` and log-update's skip-spacer
703
+ // rule prevents clearing whatever prev content was at that column.
704
+ // Scenario: [a, 💻, spacer] → [本, spacer, ORPHAN spacer] when
705
+ // yoga squishes a💻 to height 0 and 本 renders at the same y.
706
+ if ((cells[spacerCI + 1] & WIDTH_MASK) === 1 /* CellWidth.Wide */) {
707
+ const orphanCI = spacerCI + 2;
708
+ if (spacerX + 1 < screen.width &&
709
+ (cells[orphanCI + 1] & WIDTH_MASK) === 2 /* CellWidth.SpacerTail */) {
710
+ cells[orphanCI] = EMPTY_CHAR_INDEX;
711
+ cells[orphanCI + 1] = packWord1(screen.emptyStyleId, 0, 0 /* CellWidth.Narrow */);
712
+ }
713
+ }
714
+ cells[spacerCI] = SPACER_CHAR_INDEX;
715
+ cells[spacerCI + 1] = packWord1(screen.emptyStyleId, 0, 2 /* CellWidth.SpacerTail */);
716
+ // Expand damage to include SpacerTail so diff() scans it
717
+ const d = screen.damage;
718
+ if (d && spacerX >= d.x + d.width) {
719
+ d.width = spacerX - d.x + 1;
720
+ }
721
+ }
722
+ }
723
+ }
724
+ /**
725
+ * Replace the styleId of a cell in-place without disturbing char, width,
726
+ * or hyperlink. Preserves empty cells as-is (char stays ' '). Tracks damage
727
+ * for the cell so diffEach picks up the change.
728
+ * @param screen - the screen to update.
729
+ * @param x - the cell column.
730
+ * @param y - the cell row.
731
+ * @param styleId - the new style ID.
732
+ */
733
+ export function setCellStyleId(screen, x, y, styleId) {
734
+ if (x < 0 || y < 0 || x >= screen.width || y >= screen.height)
735
+ return;
736
+ const ci = (y * screen.width + x) << 1;
737
+ const cells = screen.cells;
738
+ const word1 = cells[ci + 1];
739
+ const width = word1 & WIDTH_MASK;
740
+ // Skip spacer cells — inverse on the head cell visually covers both columns
741
+ if (width === 2 /* CellWidth.SpacerTail */ || width === 3 /* CellWidth.SpacerHead */)
742
+ return;
743
+ const hid = (word1 >>> HYPERLINK_SHIFT) & HYPERLINK_MASK;
744
+ cells[ci + 1] = packWord1(styleId, hid, width);
745
+ // Expand damage so diffEach scans this cell
746
+ const d = screen.damage;
747
+ if (d) {
748
+ screen.damage = unionRect(d, { x, y, width: 1, height: 1 });
749
+ }
750
+ else {
751
+ screen.damage = { x, y, width: 1, height: 1 };
752
+ }
753
+ }
754
+ /**
755
+ * Intern a character string via the screen's shared CharPool.
756
+ * Supports grapheme clusters like family emoji.
757
+ */
758
+ function internCharString(screen, char) {
759
+ return screen.charPool.intern(char);
760
+ }
761
+ /**
762
+ * Bulk-copy a rectangular region from src to dst using TypedArray.set().
763
+ * Single cells.set() call per row (or one call for contiguous blocks).
764
+ * Damage is computed once for the whole region.
765
+ *
766
+ * Clamps negative regionX/regionY to 0 (matching clearRegion) — absolute-
767
+ * positioned overlays in tiny terminals can compute negative screen coords.
768
+ * maxX/maxY should already be clamped to both screen bounds by the caller.
769
+ * @param dst - the destination screen.
770
+ * @param src - the source screen.
771
+ * @param regionX - the source region's left column.
772
+ * @param regionY - the source region's top row.
773
+ * @param maxX - the exclusive right edge of the region.
774
+ * @param maxY - the exclusive bottom edge of the region.
775
+ */
776
+ export function blitRegion(dst, src, regionX, regionY, maxX, maxY) {
777
+ regionX = Math.max(0, regionX);
778
+ regionY = Math.max(0, regionY);
779
+ if (regionX >= maxX || regionY >= maxY)
780
+ return;
781
+ const rowLen = maxX - regionX;
782
+ const srcStride = src.width << 1;
783
+ const dstStride = dst.width << 1;
784
+ const rowBytes = rowLen << 1; // 2 Int32s per cell
785
+ const srcCells = src.cells;
786
+ const dstCells = dst.cells;
787
+ const srcNoSel = src.noSelect;
788
+ const dstNoSel = dst.noSelect;
789
+ // softWrap is per-row — copy the row range regardless of stride/width.
790
+ // Partial-width blits still carry the row's wrap provenance since the
791
+ // blitted content (a cached ink-text node) is what set the bit.
792
+ dst.softWrap.set(src.softWrap.subarray(regionY, maxY), regionY);
793
+ // Fast path: contiguous memory when copying full-width rows at same stride
794
+ if (regionX === 0 && maxX === src.width && src.width === dst.width) {
795
+ const srcStart = regionY * srcStride;
796
+ const totalBytes = (maxY - regionY) * srcStride;
797
+ dstCells.set(srcCells.subarray(srcStart, srcStart + totalBytes), srcStart);
798
+ // noSelect is 1 byte/cell vs cells' 8 — same region, different scale
799
+ const nsStart = regionY * src.width;
800
+ const nsLen = (maxY - regionY) * src.width;
801
+ dstNoSel.set(srcNoSel.subarray(nsStart, nsStart + nsLen), nsStart);
802
+ }
803
+ else {
804
+ // Per-row copy for partial-width or mismatched-stride regions
805
+ let srcRowCI = regionY * srcStride + (regionX << 1);
806
+ let dstRowCI = regionY * dstStride + (regionX << 1);
807
+ let srcRowNS = regionY * src.width + regionX;
808
+ let dstRowNS = regionY * dst.width + regionX;
809
+ for (let y = regionY; y < maxY; y++) {
810
+ dstCells.set(srcCells.subarray(srcRowCI, srcRowCI + rowBytes), dstRowCI);
811
+ dstNoSel.set(srcNoSel.subarray(srcRowNS, srcRowNS + rowLen), dstRowNS);
812
+ srcRowCI += srcStride;
813
+ dstRowCI += dstStride;
814
+ srcRowNS += src.width;
815
+ dstRowNS += dst.width;
816
+ }
817
+ }
818
+ // Compute damage once for the whole region
819
+ const regionRect = {
820
+ x: regionX,
821
+ y: regionY,
822
+ width: rowLen,
823
+ height: maxY - regionY,
824
+ };
825
+ if (dst.damage) {
826
+ dst.damage = unionRect(dst.damage, regionRect);
827
+ }
828
+ else {
829
+ dst.damage = regionRect;
830
+ }
831
+ // Handle wide char at right edge: spacer might be outside blit region
832
+ // but still within dst bounds. Per-row check only at the boundary column.
833
+ if (maxX < dst.width) {
834
+ let srcLastCI = (regionY * src.width + (maxX - 1)) << 1;
835
+ let dstSpacerCI = (regionY * dst.width + maxX) << 1;
836
+ let wroteSpacerOutsideRegion = false;
837
+ for (let y = regionY; y < maxY; y++) {
838
+ if ((srcCells[srcLastCI + 1] & WIDTH_MASK) === 1 /* CellWidth.Wide */) {
839
+ dstCells[dstSpacerCI] = SPACER_CHAR_INDEX;
840
+ dstCells[dstSpacerCI + 1] = packWord1(dst.emptyStyleId, 0, 2 /* CellWidth.SpacerTail */);
841
+ wroteSpacerOutsideRegion = true;
842
+ }
843
+ srcLastCI += srcStride;
844
+ dstSpacerCI += dstStride;
845
+ }
846
+ // Expand damage to include SpacerTail column if we wrote any
847
+ if (wroteSpacerOutsideRegion && dst.damage) {
848
+ const rightEdge = dst.damage.x + dst.damage.width;
849
+ if (rightEdge === maxX) {
850
+ dst.damage = { ...dst.damage, width: dst.damage.width + 1 };
851
+ }
852
+ }
853
+ }
854
+ }
855
+ /**
856
+ * Bulk-clear a rectangular region of the screen.
857
+ * Uses BigInt64Array.fill() for fast row clears.
858
+ * Handles wide character boundary cleanup at region edges.
859
+ * @param screen - the screen to clear.
860
+ * @param regionX - the region's left column.
861
+ * @param regionY - the region's top row.
862
+ * @param regionWidth - the region width in cells.
863
+ * @param regionHeight - the region height in rows.
864
+ */
865
+ export function clearRegion(screen, regionX, regionY, regionWidth, regionHeight) {
866
+ const startX = Math.max(0, regionX);
867
+ const startY = Math.max(0, regionY);
868
+ const maxX = Math.min(regionX + regionWidth, screen.width);
869
+ const maxY = Math.min(regionY + regionHeight, screen.height);
870
+ if (startX >= maxX || startY >= maxY)
871
+ return;
872
+ const cells = screen.cells;
873
+ const cells64 = screen.cells64;
874
+ const screenWidth = screen.width;
875
+ const rowBase = startY * screenWidth;
876
+ let damageMinX = startX;
877
+ let damageMaxX = maxX;
878
+ // EMPTY_CELL_VALUE (0n) matches the zero-initialized state:
879
+ // word0=EMPTY_CHAR_INDEX(0), word1=packWord1(0,0,0)=0
880
+ if (startX === 0 && maxX === screenWidth) {
881
+ // Full-width: single fill, no boundary checks needed
882
+ cells64.fill(EMPTY_CELL_VALUE, rowBase, rowBase + (maxY - startY) * screenWidth);
883
+ }
884
+ else {
885
+ // Partial-width: single loop handles boundary cleanup and fill per row.
886
+ const stride = screenWidth << 1; // 2 Int32s per cell
887
+ const rowLen = maxX - startX;
888
+ const checkLeft = startX > 0;
889
+ const checkRight = maxX < screenWidth;
890
+ let leftEdge = (rowBase + startX) << 1;
891
+ let rightEdge = (rowBase + maxX - 1) << 1;
892
+ let fillStart = rowBase + startX;
893
+ for (let y = startY; y < maxY; y++) {
894
+ // Left boundary: if cell at startX is a SpacerTail, the Wide char
895
+ // at startX-1 (outside the region) will be orphaned. Clear it.
896
+ if (checkLeft) {
897
+ // leftEdge points to word0 of cell at startX; +1 is its word1
898
+ if ((cells[leftEdge + 1] & WIDTH_MASK) === 2 /* CellWidth.SpacerTail */) {
899
+ // word1 of cell at startX-1 is leftEdge-1; word0 is leftEdge-2
900
+ const prevW1 = leftEdge - 1;
901
+ if ((cells[prevW1] & WIDTH_MASK) === 1 /* CellWidth.Wide */) {
902
+ cells[prevW1 - 1] = EMPTY_CHAR_INDEX;
903
+ cells[prevW1] = packWord1(screen.emptyStyleId, 0, 0 /* CellWidth.Narrow */);
904
+ damageMinX = startX - 1;
905
+ }
906
+ }
907
+ }
908
+ // Right boundary: if cell at maxX-1 is Wide, its SpacerTail at maxX
909
+ // (outside the region) will be orphaned. Clear it.
910
+ if (checkRight) {
911
+ // rightEdge points to word0 of cell at maxX-1; +1 is its word1
912
+ if ((cells[rightEdge + 1] & WIDTH_MASK) === 1 /* CellWidth.Wide */) {
913
+ // word1 of cell at maxX is rightEdge+3 (+2 to next word0, +1 to word1)
914
+ const nextW1 = rightEdge + 3;
915
+ if ((cells[nextW1] & WIDTH_MASK) === 2 /* CellWidth.SpacerTail */) {
916
+ cells[nextW1 - 1] = EMPTY_CHAR_INDEX;
917
+ cells[nextW1] = packWord1(screen.emptyStyleId, 0, 0 /* CellWidth.Narrow */);
918
+ damageMaxX = maxX + 1;
919
+ }
920
+ }
921
+ }
922
+ cells64.fill(EMPTY_CELL_VALUE, fillStart, fillStart + rowLen);
923
+ leftEdge += stride;
924
+ rightEdge += stride;
925
+ fillStart += screenWidth;
926
+ }
927
+ }
928
+ // Update damage once for the whole region
929
+ const regionRect = {
930
+ x: damageMinX,
931
+ y: startY,
932
+ width: damageMaxX - damageMinX,
933
+ height: maxY - startY,
934
+ };
935
+ if (screen.damage) {
936
+ screen.damage = unionRect(screen.damage, regionRect);
937
+ }
938
+ else {
939
+ screen.damage = regionRect;
940
+ }
941
+ }
942
+ /**
943
+ * Shift full-width rows within [top, bottom] (inclusive, 0-indexed) by n.
944
+ * n > 0 shifts UP (simulating CSI n S); n < 0 shifts DOWN (CSI n T).
945
+ * Vacated rows are cleared. Does NOT update damage. Both cells and the
946
+ * noSelect bitmap are shifted so text-selection markers stay aligned when
947
+ * this is applied to next.screen during scroll fast path.
948
+ * @param screen - the screen to shift.
949
+ * @param top - the first row of the shifted range (inclusive).
950
+ * @param bottom - the last row of the shifted range (inclusive).
951
+ * @param n - the shift in rows; positive shifts up, negative shifts down.
952
+ */
953
+ export function shiftRows(screen, top, bottom, n) {
954
+ if (n === 0 || top < 0 || bottom >= screen.height || top > bottom)
955
+ return;
956
+ const w = screen.width;
957
+ const cells64 = screen.cells64;
958
+ const noSel = screen.noSelect;
959
+ const sw = screen.softWrap;
960
+ const absN = Math.abs(n);
961
+ if (absN > bottom - top) {
962
+ cells64.fill(EMPTY_CELL_VALUE, top * w, (bottom + 1) * w);
963
+ noSel.fill(0, top * w, (bottom + 1) * w);
964
+ sw.fill(0, top, bottom + 1);
965
+ return;
966
+ }
967
+ if (n > 0) {
968
+ // SU: row top+n..bottom → top..bottom-n; clear bottom-n+1..bottom
969
+ cells64.copyWithin(top * w, (top + n) * w, (bottom + 1) * w);
970
+ noSel.copyWithin(top * w, (top + n) * w, (bottom + 1) * w);
971
+ sw.copyWithin(top, top + n, bottom + 1);
972
+ cells64.fill(EMPTY_CELL_VALUE, (bottom - n + 1) * w, (bottom + 1) * w);
973
+ noSel.fill(0, (bottom - n + 1) * w, (bottom + 1) * w);
974
+ sw.fill(0, bottom - n + 1, bottom + 1);
975
+ }
976
+ else {
977
+ // SD: row top..bottom+n → top-n..bottom; clear top..top-n-1
978
+ cells64.copyWithin((top - n) * w, top * w, (bottom + n + 1) * w);
979
+ noSel.copyWithin((top - n) * w, top * w, (bottom + n + 1) * w);
980
+ sw.copyWithin(top - n, top, bottom + n + 1);
981
+ cells64.fill(EMPTY_CELL_VALUE, top * w, (top - n) * w);
982
+ noSel.fill(0, top * w, (top - n) * w);
983
+ sw.fill(0, top, top - n);
984
+ }
985
+ }
986
+ // Matches OSC 8 ; ; URI BEL
987
+ const OSC8_REGEX = new RegExp(`^${ESC}\\]8${SEP}${SEP}([^${BEL}]*)${BEL}$`);
988
+ /**
989
+ * The OSC 8 hyperlink escape prefix (`ESC ] 8 ;`) used for cheap pre-checks
990
+ * before running the full OSC 8 regex.
991
+ */
992
+ export const OSC8_PREFIX = `${ESC}]8${SEP}`;
993
+ /**
994
+ * Extract the hyperlink URI from the first OSC 8 style in a style stack.
995
+ * @param styles - the ANSI code stack to scan.
996
+ * @returns the hyperlink URI, or null when no OSC 8 style is present.
997
+ */
998
+ export function extractHyperlinkFromStyles(styles) {
999
+ for (const style of styles) {
1000
+ const code = style.code;
1001
+ if (code.length < 5 || !code.startsWith(OSC8_PREFIX))
1002
+ continue;
1003
+ const match = code.match(OSC8_REGEX);
1004
+ if (match) {
1005
+ return match[1] || null;
1006
+ }
1007
+ }
1008
+ return null;
1009
+ }
1010
+ /**
1011
+ * Return the style stack with all OSC 8 hyperlink styles removed.
1012
+ * @param styles - the ANSI code stack to filter.
1013
+ * @returns a new stack without hyperlink styles.
1014
+ */
1015
+ export function filterOutHyperlinkStyles(styles) {
1016
+ return styles.filter(style => !style.code.startsWith(OSC8_PREFIX) || !OSC8_REGEX.test(style.code));
1017
+ }
1018
+ // ---
1019
+ /**
1020
+ * Returns an array of all changes between two screens. Used by tests.
1021
+ * Production code should use diffEach() to avoid allocations.
1022
+ * @param prev - the previous screen state.
1023
+ * @param next - the next screen state.
1024
+ * @returns one entry per changed cell: the point plus the removed and added
1025
+ * cell views (undefined when that side has no cell).
1026
+ */
1027
+ export function diff(prev, next) {
1028
+ const output = [];
1029
+ diffEach(prev, next, (x, y, removed, added) => {
1030
+ // Copy cells since diffEach reuses the objects
1031
+ output.push([
1032
+ { x, y },
1033
+ removed ? { ...removed } : undefined,
1034
+ added ? { ...added } : undefined,
1035
+ ]);
1036
+ });
1037
+ return output;
1038
+ }
1039
+ /**
1040
+ * Like diff(), but calls a callback for each change instead of building an array.
1041
+ * Reuses two Cell objects to avoid per-change allocations. The callback must not
1042
+ * retain references to the Cell objects — their contents are overwritten each call.
1043
+ *
1044
+ * Returns true if the callback ever returned true (early exit signal).
1045
+ * @param prev - the previous screen state.
1046
+ * @param next - the next screen state.
1047
+ * @param cb - the per-change callback; returning true stops iteration.
1048
+ * @returns true when the callback requested an early exit.
1049
+ */
1050
+ export function diffEach(prev, next, cb) {
1051
+ const prevWidth = prev.width;
1052
+ const nextWidth = next.width;
1053
+ const prevHeight = prev.height;
1054
+ const nextHeight = next.height;
1055
+ let region;
1056
+ if (prevWidth === 0 && prevHeight === 0) {
1057
+ region = { x: 0, y: 0, width: nextWidth, height: nextHeight };
1058
+ }
1059
+ else if (next.damage) {
1060
+ region = next.damage;
1061
+ if (prev.damage) {
1062
+ region = unionRect(region, prev.damage);
1063
+ }
1064
+ }
1065
+ else if (prev.damage) {
1066
+ region = prev.damage;
1067
+ }
1068
+ else {
1069
+ region = { x: 0, y: 0, width: 0, height: 0 };
1070
+ }
1071
+ if (prevHeight > nextHeight) {
1072
+ region = unionRect(region, {
1073
+ x: 0,
1074
+ y: nextHeight,
1075
+ width: prevWidth,
1076
+ height: prevHeight - nextHeight,
1077
+ });
1078
+ }
1079
+ if (prevWidth > nextWidth) {
1080
+ region = unionRect(region, {
1081
+ x: nextWidth,
1082
+ y: 0,
1083
+ width: prevWidth - nextWidth,
1084
+ height: prevHeight,
1085
+ });
1086
+ }
1087
+ const maxHeight = Math.max(prevHeight, nextHeight);
1088
+ const maxWidth = Math.max(prevWidth, nextWidth);
1089
+ const endY = Math.min(region.y + region.height, maxHeight);
1090
+ const endX = Math.min(region.x + region.width, maxWidth);
1091
+ if (prevWidth === nextWidth) {
1092
+ return diffSameWidth(prev, next, region.x, endX, region.y, endY, cb);
1093
+ }
1094
+ return diffDifferentWidth(prev, next, region.x, endX, region.y, endY, cb);
1095
+ }
1096
+ /**
1097
+ * Scan for the next cell that differs between two Int32Arrays.
1098
+ * Returns the number of matching cells before the first difference,
1099
+ * or `count` if all cells match. Tiny and pure for JIT inlining.
1100
+ */
1101
+ function findNextDiff(a, b, w0, count) {
1102
+ for (let i = 0; i < count; i++, w0 += 2) {
1103
+ const w1 = w0 | 1;
1104
+ if (a[w0] !== b[w0] || a[w1] !== b[w1])
1105
+ return i;
1106
+ }
1107
+ return count;
1108
+ }
1109
+ /**
1110
+ * Diff one row where both screens are in bounds.
1111
+ * Scans for differences with findNextDiff, unpacks and calls cb for each.
1112
+ */
1113
+ function diffRowBoth(prevCells, nextCells, prev, next, ci, y, startX, endX, prevCell, nextCell, cb) {
1114
+ let x = startX;
1115
+ while (x < endX) {
1116
+ const skip = findNextDiff(prevCells, nextCells, ci, endX - x);
1117
+ x += skip;
1118
+ ci += skip << 1;
1119
+ if (x >= endX)
1120
+ break;
1121
+ cellAtCI(prev, ci, prevCell);
1122
+ cellAtCI(next, ci, nextCell);
1123
+ if (cb(x, y, prevCell, nextCell))
1124
+ return true;
1125
+ x++;
1126
+ ci += 2;
1127
+ }
1128
+ return false;
1129
+ }
1130
+ /**
1131
+ * Emit removals for a row that only exists in prev (height shrank).
1132
+ * Cannot skip empty cells — the terminal still has content from the
1133
+ * previous frame that needs to be cleared.
1134
+ */
1135
+ function diffRowRemoved(prev, ci, y, startX, endX, prevCell, cb) {
1136
+ for (let x = startX; x < endX; x++, ci += 2) {
1137
+ cellAtCI(prev, ci, prevCell);
1138
+ if (cb(x, y, prevCell, undefined))
1139
+ return true;
1140
+ }
1141
+ return false;
1142
+ }
1143
+ /**
1144
+ * Emit additions for a row that only exists in next (height grew).
1145
+ * Skips empty/unwritten cells.
1146
+ */
1147
+ function diffRowAdded(nextCells, next, ci, y, startX, endX, nextCell, cb) {
1148
+ for (let x = startX; x < endX; x++, ci += 2) {
1149
+ if (nextCells[ci] === 0 && nextCells[ci | 1] === 0)
1150
+ continue;
1151
+ cellAtCI(next, ci, nextCell);
1152
+ if (cb(x, y, undefined, nextCell))
1153
+ return true;
1154
+ }
1155
+ return false;
1156
+ }
1157
+ /**
1158
+ * Diff two screens with identical width.
1159
+ * Dispatches each row to a small, JIT-friendly function.
1160
+ */
1161
+ function diffSameWidth(prev, next, startX, endX, startY, endY, cb) {
1162
+ const prevCells = prev.cells;
1163
+ const nextCells = next.cells;
1164
+ const width = prev.width;
1165
+ const prevHeight = prev.height;
1166
+ const nextHeight = next.height;
1167
+ const stride = width << 1;
1168
+ const prevCell = {
1169
+ char: ' ',
1170
+ styleId: 0,
1171
+ width: 0 /* CellWidth.Narrow */,
1172
+ hyperlink: undefined,
1173
+ };
1174
+ const nextCell = {
1175
+ char: ' ',
1176
+ styleId: 0,
1177
+ width: 0 /* CellWidth.Narrow */,
1178
+ hyperlink: undefined,
1179
+ };
1180
+ const rowEndX = Math.min(endX, width);
1181
+ let rowCI = (startY * width + startX) << 1;
1182
+ for (let y = startY; y < endY; y++) {
1183
+ const prevIn = y < prevHeight;
1184
+ const nextIn = y < nextHeight;
1185
+ if (prevIn && nextIn) {
1186
+ if (diffRowBoth(prevCells, nextCells, prev, next, rowCI, y, startX, rowEndX, prevCell, nextCell, cb))
1187
+ return true;
1188
+ }
1189
+ else if (prevIn) {
1190
+ if (diffRowRemoved(prev, rowCI, y, startX, rowEndX, prevCell, cb))
1191
+ return true;
1192
+ }
1193
+ else if (nextIn) {
1194
+ if (diffRowAdded(nextCells, next, rowCI, y, startX, rowEndX, nextCell, cb))
1195
+ return true;
1196
+ }
1197
+ rowCI += stride;
1198
+ }
1199
+ return false;
1200
+ }
1201
+ /**
1202
+ * Fallback: diff two screens with different widths (resize).
1203
+ * Separate indices for prev and next cells arrays.
1204
+ */
1205
+ function diffDifferentWidth(prev, next, startX, endX, startY, endY, cb) {
1206
+ const prevWidth = prev.width;
1207
+ const nextWidth = next.width;
1208
+ const prevCells = prev.cells;
1209
+ const nextCells = next.cells;
1210
+ const prevCell = {
1211
+ char: ' ',
1212
+ styleId: 0,
1213
+ width: 0 /* CellWidth.Narrow */,
1214
+ hyperlink: undefined,
1215
+ };
1216
+ const nextCell = {
1217
+ char: ' ',
1218
+ styleId: 0,
1219
+ width: 0 /* CellWidth.Narrow */,
1220
+ hyperlink: undefined,
1221
+ };
1222
+ const prevStride = prevWidth << 1;
1223
+ const nextStride = nextWidth << 1;
1224
+ let prevRowCI = (startY * prevWidth + startX) << 1;
1225
+ let nextRowCI = (startY * nextWidth + startX) << 1;
1226
+ for (let y = startY; y < endY; y++) {
1227
+ const prevIn = y < prev.height;
1228
+ const nextIn = y < next.height;
1229
+ const prevEndX = prevIn ? Math.min(endX, prevWidth) : startX;
1230
+ const nextEndX = nextIn ? Math.min(endX, nextWidth) : startX;
1231
+ const bothEndX = Math.min(prevEndX, nextEndX);
1232
+ let prevCI = prevRowCI;
1233
+ let nextCI = nextRowCI;
1234
+ for (let x = startX; x < bothEndX; x++) {
1235
+ if (prevCells[prevCI] === nextCells[nextCI] &&
1236
+ prevCells[prevCI + 1] === nextCells[nextCI + 1]) {
1237
+ prevCI += 2;
1238
+ nextCI += 2;
1239
+ continue;
1240
+ }
1241
+ cellAtCI(prev, prevCI, prevCell);
1242
+ cellAtCI(next, nextCI, nextCell);
1243
+ prevCI += 2;
1244
+ nextCI += 2;
1245
+ if (cb(x, y, prevCell, nextCell))
1246
+ return true;
1247
+ }
1248
+ if (prevEndX > bothEndX) {
1249
+ prevCI = prevRowCI + ((bothEndX - startX) << 1);
1250
+ for (let x = bothEndX; x < prevEndX; x++) {
1251
+ cellAtCI(prev, prevCI, prevCell);
1252
+ prevCI += 2;
1253
+ if (cb(x, y, prevCell, undefined))
1254
+ return true;
1255
+ }
1256
+ }
1257
+ if (nextEndX > bothEndX) {
1258
+ nextCI = nextRowCI + ((bothEndX - startX) << 1);
1259
+ for (let x = bothEndX; x < nextEndX; x++) {
1260
+ if (nextCells[nextCI] === 0 && nextCells[nextCI | 1] === 0) {
1261
+ nextCI += 2;
1262
+ continue;
1263
+ }
1264
+ cellAtCI(next, nextCI, nextCell);
1265
+ nextCI += 2;
1266
+ if (cb(x, y, undefined, nextCell))
1267
+ return true;
1268
+ }
1269
+ }
1270
+ prevRowCI += prevStride;
1271
+ nextRowCI += nextStride;
1272
+ }
1273
+ return false;
1274
+ }
1275
+ /**
1276
+ * Mark a rectangular region as noSelect (exclude from text selection).
1277
+ * Clamps to screen bounds. Called from output.ts when a <NoSelect> box
1278
+ * renders. No damage tracking — noSelect doesn't affect terminal output,
1279
+ * only getSelectedText/applySelectionOverlay which read it directly.
1280
+ * @param screen - the screen to update.
1281
+ * @param x - the region's left column.
1282
+ * @param y - the region's top row.
1283
+ * @param width - the region width in cells.
1284
+ * @param height - the region height in rows.
1285
+ */
1286
+ export function markNoSelectRegion(screen, x, y, width, height) {
1287
+ const maxX = Math.min(x + width, screen.width);
1288
+ const maxY = Math.min(y + height, screen.height);
1289
+ const noSel = screen.noSelect;
1290
+ const stride = screen.width;
1291
+ for (let row = Math.max(0, y); row < maxY; row++) {
1292
+ const rowStart = row * stride;
1293
+ noSel.fill(1, rowStart + Math.max(0, x), rowStart + maxX);
1294
+ }
1295
+ }