@xincli/opentui-core 0.4.6

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 (476) hide show
  1. package/README.md +63 -0
  2. package/dev/keypress-debug-renderer.ts +148 -0
  3. package/dev/keypress-debug.ts +43 -0
  4. package/dev/print-env-vars.ts +32 -0
  5. package/dev/test-tmux-graphics-334.sh +68 -0
  6. package/dev/test-tmux-theme-queries.sh +151 -0
  7. package/dev/thai-debug-test.ts +68 -0
  8. package/dev/theme-mode.ts +290 -0
  9. package/docs/development.md +144 -0
  10. package/package.json +102 -0
  11. package/prebuilt/aarch64-android/libopentui.so +0 -0
  12. package/scripts/build-android.ts +226 -0
  13. package/scripts/build-native-termux.sh +525 -0
  14. package/scripts/build.ts +530 -0
  15. package/scripts/dist-test.ts +312 -0
  16. package/scripts/package-prebuilt.ts +110 -0
  17. package/scripts/publish.ts +60 -0
  18. package/scripts/test-node-hook.mjs +16 -0
  19. package/scripts/test-node.ts +241 -0
  20. package/scripts/vendor-deps.sh +73 -0
  21. package/src/NativeSpanFeed.ts +375 -0
  22. package/src/Renderable.ts +1878 -0
  23. package/src/__snapshots__/buffer.test.ts.snap +28 -0
  24. package/src/animation/Timeline.test.ts +2709 -0
  25. package/src/animation/Timeline.ts +598 -0
  26. package/src/ansi.ts +22 -0
  27. package/src/audio.ts +479 -0
  28. package/src/benchmark/attenuation-benchmark.ts +81 -0
  29. package/src/benchmark/audio-playback-benchmark.ts +344 -0
  30. package/src/benchmark/box-draw-benchmark.ts +1042 -0
  31. package/src/benchmark/colormatrix-benchmark.ts +123 -0
  32. package/src/benchmark/gain-benchmark.ts +80 -0
  33. package/src/benchmark/latest-all-bench-run.json +707 -0
  34. package/src/benchmark/latest-async-bench-run.json +336 -0
  35. package/src/benchmark/latest-default-bench-run.json +657 -0
  36. package/src/benchmark/latest-large-bench-run.json +707 -0
  37. package/src/benchmark/latest-quick-bench-run.json +207 -0
  38. package/src/benchmark/layout-benchmark.ts +2547 -0
  39. package/src/benchmark/markdown-benchmark.ts +1796 -0
  40. package/src/benchmark/native-span-feed-async-benchmark.ts +355 -0
  41. package/src/benchmark/native-span-feed-benchmark.md +56 -0
  42. package/src/benchmark/native-span-feed-benchmark.ts +596 -0
  43. package/src/benchmark/native-span-feed-compare.ts +280 -0
  44. package/src/benchmark/render-traversal-benchmark.ts +928 -0
  45. package/src/benchmark/text-buffer-render-benchmark.ts +874 -0
  46. package/src/benchmark/text-table-benchmark.ts +948 -0
  47. package/src/buffer.test.ts +292 -0
  48. package/src/buffer.ts +564 -0
  49. package/src/console.test.ts +629 -0
  50. package/src/console.ts +1253 -0
  51. package/src/edit-buffer.test.ts +1852 -0
  52. package/src/edit-buffer.ts +417 -0
  53. package/src/editor-view.test.ts +1056 -0
  54. package/src/editor-view.ts +290 -0
  55. package/src/index.ts +25 -0
  56. package/src/lib/KeyHandler.integration.test.ts +292 -0
  57. package/src/lib/KeyHandler.stopPropagation.test.ts +289 -0
  58. package/src/lib/KeyHandler.test.ts +662 -0
  59. package/src/lib/KeyHandler.ts +222 -0
  60. package/src/lib/RGBA.test.ts +60 -0
  61. package/src/lib/RGBA.ts +343 -0
  62. package/src/lib/ascii.font.ts +330 -0
  63. package/src/lib/border.test.ts +83 -0
  64. package/src/lib/border.ts +170 -0
  65. package/src/lib/bunfs.test.ts +27 -0
  66. package/src/lib/bunfs.ts +18 -0
  67. package/src/lib/clipboard.test.ts +63 -0
  68. package/src/lib/clipboard.ts +41 -0
  69. package/src/lib/clock.ts +35 -0
  70. package/src/lib/data-paths.test.ts +133 -0
  71. package/src/lib/data-paths.ts +109 -0
  72. package/src/lib/debounce.ts +106 -0
  73. package/src/lib/detect-links.test.ts +98 -0
  74. package/src/lib/detect-links.ts +56 -0
  75. package/src/lib/env.test.ts +228 -0
  76. package/src/lib/env.ts +209 -0
  77. package/src/lib/extmarks-history.ts +51 -0
  78. package/src/lib/extmarks-multiwidth.test.ts +324 -0
  79. package/src/lib/extmarks.test.ts +3547 -0
  80. package/src/lib/extmarks.ts +861 -0
  81. package/src/lib/fonts/block.json +405 -0
  82. package/src/lib/fonts/grid.json +265 -0
  83. package/src/lib/fonts/huge.json +741 -0
  84. package/src/lib/fonts/pallet.json +314 -0
  85. package/src/lib/fonts/shade.json +591 -0
  86. package/src/lib/fonts/slick.json +321 -0
  87. package/src/lib/fonts/tiny.json +69 -0
  88. package/src/lib/hast-styled-text.ts +59 -0
  89. package/src/lib/index.ts +22 -0
  90. package/src/lib/keybinding.internal.test.ts +342 -0
  91. package/src/lib/keybinding.internal.ts +182 -0
  92. package/src/lib/objects-in-viewport.test.ts +789 -0
  93. package/src/lib/objects-in-viewport.ts +153 -0
  94. package/src/lib/output.capture.ts +58 -0
  95. package/src/lib/parse.keypress-kitty.protocol.test.ts +347 -0
  96. package/src/lib/parse.keypress-kitty.test.ts +712 -0
  97. package/src/lib/parse.keypress-kitty.ts +470 -0
  98. package/src/lib/parse.keypress.test.ts +2020 -0
  99. package/src/lib/parse.keypress.ts +480 -0
  100. package/src/lib/parse.mouse.test.ts +552 -0
  101. package/src/lib/parse.mouse.ts +232 -0
  102. package/src/lib/paste.ts +18 -0
  103. package/src/lib/queue.ts +65 -0
  104. package/src/lib/render-geometry.ts +36 -0
  105. package/src/lib/renderable.validations.test.ts +87 -0
  106. package/src/lib/renderable.validations.ts +83 -0
  107. package/src/lib/scroll-acceleration.ts +98 -0
  108. package/src/lib/selection.ts +261 -0
  109. package/src/lib/singleton.ts +34 -0
  110. package/src/lib/stdin-parser.test.ts +2439 -0
  111. package/src/lib/stdin-parser.ts +2007 -0
  112. package/src/lib/styled-text.ts +178 -0
  113. package/src/lib/terminal-capability-detection.test.ts +226 -0
  114. package/src/lib/terminal-capability-detection.ts +98 -0
  115. package/src/lib/terminal-palette.test.ts +954 -0
  116. package/src/lib/terminal-palette.ts +441 -0
  117. package/src/lib/tree-sitter/assets/README.md +119 -0
  118. package/src/lib/tree-sitter/assets/javascript/highlights.scm +205 -0
  119. package/src/lib/tree-sitter/assets/javascript/tree-sitter-javascript.wasm +0 -0
  120. package/src/lib/tree-sitter/assets/markdown/highlights.scm +150 -0
  121. package/src/lib/tree-sitter/assets/markdown/injections.scm +27 -0
  122. package/src/lib/tree-sitter/assets/markdown/tree-sitter-markdown.wasm +0 -0
  123. package/src/lib/tree-sitter/assets/markdown_inline/highlights.scm +115 -0
  124. package/src/lib/tree-sitter/assets/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  125. package/src/lib/tree-sitter/assets/typescript/highlights.scm +604 -0
  126. package/src/lib/tree-sitter/assets/typescript/tree-sitter-typescript.wasm +0 -0
  127. package/src/lib/tree-sitter/assets/update.ts +360 -0
  128. package/src/lib/tree-sitter/assets/zig/highlights.scm +284 -0
  129. package/src/lib/tree-sitter/assets/zig/tree-sitter-zig.wasm +0 -0
  130. package/src/lib/tree-sitter/assets.d.ts +9 -0
  131. package/src/lib/tree-sitter/cache.test.ts +291 -0
  132. package/src/lib/tree-sitter/client.test.ts +1434 -0
  133. package/src/lib/tree-sitter/client.ts +847 -0
  134. package/src/lib/tree-sitter/default-parsers.ts +133 -0
  135. package/src/lib/tree-sitter/download-utils.ts +148 -0
  136. package/src/lib/tree-sitter/index.ts +75 -0
  137. package/src/lib/tree-sitter/parser.worker.ts +1098 -0
  138. package/src/lib/tree-sitter/parsers-config.ts +81 -0
  139. package/src/lib/tree-sitter/resolve-ft.test.ts +55 -0
  140. package/src/lib/tree-sitter/resolve-ft.ts +189 -0
  141. package/src/lib/tree-sitter/types.ts +132 -0
  142. package/src/lib/tree-sitter/update-assets.ts +10 -0
  143. package/src/lib/tree-sitter-styled-text.test.ts +1253 -0
  144. package/src/lib/tree-sitter-styled-text.ts +314 -0
  145. package/src/lib/validate-dir-name.ts +55 -0
  146. package/src/lib/yoga.options.test.ts +628 -0
  147. package/src/lib/yoga.options.ts +346 -0
  148. package/src/native-event-worker-repro.test.ts +48 -0
  149. package/src/native-event-worker-repro.worker.ts +9 -0
  150. package/src/native-handle.test.ts +221 -0
  151. package/src/platform/ffi.test.ts +516 -0
  152. package/src/platform/ffi.ts +653 -0
  153. package/src/platform/runtime.test.ts +52 -0
  154. package/src/platform/runtime.ts +121 -0
  155. package/src/platform/test.ts +45 -0
  156. package/src/platform/worker-handler-cleanup.fixture.ts +12 -0
  157. package/src/platform/worker-onmessage-startup.fixture.ts +10 -0
  158. package/src/platform/worker-startup.fixture.ts +9 -0
  159. package/src/platform/worker.node-test.ts +153 -0
  160. package/src/platform/worker.ts +425 -0
  161. package/src/plugins/core-slot.ts +579 -0
  162. package/src/plugins/registry.ts +402 -0
  163. package/src/plugins/types.ts +46 -0
  164. package/src/post/effects.ts +948 -0
  165. package/src/post/filters.ts +512 -0
  166. package/src/post/matrices.ts +288 -0
  167. package/src/renderables/ASCIIFont.ts +219 -0
  168. package/src/renderables/Box.test.ts +474 -0
  169. package/src/renderables/Box.ts +353 -0
  170. package/src/renderables/Code.test.ts +2388 -0
  171. package/src/renderables/Code.ts +555 -0
  172. package/src/renderables/Diff.regression.test.ts +381 -0
  173. package/src/renderables/Diff.test.ts +3212 -0
  174. package/src/renderables/Diff.ts +1278 -0
  175. package/src/renderables/EditBufferRenderable.test.ts +288 -0
  176. package/src/renderables/EditBufferRenderable.ts +1152 -0
  177. package/src/renderables/FrameBuffer.ts +47 -0
  178. package/src/renderables/Input.test.ts +1310 -0
  179. package/src/renderables/Input.ts +273 -0
  180. package/src/renderables/LineNumberRenderable.ts +725 -0
  181. package/src/renderables/Markdown.ts +2144 -0
  182. package/src/renderables/ScrollBar.ts +423 -0
  183. package/src/renderables/ScrollBox.ts +903 -0
  184. package/src/renderables/Select.test.ts +1134 -0
  185. package/src/renderables/Select.ts +535 -0
  186. package/src/renderables/Slider.test.ts +456 -0
  187. package/src/renderables/Slider.ts +342 -0
  188. package/src/renderables/TabSelect.test.ts +238 -0
  189. package/src/renderables/TabSelect.ts +447 -0
  190. package/src/renderables/Text.selection-buffer.test.ts +118 -0
  191. package/src/renderables/Text.test.ts +2662 -0
  192. package/src/renderables/Text.ts +147 -0
  193. package/src/renderables/TextBufferRenderable.ts +504 -0
  194. package/src/renderables/TextNode.test.ts +1137 -0
  195. package/src/renderables/TextNode.ts +379 -0
  196. package/src/renderables/TextTable.test.ts +1411 -0
  197. package/src/renderables/TextTable.ts +1434 -0
  198. package/src/renderables/Textarea.ts +425 -0
  199. package/src/renderables/TimeToFirstDraw.ts +89 -0
  200. package/src/renderables/__snapshots__/Code.test.ts.snap +13 -0
  201. package/src/renderables/__snapshots__/Diff.test.ts.snap +785 -0
  202. package/src/renderables/__snapshots__/Text.test.ts.snap +421 -0
  203. package/src/renderables/__snapshots__/TextTable.test.ts.snap +215 -0
  204. package/src/renderables/__tests__/Code.test.ts +55 -0
  205. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +144 -0
  206. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +827 -0
  207. package/src/renderables/__tests__/LineNumberRenderable.test.ts +1906 -0
  208. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +85 -0
  209. package/src/renderables/__tests__/Markdown.code-colors.test.ts +288 -0
  210. package/src/renderables/__tests__/Markdown.test.ts +4175 -0
  211. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +87 -0
  212. package/src/renderables/__tests__/Textarea.buffer.test.ts +683 -0
  213. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +675 -0
  214. package/src/renderables/__tests__/Textarea.editing.test.ts +2120 -0
  215. package/src/renderables/__tests__/Textarea.error-handling.test.ts +35 -0
  216. package/src/renderables/__tests__/Textarea.events.test.ts +738 -0
  217. package/src/renderables/__tests__/Textarea.highlights.test.ts +590 -0
  218. package/src/renderables/__tests__/Textarea.keybinding.test.ts +3275 -0
  219. package/src/renderables/__tests__/Textarea.paste.test.ts +357 -0
  220. package/src/renderables/__tests__/Textarea.rendering.test.ts +1927 -0
  221. package/src/renderables/__tests__/Textarea.scroll.test.ts +733 -0
  222. package/src/renderables/__tests__/Textarea.selection.test.ts +1589 -0
  223. package/src/renderables/__tests__/Textarea.stress.test.ts +670 -0
  224. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +383 -0
  225. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +310 -0
  226. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +221 -0
  227. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +89 -0
  228. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +457 -0
  229. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +158 -0
  230. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +387 -0
  231. package/src/renderables/__tests__/markdown-parser.test.ts +241 -0
  232. package/src/renderables/__tests__/renderable-test-utils.ts +60 -0
  233. package/src/renderables/composition/README.md +8 -0
  234. package/src/renderables/composition/VRenderable.ts +32 -0
  235. package/src/renderables/composition/constructs.ts +127 -0
  236. package/src/renderables/composition/vnode.ts +277 -0
  237. package/src/renderables/index.ts +23 -0
  238. package/src/renderables/markdown-parser.ts +79 -0
  239. package/src/renderer-theme-mode.ts +199 -0
  240. package/src/renderer.ts +4975 -0
  241. package/src/runtime-plugin-support-configure.ts +63 -0
  242. package/src/runtime-plugin-support.ts +13 -0
  243. package/src/runtime-plugin.ts +626 -0
  244. package/src/specs/terminal-startup.md +64 -0
  245. package/src/syntax-style.test.ts +841 -0
  246. package/src/syntax-style.ts +274 -0
  247. package/src/testing/README.md +219 -0
  248. package/src/testing/bun-test-node.ts +880 -0
  249. package/src/testing/capture-spans.test.ts +216 -0
  250. package/src/testing/integration.test.ts +276 -0
  251. package/src/testing/manual-clock.ts +117 -0
  252. package/src/testing/mock-keys.test.ts +1378 -0
  253. package/src/testing/mock-keys.ts +457 -0
  254. package/src/testing/mock-mouse.test.ts +218 -0
  255. package/src/testing/mock-mouse.ts +247 -0
  256. package/src/testing/mock-tree-sitter-client.ts +81 -0
  257. package/src/testing/spy.ts +13 -0
  258. package/src/testing/terminal-capabilities.ts +47 -0
  259. package/src/testing/test-recorder.test.ts +410 -0
  260. package/src/testing/test-recorder.ts +133 -0
  261. package/src/testing/test-renderer.ts +375 -0
  262. package/src/testing/test-renderer.wait.test.ts +278 -0
  263. package/src/testing/test-streams.ts +31 -0
  264. package/src/testing.ts +9 -0
  265. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +481 -0
  266. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +19 -0
  267. package/src/tests/__snapshots__/scrollbox.test.ts.snap +29 -0
  268. package/src/tests/absolute-positioning.snapshot.test.ts +638 -0
  269. package/src/tests/allocator-stats.test.ts +53 -0
  270. package/src/tests/audio.test.ts +303 -0
  271. package/src/tests/destroy-during-render.test.ts +200 -0
  272. package/src/tests/destroy-on-exit.fixture.ts +36 -0
  273. package/src/tests/destroy-on-exit.test.ts +62 -0
  274. package/src/tests/ffi-borrowed-pointer-callsites.test.ts +153 -0
  275. package/src/tests/hover-cursor.test.ts +98 -0
  276. package/src/tests/native-backed-measurement-lifecycle.test.ts +133 -0
  277. package/src/tests/native-backed-measurement-parity.test.ts +322 -0
  278. package/src/tests/native-span-feed-async.test.ts +173 -0
  279. package/src/tests/native-span-feed-close.test.ts +120 -0
  280. package/src/tests/native-span-feed-coverage.test.ts +227 -0
  281. package/src/tests/native-span-feed-edge-cases.test.ts +384 -0
  282. package/src/tests/native-span-feed-use-after-free.test.ts +45 -0
  283. package/src/tests/opacity.test.ts +123 -0
  284. package/src/tests/renderable.snapshot.test.ts +524 -0
  285. package/src/tests/renderable.test.ts +1851 -0
  286. package/src/tests/renderer.clock.test.ts +270 -0
  287. package/src/tests/renderer.console-startup.test.ts +2151 -0
  288. package/src/tests/renderer.control.test.ts +458 -0
  289. package/src/tests/renderer.core-slot-binding.test.ts +962 -0
  290. package/src/tests/renderer.cursor.test.ts +26 -0
  291. package/src/tests/renderer.custom-stdout.test.ts +1112 -0
  292. package/src/tests/renderer.destroy-during-render.test.ts +147 -0
  293. package/src/tests/renderer.focus-restore.test.ts +257 -0
  294. package/src/tests/renderer.focus.test.ts +329 -0
  295. package/src/tests/renderer.idle.test.ts +219 -0
  296. package/src/tests/renderer.input.test.ts +2615 -0
  297. package/src/tests/renderer.kitty-flags.test.ts +195 -0
  298. package/src/tests/renderer.lifecycle.test.ts +34 -0
  299. package/src/tests/renderer.mouse.test.ts +1315 -0
  300. package/src/tests/renderer.notifications.test.ts +27 -0
  301. package/src/tests/renderer.palette.test.ts +1199 -0
  302. package/src/tests/renderer.remote.test.ts +247 -0
  303. package/src/tests/renderer.render-stats.test.ts +146 -0
  304. package/src/tests/renderer.scrollback-surface.test.ts +474 -0
  305. package/src/tests/renderer.selection.test.ts +138 -0
  306. package/src/tests/renderer.slot-registry.test.ts +684 -0
  307. package/src/tests/renderer.tracker.test.ts +202 -0
  308. package/src/tests/renderer.useMouse.test.ts +83 -0
  309. package/src/tests/runtime-plugin-node-modules-cycle.fixture.ts +76 -0
  310. package/src/tests/runtime-plugin-node-modules-import-like-string.fixture.ts +48 -0
  311. package/src/tests/runtime-plugin-node-modules-mjs.fixture.ts +43 -0
  312. package/src/tests/runtime-plugin-node-modules-no-bare-rewrite.fixture.ts +67 -0
  313. package/src/tests/runtime-plugin-node-modules-package-type-cache.fixture.ts +72 -0
  314. package/src/tests/runtime-plugin-node-modules-runtime-specifier.fixture.ts +44 -0
  315. package/src/tests/runtime-plugin-node-modules-scoped-package-bare-rewrite.fixture.ts +85 -0
  316. package/src/tests/runtime-plugin-path-alias.fixture.ts +43 -0
  317. package/src/tests/runtime-plugin-resolve-roots.fixture.ts +65 -0
  318. package/src/tests/runtime-plugin-support-configure.fixture.ts +31 -0
  319. package/src/tests/runtime-plugin-support-late-addition.fixture.ts +17 -0
  320. package/src/tests/runtime-plugin-support.fixture.ts +11 -0
  321. package/src/tests/runtime-plugin-support.test.ts +54 -0
  322. package/src/tests/runtime-plugin-windows-file-url.fixture.ts +30 -0
  323. package/src/tests/runtime-plugin.fixture.ts +40 -0
  324. package/src/tests/runtime-plugin.test.ts +369 -0
  325. package/src/tests/scrollbox-culling-bug.test.ts +114 -0
  326. package/src/tests/scrollbox-hitgrid-resize.test.ts +136 -0
  327. package/src/tests/scrollbox-hitgrid.test.ts +909 -0
  328. package/src/tests/scrollbox.test.ts +1895 -0
  329. package/src/tests/wrap-resize-perf.test.ts +276 -0
  330. package/src/tests/yoga-api-coverage.test.ts +409 -0
  331. package/src/tests/yoga-callback-stress.test.ts +122 -0
  332. package/src/tests/yoga-ffi.test.ts +117 -0
  333. package/src/tests/yoga-setters.test.ts +921 -0
  334. package/src/tests/yoga-upstream/YGAlignBaselineTest.test.ts +124 -0
  335. package/src/tests/yoga-upstream/YGComputedBorderTest.test.ts +27 -0
  336. package/src/tests/yoga-upstream/YGComputedMarginTest.test.ts +27 -0
  337. package/src/tests/yoga-upstream/YGComputedPaddingTest.test.ts +27 -0
  338. package/src/tests/yoga-upstream/YGDirtiedTest.test.ts +157 -0
  339. package/src/tests/yoga-upstream/YGErrataTest.test.ts +40 -0
  340. package/src/tests/yoga-upstream/YGFlexBasisAuto.test.ts +23 -0
  341. package/src/tests/yoga-upstream/YGHasNewLayout.test.ts +81 -0
  342. package/src/tests/yoga-upstream/YGMeasureCacheTest.test.ts +31 -0
  343. package/src/tests/yoga-upstream/YGMeasureTest.test.ts +70 -0
  344. package/src/tests/yoga-upstream/generated/YGAbsolutePositionTest.test.ts +1864 -0
  345. package/src/tests/yoga-upstream/generated/YGAlignContentTest.test.ts +5829 -0
  346. package/src/tests/yoga-upstream/generated/YGAlignItemsTest.test.ts +2444 -0
  347. package/src/tests/yoga-upstream/generated/YGAlignSelfTest.test.ts +293 -0
  348. package/src/tests/yoga-upstream/generated/YGAndroidNewsFeed.test.ts +315 -0
  349. package/src/tests/yoga-upstream/generated/YGAspectRatioTest.test.ts +231 -0
  350. package/src/tests/yoga-upstream/generated/YGAutoTest.test.ts +347 -0
  351. package/src/tests/yoga-upstream/generated/YGBorderTest.test.ts +256 -0
  352. package/src/tests/yoga-upstream/generated/YGBoxSizingTest.test.ts +2747 -0
  353. package/src/tests/yoga-upstream/generated/YGDimensionTest.test.ts +129 -0
  354. package/src/tests/yoga-upstream/generated/YGDisplayContentsTest.test.ts +109 -0
  355. package/src/tests/yoga-upstream/generated/YGDisplayTest.test.ts +1097 -0
  356. package/src/tests/yoga-upstream/generated/YGFlexDirectionTest.test.ts +4562 -0
  357. package/src/tests/yoga-upstream/generated/YGFlexTest.test.ts +677 -0
  358. package/src/tests/yoga-upstream/generated/YGFlexWrapTest.test.ts +2071 -0
  359. package/src/tests/yoga-upstream/generated/YGGapTest.test.ts +3303 -0
  360. package/src/tests/yoga-upstream/generated/YGIntrinsicSizeTest.test.ts +582 -0
  361. package/src/tests/yoga-upstream/generated/YGJustifyContentTest.test.ts +2150 -0
  362. package/src/tests/yoga-upstream/generated/YGMarginTest.test.ts +1963 -0
  363. package/src/tests/yoga-upstream/generated/YGMinMaxDimensionTest.test.ts +1446 -0
  364. package/src/tests/yoga-upstream/generated/YGPaddingTest.test.ts +354 -0
  365. package/src/tests/yoga-upstream/generated/YGPercentageTest.test.ts +1760 -0
  366. package/src/tests/yoga-upstream/generated/YGRoundingTest.test.ts +1161 -0
  367. package/src/tests/yoga-upstream/generated/YGSizeOverflowTest.test.ts +207 -0
  368. package/src/tests/yoga-upstream/generated/YGStaticPositionTest.test.ts +6338 -0
  369. package/src/tests/yoga-upstream/tools/MeasureCounter.ts +54 -0
  370. package/src/tests/yoga-upstream/tools/utils.ts +81 -0
  371. package/src/text-buffer-view.test.ts +722 -0
  372. package/src/text-buffer-view.ts +193 -0
  373. package/src/text-buffer.test.ts +512 -0
  374. package/src/text-buffer.ts +250 -0
  375. package/src/types.ts +199 -0
  376. package/src/utils.ts +88 -0
  377. package/src/yoga.ts +1146 -0
  378. package/src/zig/ansi.zig +502 -0
  379. package/src/zig/audio.zig +1206 -0
  380. package/src/zig/bench/README.md +50 -0
  381. package/src/zig/bench/buffer-color-blending_bench.zig +350 -0
  382. package/src/zig/bench/buffer-draw-box_bench.zig +786 -0
  383. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +894 -0
  384. package/src/zig/bench/edit-buffer_bench.zig +476 -0
  385. package/src/zig/bench/editor-view_bench.zig +169 -0
  386. package/src/zig/bench/native-span-feed_bench.zig +107 -0
  387. package/src/zig/bench/rope-markers_bench.zig +713 -0
  388. package/src/zig/bench/rope_bench.zig +514 -0
  389. package/src/zig/bench/styled-text_bench.zig +476 -0
  390. package/src/zig/bench/text-buffer-coords_bench.zig +362 -0
  391. package/src/zig/bench/text-buffer-view_bench.zig +459 -0
  392. package/src/zig/bench/text-chunk-graphemes_bench.zig +272 -0
  393. package/src/zig/bench/utf8_bench.zig +799 -0
  394. package/src/zig/bench-utils.zig +431 -0
  395. package/src/zig/bench.zig +223 -0
  396. package/src/zig/buffer-methods.zig +215 -0
  397. package/src/zig/buffer.zig +2515 -0
  398. package/src/zig/build.zig +700 -0
  399. package/src/zig/build.zig.zon +19 -0
  400. package/src/zig/edit-buffer.zig +900 -0
  401. package/src/zig/editor-view.zig +812 -0
  402. package/src/zig/event-bus.zig +28 -0
  403. package/src/zig/event-emitter.zig +65 -0
  404. package/src/zig/file-logger.zig +92 -0
  405. package/src/zig/grapheme.zig +838 -0
  406. package/src/zig/handles.zig +267 -0
  407. package/src/zig/lib.zig +2813 -0
  408. package/src/zig/link.zig +349 -0
  409. package/src/zig/logger.zig +43 -0
  410. package/src/zig/mem-registry.zig +126 -0
  411. package/src/zig/miniaudio_shim.c +21 -0
  412. package/src/zig/native-renderable.zig +112 -0
  413. package/src/zig/native-span-feed-bench-lib.zig +7 -0
  414. package/src/zig/native-span-feed.zig +747 -0
  415. package/src/zig/renderer-output.zig +677 -0
  416. package/src/zig/renderer.zig +2106 -0
  417. package/src/zig/rope.zig +1220 -0
  418. package/src/zig/split-scrollback.zig +88 -0
  419. package/src/zig/syntax-style.zig +174 -0
  420. package/src/zig/terminal.zig +1652 -0
  421. package/src/zig/test.zig +82 -0
  422. package/src/zig/tests/README.md +18 -0
  423. package/src/zig/tests/ansi_test.zig +18 -0
  424. package/src/zig/tests/audio_test.zig +439 -0
  425. package/src/zig/tests/buffer-methods_test.zig +1111 -0
  426. package/src/zig/tests/buffer_test.zig +2926 -0
  427. package/src/zig/tests/edit-buffer-history_test.zig +299 -0
  428. package/src/zig/tests/edit-buffer_test.zig +1688 -0
  429. package/src/zig/tests/editor-view_test.zig +3454 -0
  430. package/src/zig/tests/event-emitter_test.zig +249 -0
  431. package/src/zig/tests/grapheme_test.zig +1329 -0
  432. package/src/zig/tests/handles_test.zig +123 -0
  433. package/src/zig/tests/link_test.zig +220 -0
  434. package/src/zig/tests/mem-registry_test.zig +473 -0
  435. package/src/zig/tests/memory_leak_regression_test.zig +234 -0
  436. package/src/zig/tests/native-renderable_test.zig +16 -0
  437. package/src/zig/tests/native-span-feed_test.zig +1266 -0
  438. package/src/zig/tests/renderer_test.zig +2604 -0
  439. package/src/zig/tests/rope-nested_test.zig +712 -0
  440. package/src/zig/tests/rope_fuzz_test.zig +238 -0
  441. package/src/zig/tests/rope_test.zig +2362 -0
  442. package/src/zig/tests/segment-merge.test.zig +148 -0
  443. package/src/zig/tests/split-scrollback_test.zig +70 -0
  444. package/src/zig/tests/syntax-style_test.zig +558 -0
  445. package/src/zig/tests/terminal_test.zig +1489 -0
  446. package/src/zig/tests/test-renderer.zig +126 -0
  447. package/src/zig/tests/text-buffer-drawing_test.zig +3291 -0
  448. package/src/zig/tests/text-buffer-highlights_test.zig +706 -0
  449. package/src/zig/tests/text-buffer-iterators_test.zig +776 -0
  450. package/src/zig/tests/text-buffer-segment_test.zig +320 -0
  451. package/src/zig/tests/text-buffer-selection_test.zig +1056 -0
  452. package/src/zig/tests/text-buffer-selection_viewport_test.zig +358 -0
  453. package/src/zig/tests/text-buffer-view_test.zig +3672 -0
  454. package/src/zig/tests/text-buffer_test.zig +2191 -0
  455. package/src/zig/tests/unicode-width-map.zon +3909 -0
  456. package/src/zig/tests/utf8_no_zwj_test.zig +260 -0
  457. package/src/zig/tests/utf8_test.zig +4060 -0
  458. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +267 -0
  459. package/src/zig/tests/utf8_wcwidth_test.zig +357 -0
  460. package/src/zig/tests/word-wrap-editing_test.zig +498 -0
  461. package/src/zig/tests/wrap-cache-perf_test.zig +113 -0
  462. package/src/zig/tests/yoga_test.zig +65 -0
  463. package/src/zig/text-buffer-iterators.zig +496 -0
  464. package/src/zig/text-buffer-segment.zig +403 -0
  465. package/src/zig/text-buffer-view.zig +1428 -0
  466. package/src/zig/text-buffer.zig +1272 -0
  467. package/src/zig/utf8.zig +1954 -0
  468. package/src/zig/utils.zig +12 -0
  469. package/src/zig/uucode-stub.zig +76 -0
  470. package/src/zig/vendor/miniaudio/miniaudio.h +93468 -0
  471. package/src/zig/yoga.zig +658 -0
  472. package/src/zig-structs.ts +381 -0
  473. package/src/zig.ts +5260 -0
  474. package/tsconfig.build.json +17 -0
  475. package/tsconfig.json +27 -0
  476. package/tsconfig.node-test.json +138 -0
@@ -0,0 +1,1434 @@
1
+ import { MeasureMode } from "../yoga.js"
2
+ import { type RenderableOptions, Renderable } from "../Renderable.js"
3
+ import type { OptimizedBuffer } from "../buffer.js"
4
+ import { type BorderStyle, BorderCharArrays, parseBorderStyle } from "../lib/border.js"
5
+ import { convertGlobalToLocalSelection, type Selection, type LocalSelectionBounds } from "../lib/selection.js"
6
+ import { StyledText, stringToStyledText } from "../lib/styled-text.js"
7
+ import { RGBA, parseColor, type ColorInput } from "../lib/RGBA.js"
8
+ import { SyntaxStyle } from "../syntax-style.js"
9
+ import { type TextChunk, TextBuffer } from "../text-buffer.js"
10
+ import { TextBufferView } from "../text-buffer-view.js"
11
+ import type { RenderContext } from "../types.js"
12
+
13
+ // Large sentinel height for text measurement. The Zig measure path currently
14
+ // ignores height, but we pass an effectively unbounded value so if height-aware
15
+ // measuring is introduced later, table sizing remains stable.
16
+ const MEASURE_HEIGHT = 10_000
17
+
18
+ export type TextTableCellContent = TextChunk[] | null | undefined
19
+ export type TextTableContent = TextTableCellContent[][]
20
+ export type TextTableColumnWidthMode = "content" | "full"
21
+ export type TextTableColumnFitter = "proportional" | "balanced"
22
+
23
+ interface ResolvedTableBorderLayout {
24
+ left: boolean
25
+ right: boolean
26
+ top: boolean
27
+ bottom: boolean
28
+ innerVertical: boolean
29
+ innerHorizontal: boolean
30
+ }
31
+
32
+ interface TextTableCellState {
33
+ textBuffer: TextBuffer
34
+ textBufferView: TextBufferView
35
+ syntaxStyle: SyntaxStyle
36
+ }
37
+
38
+ interface TextTableLayout {
39
+ columnWidths: number[]
40
+ rowHeights: number[]
41
+ columnOffsets: number[]
42
+ rowOffsets: number[]
43
+ columnOffsetsI32: Int32Array
44
+ rowOffsetsI32: Int32Array
45
+ tableWidth: number
46
+ tableHeight: number
47
+ }
48
+
49
+ interface CellPosition {
50
+ rowIdx: number
51
+ colIdx: number
52
+ }
53
+
54
+ interface RowRange {
55
+ firstRow: number
56
+ lastRow: number
57
+ }
58
+
59
+ type TableSelectionMode = "single-cell" | "column-locked" | "grid"
60
+
61
+ interface SelectionResolution {
62
+ mode: TableSelectionMode
63
+ anchorCell: CellPosition | null
64
+ anchorColumn: number | null
65
+ }
66
+
67
+ interface CellSelectionCoords {
68
+ anchorX: number
69
+ anchorY: number
70
+ focusX: number
71
+ focusY: number
72
+ }
73
+
74
+ export interface TextTableOptions extends RenderableOptions<TextTableRenderable> {
75
+ content?: TextTableContent
76
+ wrapMode?: "none" | "char" | "word"
77
+ columnWidthMode?: TextTableColumnWidthMode
78
+ columnFitter?: TextTableColumnFitter
79
+ cellPadding?: number
80
+ cellPaddingX?: number
81
+ cellPaddingY?: number
82
+ columnGap?: number
83
+ showBorders?: boolean
84
+ border?: boolean
85
+ outerBorder?: boolean
86
+ selectable?: boolean
87
+ selectionBg?: ColorInput
88
+ selectionFg?: ColorInput
89
+ borderStyle?: BorderStyle
90
+ borderColor?: ColorInput
91
+ borderBackgroundColor?: ColorInput
92
+ backgroundColor?: ColorInput
93
+ fg?: ColorInput
94
+ bg?: ColorInput
95
+ attributes?: number
96
+ }
97
+
98
+ export class TextTableRenderable extends Renderable {
99
+ private _content: TextTableContent
100
+ private _wrapMode: "none" | "char" | "word"
101
+ private _columnWidthMode: TextTableColumnWidthMode
102
+ private _columnFitter: TextTableColumnFitter
103
+ private _cellPaddingX: number
104
+ private _cellPaddingY: number
105
+ private _columnGap: number
106
+ private _showBorders: boolean
107
+ private _border: boolean
108
+ private _outerBorder: boolean
109
+ private _hasExplicitOuterBorder: boolean
110
+ private _borderStyle: BorderStyle
111
+ private _borderColor: RGBA
112
+ private _borderBackgroundColor: RGBA
113
+ private _backgroundColor: RGBA
114
+ private _defaultFg: RGBA
115
+ private _defaultBg: RGBA
116
+ private _defaultAttributes: number
117
+ private _selectionBg: RGBA | undefined
118
+ private _selectionFg: RGBA | undefined
119
+ private _lastLocalSelection: LocalSelectionBounds | null = null
120
+ private _lastSelectionMode: TableSelectionMode | null = null
121
+
122
+ private _cells: TextTableCellState[][] = []
123
+ private _prevCellContent: TextTableCellContent[][] = []
124
+ private _rowCount: number = 0
125
+ private _columnCount: number = 0
126
+
127
+ private _layout: TextTableLayout = this.createEmptyLayout()
128
+ private _layoutDirty: boolean = true
129
+ private _rasterDirty: boolean = true
130
+
131
+ private _cachedMeasureLayout: TextTableLayout | null = null
132
+ private _cachedMeasureWidth: number | undefined = undefined
133
+
134
+ private readonly _defaultOptions = {
135
+ content: [] as TextTableContent,
136
+ wrapMode: "word" as "none" | "char" | "word",
137
+ columnWidthMode: "full" as TextTableColumnWidthMode,
138
+ columnFitter: "proportional" as TextTableColumnFitter,
139
+ cellPadding: 0,
140
+ cellPaddingX: undefined as number | undefined,
141
+ cellPaddingY: undefined as number | undefined,
142
+ columnGap: 0,
143
+ showBorders: true,
144
+ border: true,
145
+ outerBorder: true,
146
+ selectable: true,
147
+ selectionBg: undefined as ColorInput | undefined,
148
+ selectionFg: undefined as ColorInput | undefined,
149
+ borderStyle: "single" as BorderStyle,
150
+ borderColor: "#FFFFFF",
151
+ borderBackgroundColor: "transparent",
152
+ backgroundColor: "transparent",
153
+ fg: "#FFFFFF",
154
+ bg: "transparent",
155
+ attributes: 0,
156
+ } satisfies Partial<TextTableOptions>
157
+
158
+ constructor(ctx: RenderContext, options: TextTableOptions = {}) {
159
+ super(ctx, { ...options, flexShrink: options.flexShrink ?? 0, buffered: true })
160
+
161
+ this._content = options.content ?? this._defaultOptions.content
162
+ this._wrapMode = options.wrapMode ?? this._defaultOptions.wrapMode
163
+ this._columnWidthMode = options.columnWidthMode ?? this._defaultOptions.columnWidthMode
164
+ this._columnFitter = this.resolveColumnFitter(options.columnFitter)
165
+ this._cellPaddingX = this.resolveCellPadding(options.cellPaddingX ?? options.cellPadding)
166
+ this._cellPaddingY = this.resolveCellPadding(options.cellPaddingY ?? options.cellPadding)
167
+ this._columnGap = this.resolveColumnGap(options.columnGap)
168
+ this._showBorders = options.showBorders ?? this._defaultOptions.showBorders
169
+ this._border = options.border ?? this._defaultOptions.border
170
+ this._hasExplicitOuterBorder = options.outerBorder !== undefined
171
+ this._outerBorder = options.outerBorder ?? this._border
172
+ this.selectable = options.selectable ?? this._defaultOptions.selectable
173
+ this._selectionBg = options.selectionBg ? parseColor(options.selectionBg) : undefined
174
+ this._selectionFg = options.selectionFg ? parseColor(options.selectionFg) : undefined
175
+ this._borderStyle = parseBorderStyle(options.borderStyle, this._defaultOptions.borderStyle)
176
+ this._borderColor = parseColor(options.borderColor ?? this._defaultOptions.borderColor)
177
+ this._borderBackgroundColor = parseColor(
178
+ options.borderBackgroundColor ?? this._defaultOptions.borderBackgroundColor,
179
+ )
180
+ this._backgroundColor = parseColor(options.backgroundColor ?? this._defaultOptions.backgroundColor)
181
+ this._defaultFg = parseColor(options.fg ?? this._defaultOptions.fg)
182
+ this._defaultBg = parseColor(options.bg ?? this._defaultOptions.bg)
183
+ this._defaultAttributes = options.attributes ?? this._defaultOptions.attributes
184
+
185
+ this.setupMeasureFunc()
186
+ this.rebuildCells()
187
+ }
188
+
189
+ public get content(): TextTableContent {
190
+ return this._content
191
+ }
192
+
193
+ public set content(value: TextTableContent) {
194
+ this._content = value ?? []
195
+ this.rebuildCells()
196
+ }
197
+
198
+ public get wrapMode(): "none" | "char" | "word" {
199
+ return this._wrapMode
200
+ }
201
+
202
+ public set wrapMode(value: "none" | "char" | "word") {
203
+ if (this._wrapMode === value) return
204
+ this._wrapMode = value
205
+ for (const row of this._cells) {
206
+ for (const cell of row) {
207
+ cell.textBufferView.setWrapMode(value)
208
+ }
209
+ }
210
+ this.invalidateLayoutAndRaster()
211
+ }
212
+
213
+ public get columnWidthMode(): TextTableColumnWidthMode {
214
+ return this._columnWidthMode
215
+ }
216
+
217
+ public set columnWidthMode(value: TextTableColumnWidthMode) {
218
+ if (this._columnWidthMode === value) return
219
+ this._columnWidthMode = value
220
+ this.invalidateLayoutAndRaster()
221
+ }
222
+
223
+ public get columnFitter(): TextTableColumnFitter {
224
+ return this._columnFitter
225
+ }
226
+
227
+ public set columnFitter(value: TextTableColumnFitter) {
228
+ const next = this.resolveColumnFitter(value)
229
+ if (this._columnFitter === next) return
230
+ this._columnFitter = next
231
+ this.invalidateLayoutAndRaster()
232
+ }
233
+
234
+ public get cellPadding(): number {
235
+ return this._cellPaddingX === this._cellPaddingY ? this._cellPaddingX : 0
236
+ }
237
+
238
+ public set cellPadding(value: number) {
239
+ const next = this.resolveCellPadding(value)
240
+ if (this._cellPaddingX === next && this._cellPaddingY === next) return
241
+ this._cellPaddingX = next
242
+ this._cellPaddingY = next
243
+ this.invalidateLayoutAndRaster()
244
+ }
245
+
246
+ public get cellPaddingX(): number {
247
+ return this._cellPaddingX
248
+ }
249
+
250
+ public set cellPaddingX(value: number) {
251
+ const next = this.resolveCellPadding(value)
252
+ if (this._cellPaddingX === next) return
253
+ this._cellPaddingX = next
254
+ this.invalidateLayoutAndRaster()
255
+ }
256
+
257
+ public get cellPaddingY(): number {
258
+ return this._cellPaddingY
259
+ }
260
+
261
+ public set cellPaddingY(value: number) {
262
+ const next = this.resolveCellPadding(value)
263
+ if (this._cellPaddingY === next) return
264
+ this._cellPaddingY = next
265
+ this.invalidateLayoutAndRaster()
266
+ }
267
+
268
+ public get columnGap(): number {
269
+ return this._columnGap
270
+ }
271
+
272
+ public set columnGap(value: number) {
273
+ const next = this.resolveColumnGap(value)
274
+ if (this._columnGap === next) return
275
+ this._columnGap = next
276
+ this.invalidateLayoutAndRaster()
277
+ }
278
+
279
+ public get showBorders(): boolean {
280
+ return this._showBorders
281
+ }
282
+
283
+ public set showBorders(value: boolean) {
284
+ if (this._showBorders === value) return
285
+ this._showBorders = value
286
+ this.invalidateRasterOnly()
287
+ }
288
+
289
+ public get outerBorder(): boolean {
290
+ return this._outerBorder
291
+ }
292
+
293
+ public set outerBorder(value: boolean) {
294
+ if (this._outerBorder === value) return
295
+
296
+ this._hasExplicitOuterBorder = true
297
+ this._outerBorder = value
298
+ this.invalidateLayoutAndRaster()
299
+ }
300
+
301
+ public get border(): boolean {
302
+ return this._border
303
+ }
304
+
305
+ public set border(value: boolean) {
306
+ if (this._border === value) return
307
+
308
+ this._border = value
309
+
310
+ if (!this._hasExplicitOuterBorder) {
311
+ this._outerBorder = value
312
+ }
313
+
314
+ this.invalidateLayoutAndRaster()
315
+ }
316
+
317
+ public get borderStyle(): BorderStyle {
318
+ return this._borderStyle
319
+ }
320
+
321
+ public set borderStyle(value: BorderStyle) {
322
+ const next = parseBorderStyle(value, this._defaultOptions.borderStyle)
323
+ if (this._borderStyle === next) return
324
+ this._borderStyle = next
325
+ this.invalidateRasterOnly()
326
+ }
327
+
328
+ public get borderColor(): RGBA {
329
+ return this._borderColor
330
+ }
331
+
332
+ public set borderColor(value: ColorInput) {
333
+ const next = parseColor(value)
334
+ if (this._borderColor === next) return
335
+ this._borderColor = next
336
+ this.invalidateRasterOnly()
337
+ }
338
+
339
+ public shouldStartSelection(x: number, y: number): boolean {
340
+ if (!this.selectable) return false
341
+
342
+ this.ensureLayoutReady()
343
+
344
+ const localX = x - this.x
345
+ const localY = y - this.y
346
+ return this.getCellAtLocalPosition(localX, localY) !== null
347
+ }
348
+
349
+ public onSelectionChanged(selection: Selection | null): boolean {
350
+ this.ensureLayoutReady()
351
+
352
+ const previousLocalSelection = this._lastLocalSelection
353
+ const localSelection = convertGlobalToLocalSelection(selection, this.x, this.y)
354
+ this._lastLocalSelection = localSelection
355
+ const dirtyRows = this.getDirtySelectionRowRange(previousLocalSelection, localSelection)
356
+
357
+ if (!localSelection?.isActive) {
358
+ this.resetCellSelections()
359
+ this._lastSelectionMode = null
360
+ } else {
361
+ this.applySelectionToCells(localSelection, selection?.isStart ?? false)
362
+ }
363
+
364
+ if (dirtyRows !== null) {
365
+ this.redrawSelectionRows(dirtyRows.firstRow, dirtyRows.lastRow)
366
+ }
367
+
368
+ return this.hasSelection()
369
+ }
370
+
371
+ public hasSelection(): boolean {
372
+ for (const row of this._cells) {
373
+ for (const cell of row) {
374
+ if (cell.textBufferView.hasSelection()) {
375
+ return true
376
+ }
377
+ }
378
+ }
379
+
380
+ return false
381
+ }
382
+
383
+ public getSelection(): { start: number; end: number } | null {
384
+ for (const row of this._cells) {
385
+ for (const cell of row) {
386
+ const selection = cell.textBufferView.getSelection()
387
+ if (selection) {
388
+ return selection
389
+ }
390
+ }
391
+ }
392
+
393
+ return null
394
+ }
395
+
396
+ public getSelectedText(): string {
397
+ const selectedRows: string[] = []
398
+
399
+ for (let rowIdx = 0; rowIdx < this._rowCount; rowIdx++) {
400
+ const rowSelections: string[] = []
401
+
402
+ for (let colIdx = 0; colIdx < this._columnCount; colIdx++) {
403
+ const cell = this._cells[rowIdx]?.[colIdx]
404
+ if (!cell || !cell.textBufferView.hasSelection()) continue
405
+
406
+ const selectedText = cell.textBufferView.getSelectedText()
407
+ if (selectedText.length > 0) {
408
+ rowSelections.push(selectedText)
409
+ }
410
+ }
411
+
412
+ if (rowSelections.length > 0) {
413
+ selectedRows.push(rowSelections.join("\t"))
414
+ }
415
+ }
416
+
417
+ return selectedRows.join("\n")
418
+ }
419
+
420
+ protected onResize(width: number, height: number): void {
421
+ this.invalidateLayoutAndRaster(false)
422
+ super.onResize(width, height)
423
+ }
424
+
425
+ protected renderSelf(buffer: OptimizedBuffer): void {
426
+ if (!this.visible || this.isDestroyed) return
427
+
428
+ if (this._layoutDirty) {
429
+ this.rebuildLayoutForCurrentWidth()
430
+ }
431
+
432
+ if (!this._rasterDirty) return
433
+
434
+ buffer.clear(this._backgroundColor)
435
+
436
+ if (this._rowCount === 0 || this._columnCount === 0) {
437
+ this._rasterDirty = false
438
+ return
439
+ }
440
+
441
+ this.drawBorders(buffer)
442
+ this.drawCells(buffer)
443
+
444
+ this._rasterDirty = false
445
+ }
446
+
447
+ protected destroySelf(): void {
448
+ this.destroyCells()
449
+ super.destroySelf()
450
+ }
451
+
452
+ private setupMeasureFunc(): void {
453
+ const measureFunc = (
454
+ width: number,
455
+ widthMode: MeasureMode,
456
+ _height: number,
457
+ _heightMode: MeasureMode,
458
+ ): { width: number; height: number } => {
459
+ const hasWidthConstraint = widthMode !== MeasureMode.Undefined && Number.isFinite(width)
460
+ const rawWidthConstraint = hasWidthConstraint ? Math.max(1, Math.floor(width)) : undefined
461
+ const widthConstraint = this.resolveLayoutWidthConstraint(rawWidthConstraint)
462
+ const measuredLayout = this.computeLayout(widthConstraint)
463
+ this._cachedMeasureLayout = measuredLayout
464
+ this._cachedMeasureWidth = widthConstraint
465
+
466
+ let measuredWidth = measuredLayout.tableWidth > 0 ? measuredLayout.tableWidth : 1
467
+ let measuredHeight = measuredLayout.tableHeight > 0 ? measuredLayout.tableHeight : 1
468
+
469
+ if (widthMode === MeasureMode.AtMost && rawWidthConstraint !== undefined && this._positionType !== "absolute") {
470
+ measuredWidth = Math.min(rawWidthConstraint, measuredWidth)
471
+ }
472
+
473
+ // Keep intrinsic height even under AtMost constraints. Clamping here can under-report
474
+ // content height during Yoga measure passes and leave parent scroll extents stale.
475
+ return {
476
+ width: measuredWidth,
477
+ height: measuredHeight,
478
+ }
479
+ }
480
+
481
+ this.yogaNode.setMeasureFunc(measureFunc)
482
+ }
483
+
484
+ private rebuildCells(): void {
485
+ const newRowCount = this._content.length
486
+ const newColumnCount = this._content.reduce((max, row) => Math.max(max, row.length), 0)
487
+
488
+ if (this._cells.length === 0) {
489
+ this._rowCount = newRowCount
490
+ this._columnCount = newColumnCount
491
+ this._cells = []
492
+ this._prevCellContent = []
493
+
494
+ for (let rowIdx = 0; rowIdx < newRowCount; rowIdx++) {
495
+ const row = this._content[rowIdx] ?? []
496
+ const rowCells: TextTableCellState[] = []
497
+ const rowRefs: TextTableCellContent[] = []
498
+
499
+ for (let colIdx = 0; colIdx < newColumnCount; colIdx++) {
500
+ const cellContent = row[colIdx]
501
+ rowCells.push(this.createCell(cellContent))
502
+ rowRefs.push(cellContent)
503
+ }
504
+
505
+ this._cells.push(rowCells)
506
+ this._prevCellContent.push(rowRefs)
507
+ }
508
+
509
+ this.invalidateLayoutAndRaster()
510
+ return
511
+ }
512
+
513
+ this.updateCellsDiff(newRowCount, newColumnCount)
514
+ this.invalidateLayoutAndRaster()
515
+ }
516
+
517
+ private updateCellsDiff(newRowCount: number, newColumnCount: number): void {
518
+ const oldRowCount = this._rowCount
519
+ const oldColumnCount = this._columnCount
520
+ const keepRows = Math.min(oldRowCount, newRowCount)
521
+ const keepCols = Math.min(oldColumnCount, newColumnCount)
522
+
523
+ for (let rowIdx = 0; rowIdx < keepRows; rowIdx++) {
524
+ const newRow = this._content[rowIdx] ?? []
525
+ const cellRow = this._cells[rowIdx]
526
+ const refRow = this._prevCellContent[rowIdx]
527
+
528
+ for (let colIdx = 0; colIdx < keepCols; colIdx++) {
529
+ const cellContent = newRow[colIdx]
530
+ if (cellContent === refRow[colIdx]) continue
531
+
532
+ const oldCell = cellRow[colIdx]
533
+ oldCell.textBufferView.destroy()
534
+ oldCell.textBuffer.destroy()
535
+ oldCell.syntaxStyle.destroy()
536
+
537
+ cellRow[colIdx] = this.createCell(cellContent)
538
+ refRow[colIdx] = cellContent
539
+ }
540
+
541
+ if (newColumnCount > oldColumnCount) {
542
+ for (let colIdx = oldColumnCount; colIdx < newColumnCount; colIdx++) {
543
+ const cellContent = newRow[colIdx]
544
+ cellRow.push(this.createCell(cellContent))
545
+ refRow.push(cellContent)
546
+ }
547
+ } else if (newColumnCount < oldColumnCount) {
548
+ for (let colIdx = newColumnCount; colIdx < oldColumnCount; colIdx++) {
549
+ const cell = cellRow[colIdx]
550
+ cell.textBufferView.destroy()
551
+ cell.textBuffer.destroy()
552
+ cell.syntaxStyle.destroy()
553
+ }
554
+ cellRow.length = newColumnCount
555
+ refRow.length = newColumnCount
556
+ }
557
+ }
558
+
559
+ if (newRowCount > oldRowCount) {
560
+ for (let rowIdx = oldRowCount; rowIdx < newRowCount; rowIdx++) {
561
+ const newRow = this._content[rowIdx] ?? []
562
+ const rowCells: TextTableCellState[] = []
563
+ const rowRefs: TextTableCellContent[] = []
564
+
565
+ for (let colIdx = 0; colIdx < newColumnCount; colIdx++) {
566
+ const cellContent = newRow[colIdx]
567
+ rowCells.push(this.createCell(cellContent))
568
+ rowRefs.push(cellContent)
569
+ }
570
+
571
+ this._cells.push(rowCells)
572
+ this._prevCellContent.push(rowRefs)
573
+ }
574
+ } else if (newRowCount < oldRowCount) {
575
+ for (let rowIdx = newRowCount; rowIdx < oldRowCount; rowIdx++) {
576
+ const row = this._cells[rowIdx]
577
+ for (const cell of row) {
578
+ cell.textBufferView.destroy()
579
+ cell.textBuffer.destroy()
580
+ cell.syntaxStyle.destroy()
581
+ }
582
+ }
583
+ this._cells.length = newRowCount
584
+ this._prevCellContent.length = newRowCount
585
+ }
586
+
587
+ this._rowCount = newRowCount
588
+ this._columnCount = newColumnCount
589
+ }
590
+
591
+ private createCell(content: TextTableCellContent): TextTableCellState {
592
+ const styledText = this.toStyledText(content)
593
+ const textBuffer = TextBuffer.create(this._ctx.widthMethod)
594
+ const syntaxStyle = SyntaxStyle.create()
595
+
596
+ textBuffer.setDefaultFg(this._defaultFg)
597
+ textBuffer.setDefaultBg(this._defaultBg)
598
+ textBuffer.setDefaultAttributes(this._defaultAttributes)
599
+ textBuffer.setSyntaxStyle(syntaxStyle)
600
+ textBuffer.setStyledText(styledText)
601
+
602
+ const textBufferView = TextBufferView.create(textBuffer)
603
+ textBufferView.setWrapMode(this._wrapMode)
604
+
605
+ return { textBuffer, textBufferView, syntaxStyle }
606
+ }
607
+
608
+ private toStyledText(content: TextTableCellContent): StyledText {
609
+ if (Array.isArray(content)) {
610
+ return new StyledText(content)
611
+ }
612
+
613
+ if (content === null || content === undefined) {
614
+ return stringToStyledText("")
615
+ }
616
+
617
+ return stringToStyledText(String(content))
618
+ }
619
+
620
+ private destroyCells(): void {
621
+ for (const row of this._cells) {
622
+ for (const cell of row) {
623
+ cell.textBufferView.destroy()
624
+ cell.textBuffer.destroy()
625
+ cell.syntaxStyle.destroy()
626
+ }
627
+ }
628
+
629
+ this._cells = []
630
+ this._prevCellContent = []
631
+ this._rowCount = 0
632
+ this._columnCount = 0
633
+ this._layout = this.createEmptyLayout()
634
+ }
635
+
636
+ private rebuildLayoutForCurrentWidth(): void {
637
+ const maxTableWidth = this.resolveLayoutWidthConstraint(this.width)
638
+
639
+ let layout: TextTableLayout
640
+ if (this._cachedMeasureLayout !== null && this._cachedMeasureWidth === maxTableWidth) {
641
+ layout = this._cachedMeasureLayout
642
+ } else {
643
+ layout = this.computeLayout(maxTableWidth)
644
+ }
645
+ this._cachedMeasureLayout = null
646
+ this._cachedMeasureWidth = undefined
647
+
648
+ this._layout = layout
649
+ this.applyLayoutToViews(layout)
650
+ this._layoutDirty = false
651
+
652
+ if (this._lastLocalSelection?.isActive) {
653
+ this.applySelectionToCells(this._lastLocalSelection, true)
654
+ }
655
+ }
656
+
657
+ private computeLayout(maxTableWidth?: number): TextTableLayout {
658
+ if (this._rowCount === 0 || this._columnCount === 0) {
659
+ return this.createEmptyLayout()
660
+ }
661
+
662
+ const borderLayout = this.resolveBorderLayout()
663
+ const columnWidths = this.computeColumnWidths(maxTableWidth, borderLayout)
664
+ const rowHeights = this.computeRowHeights(columnWidths)
665
+ const columnOffsets = this.computeOffsets(
666
+ columnWidths,
667
+ borderLayout.left,
668
+ borderLayout.right,
669
+ borderLayout.innerVertical,
670
+ this.getInterColumnGap(borderLayout),
671
+ )
672
+ const rowOffsets = this.computeOffsets(
673
+ rowHeights,
674
+ borderLayout.top,
675
+ borderLayout.bottom,
676
+ borderLayout.innerHorizontal,
677
+ )
678
+ return {
679
+ columnWidths,
680
+ rowHeights,
681
+ columnOffsets,
682
+ rowOffsets,
683
+ columnOffsetsI32: new Int32Array(columnOffsets),
684
+ rowOffsetsI32: new Int32Array(rowOffsets),
685
+ tableWidth: (columnOffsets[columnOffsets.length - 1] ?? 0) + 1,
686
+ tableHeight: (rowOffsets[rowOffsets.length - 1] ?? 0) + 1,
687
+ }
688
+ }
689
+
690
+ private isFullWidthMode(): boolean {
691
+ return this._columnWidthMode === "full"
692
+ }
693
+
694
+ private computeColumnWidths(maxTableWidth: number | undefined, borderLayout: ResolvedTableBorderLayout): number[] {
695
+ const horizontalPadding = this.getHorizontalCellPadding()
696
+ const intrinsicWidths = new Array(this._columnCount).fill(1 + horizontalPadding)
697
+
698
+ for (let rowIdx = 0; rowIdx < this._rowCount; rowIdx++) {
699
+ for (let colIdx = 0; colIdx < this._columnCount; colIdx++) {
700
+ const cell = this._cells[rowIdx]?.[colIdx]
701
+ if (!cell) continue
702
+
703
+ const measure = cell.textBufferView.measureForDimensions(0, MEASURE_HEIGHT)
704
+ const measuredWidth = Math.max(1, measure?.widthColsMax ?? 0) + horizontalPadding
705
+ intrinsicWidths[colIdx] = Math.max(intrinsicWidths[colIdx], measuredWidth)
706
+ }
707
+ }
708
+
709
+ if (maxTableWidth === undefined || !Number.isFinite(maxTableWidth) || maxTableWidth <= 0) {
710
+ return intrinsicWidths
711
+ }
712
+
713
+ const maxContentWidth = Math.max(
714
+ 1,
715
+ Math.floor(maxTableWidth) - this.getVerticalBorderCount(borderLayout) - this.getTotalInterColumnGap(borderLayout),
716
+ )
717
+ const currentWidth = intrinsicWidths.reduce((sum, width) => sum + width, 0)
718
+
719
+ if (currentWidth === maxContentWidth) {
720
+ return intrinsicWidths
721
+ }
722
+
723
+ if (currentWidth < maxContentWidth) {
724
+ if (this.isFullWidthMode()) {
725
+ return this.expandColumnWidths(intrinsicWidths, maxContentWidth)
726
+ }
727
+
728
+ return intrinsicWidths
729
+ }
730
+
731
+ if (this._wrapMode === "none") {
732
+ return intrinsicWidths
733
+ }
734
+
735
+ return this.fitColumnWidths(intrinsicWidths, maxContentWidth)
736
+ }
737
+
738
+ private expandColumnWidths(widths: number[], targetContentWidth: number): number[] {
739
+ const baseWidths = widths.map((width) => Math.max(1, Math.floor(width)))
740
+ const totalBaseWidth = baseWidths.reduce((sum, width) => sum + width, 0)
741
+
742
+ if (totalBaseWidth >= targetContentWidth) {
743
+ return baseWidths
744
+ }
745
+
746
+ const expanded = [...baseWidths]
747
+ const columns = expanded.length
748
+ const extraWidth = targetContentWidth - totalBaseWidth
749
+ const sharedWidth = Math.floor(extraWidth / columns)
750
+ const remainder = extraWidth % columns
751
+
752
+ for (let idx = 0; idx < columns; idx++) {
753
+ expanded[idx] += sharedWidth
754
+ if (idx < remainder) {
755
+ expanded[idx] += 1
756
+ }
757
+ }
758
+
759
+ return expanded
760
+ }
761
+
762
+ private fitColumnWidths(widths: number[], targetContentWidth: number): number[] {
763
+ if (this._columnFitter === "balanced") {
764
+ return this.fitColumnWidthsBalanced(widths, targetContentWidth)
765
+ }
766
+
767
+ return this.fitColumnWidthsProportional(widths, targetContentWidth)
768
+ }
769
+
770
+ private fitColumnWidthsProportional(widths: number[], targetContentWidth: number): number[] {
771
+ const minWidth = 1 + this.getHorizontalCellPadding()
772
+ const hardMinWidths = new Array(widths.length).fill(minWidth)
773
+ const baseWidths = widths.map((width) => Math.max(1, Math.floor(width)))
774
+
775
+ const preferredMinWidths = baseWidths.map((width) => Math.min(width, minWidth + 1))
776
+ const preferredMinTotal = preferredMinWidths.reduce((sum, width) => sum + width, 0)
777
+
778
+ const floorWidths = preferredMinTotal <= targetContentWidth ? preferredMinWidths : hardMinWidths
779
+ const floorTotal = floorWidths.reduce((sum, width) => sum + width, 0)
780
+ const clampedTarget = Math.max(floorTotal, targetContentWidth)
781
+
782
+ const totalBaseWidth = baseWidths.reduce((sum, width) => sum + width, 0)
783
+
784
+ if (totalBaseWidth <= clampedTarget) {
785
+ return baseWidths
786
+ }
787
+
788
+ const shrinkable = baseWidths.map((width, idx) => width - floorWidths[idx])
789
+ const totalShrinkable = shrinkable.reduce((sum, value) => sum + value, 0)
790
+ if (totalShrinkable <= 0) {
791
+ return [...floorWidths]
792
+ }
793
+
794
+ const targetShrink = totalBaseWidth - clampedTarget
795
+ const integerShrink = new Array(baseWidths.length).fill(0)
796
+ const fractions = new Array(baseWidths.length).fill(0)
797
+ let usedShrink = 0
798
+
799
+ for (let idx = 0; idx < baseWidths.length; idx++) {
800
+ if (shrinkable[idx] <= 0) continue
801
+
802
+ const exact = (shrinkable[idx] / totalShrinkable) * targetShrink
803
+ const whole = Math.min(shrinkable[idx], Math.floor(exact))
804
+ integerShrink[idx] = whole
805
+ fractions[idx] = exact - whole
806
+ usedShrink += whole
807
+ }
808
+
809
+ let remainingShrink = targetShrink - usedShrink
810
+
811
+ while (remainingShrink > 0) {
812
+ let bestIdx = -1
813
+ let bestFraction = -1
814
+
815
+ for (let idx = 0; idx < baseWidths.length; idx++) {
816
+ if (shrinkable[idx] - integerShrink[idx] <= 0) continue
817
+ if (fractions[idx] > bestFraction) {
818
+ bestFraction = fractions[idx]
819
+ bestIdx = idx
820
+ }
821
+ }
822
+
823
+ if (bestIdx === -1) break
824
+
825
+ integerShrink[bestIdx] += 1
826
+ fractions[bestIdx] = 0
827
+ remainingShrink -= 1
828
+ }
829
+
830
+ return baseWidths.map((width, idx) => Math.max(floorWidths[idx], width - integerShrink[idx]))
831
+ }
832
+
833
+ private fitColumnWidthsBalanced(widths: number[], targetContentWidth: number): number[] {
834
+ const minWidth = 1 + this.getHorizontalCellPadding()
835
+ const hardMinWidths = new Array(widths.length).fill(minWidth)
836
+ const baseWidths = widths.map((width) => Math.max(1, Math.floor(width)))
837
+ const totalBaseWidth = baseWidths.reduce((sum, width) => sum + width, 0)
838
+ const columns = baseWidths.length
839
+
840
+ if (columns === 0 || totalBaseWidth <= targetContentWidth) {
841
+ return baseWidths
842
+ }
843
+
844
+ const evenShare = Math.max(minWidth, Math.floor(targetContentWidth / columns))
845
+ const preferredMinWidths = baseWidths.map((width) => Math.min(width, evenShare))
846
+ const preferredMinTotal = preferredMinWidths.reduce((sum, width) => sum + width, 0)
847
+ const floorWidths = preferredMinTotal <= targetContentWidth ? preferredMinWidths : hardMinWidths
848
+ const floorTotal = floorWidths.reduce((sum, width) => sum + width, 0)
849
+ const clampedTarget = Math.max(floorTotal, targetContentWidth)
850
+
851
+ if (totalBaseWidth <= clampedTarget) {
852
+ return baseWidths
853
+ }
854
+
855
+ const shrinkable = baseWidths.map((width, idx) => width - floorWidths[idx])
856
+ const totalShrinkable = shrinkable.reduce((sum, value) => sum + value, 0)
857
+ if (totalShrinkable <= 0) {
858
+ return [...floorWidths]
859
+ }
860
+
861
+ const targetShrink = totalBaseWidth - clampedTarget
862
+ const shrink = this.allocateShrinkByWeight(shrinkable, targetShrink, "sqrt")
863
+
864
+ return baseWidths.map((width, idx) => Math.max(floorWidths[idx], width - shrink[idx]))
865
+ }
866
+
867
+ private allocateShrinkByWeight(shrinkable: number[], targetShrink: number, mode: "linear" | "sqrt"): number[] {
868
+ const shrink = new Array(shrinkable.length).fill(0)
869
+
870
+ if (targetShrink <= 0) {
871
+ return shrink
872
+ }
873
+
874
+ const weights = shrinkable.map((value) => {
875
+ if (value <= 0) {
876
+ return 0
877
+ }
878
+
879
+ return mode === "sqrt" ? Math.sqrt(value) : value
880
+ })
881
+ const totalWeight = weights.reduce((sum, value) => sum + value, 0)
882
+
883
+ if (totalWeight <= 0) {
884
+ return shrink
885
+ }
886
+
887
+ const fractions = new Array(shrinkable.length).fill(0)
888
+ let usedShrink = 0
889
+
890
+ for (let idx = 0; idx < shrinkable.length; idx++) {
891
+ if (shrinkable[idx] <= 0 || weights[idx] <= 0) continue
892
+
893
+ const exact = (weights[idx] / totalWeight) * targetShrink
894
+ const whole = Math.min(shrinkable[idx], Math.floor(exact))
895
+ shrink[idx] = whole
896
+ fractions[idx] = exact - whole
897
+ usedShrink += whole
898
+ }
899
+
900
+ let remainingShrink = targetShrink - usedShrink
901
+
902
+ while (remainingShrink > 0) {
903
+ let bestIdx = -1
904
+ let bestFraction = -1
905
+
906
+ for (let idx = 0; idx < shrinkable.length; idx++) {
907
+ if (shrinkable[idx] - shrink[idx] <= 0) continue
908
+
909
+ if (
910
+ bestIdx === -1 ||
911
+ fractions[idx] > bestFraction ||
912
+ (fractions[idx] === bestFraction && shrinkable[idx] > shrinkable[bestIdx])
913
+ ) {
914
+ bestIdx = idx
915
+ bestFraction = fractions[idx]
916
+ }
917
+ }
918
+
919
+ if (bestIdx === -1) {
920
+ break
921
+ }
922
+
923
+ shrink[bestIdx] += 1
924
+ fractions[bestIdx] = 0
925
+ remainingShrink -= 1
926
+ }
927
+
928
+ return shrink
929
+ }
930
+
931
+ private computeRowHeights(columnWidths: number[]): number[] {
932
+ const horizontalPadding = this.getHorizontalCellPadding()
933
+ const verticalPadding = this.getVerticalCellPadding()
934
+ const rowHeights = new Array(this._rowCount).fill(1 + verticalPadding)
935
+
936
+ for (let rowIdx = 0; rowIdx < this._rowCount; rowIdx++) {
937
+ for (let colIdx = 0; colIdx < this._columnCount; colIdx++) {
938
+ const cell = this._cells[rowIdx]?.[colIdx]
939
+ if (!cell) continue
940
+
941
+ const width = Math.max(1, (columnWidths[colIdx] ?? 1) - horizontalPadding)
942
+ const measure = cell.textBufferView.measureForDimensions(width, MEASURE_HEIGHT)
943
+ const lineCount = Math.max(1, measure?.lineCount ?? 1)
944
+ rowHeights[rowIdx] = Math.max(rowHeights[rowIdx], lineCount + verticalPadding)
945
+ }
946
+ }
947
+
948
+ return rowHeights
949
+ }
950
+
951
+ private computeOffsets(
952
+ parts: number[],
953
+ startBoundary: boolean,
954
+ endBoundary: boolean,
955
+ includeInnerBoundaries: boolean,
956
+ innerGap: number = 0,
957
+ ): number[] {
958
+ const offsets: number[] = [startBoundary ? 0 : -1]
959
+ let cursor = offsets[0] ?? 0
960
+
961
+ for (let idx = 0; idx < parts.length; idx++) {
962
+ const size = parts[idx] ?? 1
963
+ const separatorAfter = idx < parts.length - 1 ? (includeInnerBoundaries ? 1 : innerGap) : endBoundary ? 1 : 0
964
+ cursor += size + separatorAfter
965
+ offsets.push(cursor)
966
+ }
967
+
968
+ return offsets
969
+ }
970
+
971
+ private getInterColumnGap(borderLayout: ResolvedTableBorderLayout): number {
972
+ if (borderLayout.innerVertical) {
973
+ return 0
974
+ }
975
+
976
+ return this._columnGap
977
+ }
978
+
979
+ private getTotalInterColumnGap(borderLayout: ResolvedTableBorderLayout): number {
980
+ return Math.max(0, this._columnCount - 1) * this.getInterColumnGap(borderLayout)
981
+ }
982
+
983
+ private applyLayoutToViews(layout: TextTableLayout): void {
984
+ const horizontalPadding = this.getHorizontalCellPadding()
985
+ const verticalPadding = this.getVerticalCellPadding()
986
+
987
+ for (let rowIdx = 0; rowIdx < this._rowCount; rowIdx++) {
988
+ for (let colIdx = 0; colIdx < this._columnCount; colIdx++) {
989
+ const cell = this._cells[rowIdx]?.[colIdx]
990
+ if (!cell) continue
991
+
992
+ const colWidth = layout.columnWidths[colIdx] ?? 1
993
+ const rowHeight = layout.rowHeights[rowIdx] ?? 1
994
+ const contentWidth = Math.max(1, colWidth - horizontalPadding)
995
+ const contentHeight = Math.max(1, rowHeight - verticalPadding)
996
+
997
+ if (this._wrapMode === "none") {
998
+ cell.textBufferView.setWrapWidth(null)
999
+ } else {
1000
+ cell.textBufferView.setWrapWidth(contentWidth)
1001
+ }
1002
+
1003
+ cell.textBufferView.setViewport(0, 0, contentWidth, contentHeight)
1004
+ }
1005
+ }
1006
+ }
1007
+
1008
+ private resolveBorderLayout(): ResolvedTableBorderLayout {
1009
+ return {
1010
+ left: this._outerBorder,
1011
+ right: this._outerBorder,
1012
+ top: this._outerBorder,
1013
+ bottom: this._outerBorder,
1014
+ innerVertical: this._border && this._columnCount > 1,
1015
+ innerHorizontal: this._border && this._rowCount > 1,
1016
+ }
1017
+ }
1018
+
1019
+ private getVerticalBorderCount(borderLayout: ResolvedTableBorderLayout): number {
1020
+ return (
1021
+ (borderLayout.left ? 1 : 0) +
1022
+ (borderLayout.right ? 1 : 0) +
1023
+ (borderLayout.innerVertical ? Math.max(0, this._columnCount - 1) : 0)
1024
+ )
1025
+ }
1026
+
1027
+ private getHorizontalBorderCount(borderLayout: ResolvedTableBorderLayout): number {
1028
+ return (
1029
+ (borderLayout.top ? 1 : 0) +
1030
+ (borderLayout.bottom ? 1 : 0) +
1031
+ (borderLayout.innerHorizontal ? Math.max(0, this._rowCount - 1) : 0)
1032
+ )
1033
+ }
1034
+
1035
+ private drawBorders(buffer: OptimizedBuffer): void {
1036
+ if (!this._showBorders) {
1037
+ return
1038
+ }
1039
+
1040
+ const borderLayout = this.resolveBorderLayout()
1041
+
1042
+ if (this.getVerticalBorderCount(borderLayout) === 0 && this.getHorizontalBorderCount(borderLayout) === 0) {
1043
+ return
1044
+ }
1045
+
1046
+ buffer.drawGrid({
1047
+ borderChars: BorderCharArrays[this._borderStyle],
1048
+ borderFg: this._borderColor,
1049
+ borderBg: this._borderBackgroundColor,
1050
+ columnOffsets: this._layout.columnOffsetsI32,
1051
+ rowOffsets: this._layout.rowOffsetsI32,
1052
+ drawInner: this._border,
1053
+ drawOuter: this._outerBorder,
1054
+ })
1055
+ }
1056
+
1057
+ private drawCells(buffer: OptimizedBuffer): void {
1058
+ this.drawCellRange(buffer, 0, this._rowCount - 1)
1059
+ }
1060
+
1061
+ private drawCellRange(buffer: OptimizedBuffer, firstRow: number, lastRow: number): void {
1062
+ const colOffsets = this._layout.columnOffsets
1063
+ const rowOffsets = this._layout.rowOffsets
1064
+ const cellPaddingX = this._cellPaddingX
1065
+ const cellPaddingY = this._cellPaddingY
1066
+
1067
+ for (let rowIdx = firstRow; rowIdx <= lastRow; rowIdx++) {
1068
+ const cellY = (rowOffsets[rowIdx] ?? 0) + 1 + cellPaddingY
1069
+
1070
+ for (let colIdx = 0; colIdx < this._columnCount; colIdx++) {
1071
+ const cell = this._cells[rowIdx]?.[colIdx]
1072
+ if (!cell) continue
1073
+ buffer.drawTextBuffer(cell.textBufferView, (colOffsets[colIdx] ?? 0) + 1 + cellPaddingX, cellY)
1074
+ }
1075
+ }
1076
+ }
1077
+
1078
+ private redrawSelectionRows(firstRow: number, lastRow: number): void {
1079
+ if (firstRow > lastRow) return
1080
+
1081
+ if (this._backgroundColor.a < 1) {
1082
+ this.invalidateRasterOnly()
1083
+ return
1084
+ }
1085
+
1086
+ const buffer = this.frameBuffer
1087
+ if (!buffer) return
1088
+
1089
+ this.clearCellRange(buffer, firstRow, lastRow)
1090
+ this.drawCellRange(buffer, firstRow, lastRow)
1091
+ this.requestRender()
1092
+ }
1093
+
1094
+ private clearCellRange(buffer: OptimizedBuffer, firstRow: number, lastRow: number): void {
1095
+ const colWidths = this._layout.columnWidths
1096
+ const rowHeights = this._layout.rowHeights
1097
+ const colOffsets = this._layout.columnOffsets
1098
+ const rowOffsets = this._layout.rowOffsets
1099
+
1100
+ for (let rowIdx = firstRow; rowIdx <= lastRow; rowIdx++) {
1101
+ const cellY = (rowOffsets[rowIdx] ?? 0) + 1
1102
+ const rowHeight = rowHeights[rowIdx] ?? 1
1103
+
1104
+ for (let colIdx = 0; colIdx < this._columnCount; colIdx++) {
1105
+ const cellX = (colOffsets[colIdx] ?? 0) + 1
1106
+ const colWidth = colWidths[colIdx] ?? 1
1107
+ if (this._backgroundColor.a < 1) {
1108
+ for (let y = cellY; y < cellY + rowHeight; y++) {
1109
+ for (let x = cellX; x < cellX + colWidth; x++) {
1110
+ buffer.setCell(x, y, " ", this._defaultFg, this._backgroundColor, this._defaultAttributes)
1111
+ }
1112
+ }
1113
+ } else {
1114
+ buffer.fillRect(cellX, cellY, colWidth, rowHeight, this._backgroundColor)
1115
+ }
1116
+ }
1117
+ }
1118
+ }
1119
+
1120
+ private ensureLayoutReady(): void {
1121
+ if (!this._layoutDirty) return
1122
+ this.rebuildLayoutForCurrentWidth()
1123
+ }
1124
+
1125
+ private getCellAtLocalPosition(localX: number, localY: number): CellPosition | null {
1126
+ if (this._rowCount === 0 || this._columnCount === 0) return null
1127
+ if (localX < 0 || localY < 0 || localX >= this._layout.tableWidth || localY >= this._layout.tableHeight) {
1128
+ return null
1129
+ }
1130
+
1131
+ let rowIdx = -1
1132
+ for (let idx = 0; idx < this._rowCount; idx++) {
1133
+ const top = (this._layout.rowOffsets[idx] ?? 0) + 1
1134
+ const bottom = top + (this._layout.rowHeights[idx] ?? 1) - 1
1135
+ if (localY >= top && localY <= bottom) {
1136
+ rowIdx = idx
1137
+ break
1138
+ }
1139
+ }
1140
+
1141
+ if (rowIdx < 0) return null
1142
+
1143
+ let colIdx = -1
1144
+ for (let idx = 0; idx < this._columnCount; idx++) {
1145
+ const left = (this._layout.columnOffsets[idx] ?? 0) + 1
1146
+ const right = left + (this._layout.columnWidths[idx] ?? 1) - 1
1147
+ if (localX >= left && localX <= right) {
1148
+ colIdx = idx
1149
+ break
1150
+ }
1151
+ }
1152
+
1153
+ if (colIdx < 0) return null
1154
+
1155
+ return { rowIdx, colIdx }
1156
+ }
1157
+
1158
+ private applySelectionToCells(localSelection: LocalSelectionBounds, isStart: boolean): void {
1159
+ if (localSelection.anchorX === localSelection.focusX && localSelection.anchorY === localSelection.focusY) {
1160
+ this.resetCellSelections()
1161
+ this._lastSelectionMode = null
1162
+ return
1163
+ }
1164
+
1165
+ const minSelY = Math.min(localSelection.anchorY, localSelection.focusY)
1166
+ const maxSelY = Math.max(localSelection.anchorY, localSelection.focusY)
1167
+
1168
+ const firstRow = this.findRowForLocalY(minSelY)
1169
+ const lastRow = this.findRowForLocalY(maxSelY)
1170
+ const selection = this.resolveSelectionResolution(localSelection)
1171
+ const modeChanged = this._lastSelectionMode !== selection.mode
1172
+ this._lastSelectionMode = selection.mode
1173
+ const lockToAnchorColumn = selection.mode === "column-locked" && selection.anchorColumn !== null
1174
+
1175
+ for (let rowIdx = 0; rowIdx < this._rowCount; rowIdx++) {
1176
+ if (rowIdx < firstRow || rowIdx > lastRow) {
1177
+ this.resetRowSelection(rowIdx)
1178
+ continue
1179
+ }
1180
+
1181
+ const cellTop = (this._layout.rowOffsets[rowIdx] ?? 0) + 1 + this._cellPaddingY
1182
+
1183
+ for (let colIdx = 0; colIdx < this._columnCount; colIdx++) {
1184
+ const cell = this._cells[rowIdx]?.[colIdx]
1185
+ if (!cell) continue
1186
+
1187
+ if (lockToAnchorColumn && colIdx !== selection.anchorColumn) {
1188
+ cell.textBufferView.resetLocalSelection()
1189
+ continue
1190
+ }
1191
+
1192
+ const cellLeft = (this._layout.columnOffsets[colIdx] ?? 0) + 1 + this._cellPaddingX
1193
+ let coords: CellSelectionCoords = {
1194
+ anchorX: localSelection.anchorX - cellLeft,
1195
+ anchorY: localSelection.anchorY - cellTop,
1196
+ focusX: localSelection.focusX - cellLeft,
1197
+ focusY: localSelection.focusY - cellTop,
1198
+ }
1199
+
1200
+ const isAnchorCell =
1201
+ selection.anchorCell !== null &&
1202
+ selection.anchorCell.rowIdx === rowIdx &&
1203
+ selection.anchorCell.colIdx === colIdx
1204
+
1205
+ if (selection.mode === "single-cell" && !isAnchorCell) {
1206
+ cell.textBufferView.resetLocalSelection()
1207
+ continue
1208
+ }
1209
+
1210
+ const forceSet = isAnchorCell && selection.mode !== "single-cell"
1211
+
1212
+ if (forceSet) {
1213
+ coords = this.getFullCellSelectionCoords(rowIdx, colIdx)
1214
+ }
1215
+
1216
+ const shouldUseSet = isStart || modeChanged || forceSet
1217
+
1218
+ if (shouldUseSet) {
1219
+ cell.textBufferView.setLocalSelection(
1220
+ coords.anchorX,
1221
+ coords.anchorY,
1222
+ coords.focusX,
1223
+ coords.focusY,
1224
+ this._selectionBg,
1225
+ this._selectionFg,
1226
+ )
1227
+ } else {
1228
+ cell.textBufferView.updateLocalSelection(
1229
+ coords.anchorX,
1230
+ coords.anchorY,
1231
+ coords.focusX,
1232
+ coords.focusY,
1233
+ this._selectionBg,
1234
+ this._selectionFg,
1235
+ )
1236
+ }
1237
+ }
1238
+ }
1239
+ }
1240
+
1241
+ private resolveSelectionResolution(localSelection: LocalSelectionBounds): SelectionResolution {
1242
+ const anchorCell = this.getCellAtLocalPosition(localSelection.anchorX, localSelection.anchorY)
1243
+ const focusCell = this.getCellAtLocalPosition(localSelection.focusX, localSelection.focusY)
1244
+ const anchorColumn = anchorCell?.colIdx ?? this.getColumnAtLocalX(localSelection.anchorX)
1245
+
1246
+ if (
1247
+ anchorCell !== null &&
1248
+ focusCell !== null &&
1249
+ anchorCell.rowIdx === focusCell.rowIdx &&
1250
+ anchorCell.colIdx === focusCell.colIdx
1251
+ ) {
1252
+ return {
1253
+ mode: "single-cell",
1254
+ anchorCell,
1255
+ anchorColumn,
1256
+ }
1257
+ }
1258
+
1259
+ const focusColumn = this.getColumnAtLocalX(localSelection.focusX)
1260
+ if (anchorColumn !== null && focusColumn === anchorColumn) {
1261
+ return {
1262
+ mode: "column-locked",
1263
+ anchorCell,
1264
+ anchorColumn,
1265
+ }
1266
+ }
1267
+
1268
+ return {
1269
+ mode: "grid",
1270
+ anchorCell,
1271
+ anchorColumn,
1272
+ }
1273
+ }
1274
+
1275
+ private getColumnAtLocalX(localX: number): number | null {
1276
+ if (this._columnCount === 0) return null
1277
+ if (localX < 0 || localX >= this._layout.tableWidth) return null
1278
+
1279
+ for (let colIdx = 0; colIdx < this._columnCount; colIdx++) {
1280
+ const colStart = (this._layout.columnOffsets[colIdx] ?? 0) + 1
1281
+ const colEnd = colStart + (this._layout.columnWidths[colIdx] ?? 1) - 1
1282
+ if (localX >= colStart && localX <= colEnd) {
1283
+ return colIdx
1284
+ }
1285
+ }
1286
+
1287
+ return null
1288
+ }
1289
+
1290
+ private getFullCellSelectionCoords(rowIdx: number, colIdx: number): CellSelectionCoords {
1291
+ const colWidth = this._layout.columnWidths[colIdx] ?? 1
1292
+ const rowHeight = this._layout.rowHeights[rowIdx] ?? 1
1293
+ const contentWidth = Math.max(1, colWidth - this.getHorizontalCellPadding())
1294
+ const contentHeight = Math.max(1, rowHeight - this.getVerticalCellPadding())
1295
+
1296
+ return {
1297
+ anchorX: -1,
1298
+ anchorY: 0,
1299
+ focusX: contentWidth,
1300
+ focusY: contentHeight,
1301
+ }
1302
+ }
1303
+
1304
+ private findRowForLocalY(localY: number): number {
1305
+ if (this._rowCount === 0) return 0
1306
+ if (localY < 0) return 0
1307
+
1308
+ for (let rowIdx = 0; rowIdx < this._rowCount; rowIdx++) {
1309
+ const rowStart = (this._layout.rowOffsets[rowIdx] ?? 0) + 1
1310
+ const rowEnd = rowStart + (this._layout.rowHeights[rowIdx] ?? 1) - 1
1311
+ if (localY <= rowEnd) return rowIdx
1312
+ }
1313
+
1314
+ return this._rowCount - 1
1315
+ }
1316
+
1317
+ private getSelectionRowRange(selection: LocalSelectionBounds | null): RowRange | null {
1318
+ if (!selection?.isActive || this._rowCount === 0) return null
1319
+
1320
+ const minSelY = Math.min(selection.anchorY, selection.focusY)
1321
+ const maxSelY = Math.max(selection.anchorY, selection.focusY)
1322
+
1323
+ return {
1324
+ firstRow: this.findRowForLocalY(minSelY),
1325
+ lastRow: this.findRowForLocalY(maxSelY),
1326
+ }
1327
+ }
1328
+
1329
+ private getDirtySelectionRowRange(
1330
+ previousSelection: LocalSelectionBounds | null,
1331
+ currentSelection: LocalSelectionBounds | null,
1332
+ ): RowRange | null {
1333
+ const previousRange = this.getSelectionRowRange(previousSelection)
1334
+ const currentRange = this.getSelectionRowRange(currentSelection)
1335
+
1336
+ if (previousRange === null) return currentRange
1337
+ if (currentRange === null) return previousRange
1338
+
1339
+ return {
1340
+ firstRow: Math.min(previousRange.firstRow, currentRange.firstRow),
1341
+ lastRow: Math.max(previousRange.lastRow, currentRange.lastRow),
1342
+ }
1343
+ }
1344
+
1345
+ private resetRowSelection(rowIdx: number): void {
1346
+ const row = this._cells[rowIdx]
1347
+ if (!row) return
1348
+
1349
+ for (const cell of row) {
1350
+ cell.textBufferView.resetLocalSelection()
1351
+ }
1352
+ }
1353
+
1354
+ private resetCellSelections(): void {
1355
+ for (let rowIdx = 0; rowIdx < this._rowCount; rowIdx++) {
1356
+ this.resetRowSelection(rowIdx)
1357
+ }
1358
+ }
1359
+
1360
+ private createEmptyLayout(): TextTableLayout {
1361
+ return {
1362
+ columnWidths: [],
1363
+ rowHeights: [],
1364
+ columnOffsets: [0],
1365
+ rowOffsets: [0],
1366
+ columnOffsetsI32: new Int32Array([0]),
1367
+ rowOffsetsI32: new Int32Array([0]),
1368
+ tableWidth: 0,
1369
+ tableHeight: 0,
1370
+ }
1371
+ }
1372
+
1373
+ private resolveLayoutWidthConstraint(width: number | undefined): number | undefined {
1374
+ if (width === undefined || !Number.isFinite(width) || width <= 0) {
1375
+ return undefined
1376
+ }
1377
+
1378
+ if (this._wrapMode !== "none" || this.isFullWidthMode()) {
1379
+ return Math.max(1, Math.floor(width))
1380
+ }
1381
+
1382
+ return undefined
1383
+ }
1384
+
1385
+ private getHorizontalCellPadding(): number {
1386
+ return this._cellPaddingX * 2
1387
+ }
1388
+
1389
+ private getVerticalCellPadding(): number {
1390
+ return this._cellPaddingY * 2
1391
+ }
1392
+
1393
+ private resolveColumnFitter(value: TextTableColumnFitter | undefined): TextTableColumnFitter {
1394
+ if (value === undefined) {
1395
+ return this._defaultOptions.columnFitter
1396
+ }
1397
+
1398
+ return value === "balanced" ? "balanced" : "proportional"
1399
+ }
1400
+
1401
+ private resolveCellPadding(value: number | undefined): number {
1402
+ if (value === undefined || !Number.isFinite(value)) {
1403
+ return this._defaultOptions.cellPadding
1404
+ }
1405
+
1406
+ return Math.max(0, Math.floor(value))
1407
+ }
1408
+
1409
+ private resolveColumnGap(value: number | undefined): number {
1410
+ if (value === undefined || !Number.isFinite(value)) {
1411
+ return this._defaultOptions.columnGap
1412
+ }
1413
+
1414
+ return Math.max(0, Math.floor(value))
1415
+ }
1416
+
1417
+ private invalidateLayoutAndRaster(markYogaDirty: boolean = true): void {
1418
+ this._layoutDirty = true
1419
+ this._rasterDirty = true
1420
+ this._cachedMeasureLayout = null
1421
+ this._cachedMeasureWidth = undefined
1422
+
1423
+ if (markYogaDirty) {
1424
+ this.yogaNode.markDirty()
1425
+ }
1426
+
1427
+ this.requestRender()
1428
+ }
1429
+
1430
+ private invalidateRasterOnly(): void {
1431
+ this._rasterDirty = true
1432
+ this.requestRender()
1433
+ }
1434
+ }