@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,700 @@
1
+ const std = @import("std");
2
+ const builtin = @import("builtin");
3
+
4
+ const SupportedZigVersion = struct {
5
+ major: u32,
6
+ minor: u32,
7
+ patch: u32,
8
+ };
9
+
10
+ const SUPPORTED_ZIG_VERSIONS = [_]SupportedZigVersion{
11
+ .{ .major = 0, .minor = 15, .patch = 2 },
12
+ };
13
+
14
+ const SupportedTarget = struct {
15
+ zig_target: []const u8,
16
+ output_name: []const u8,
17
+ description: []const u8,
18
+ };
19
+
20
+ // Unpinned linux-gnu targets can bind host libm symbols such as GLIBC_2.29.
21
+ const SUPPORTED_TARGETS = [_]SupportedTarget{
22
+ .{ .zig_target = "x86_64-linux-gnu.2.17", .output_name = "x86_64-linux", .description = "Linux x86_64" },
23
+ .{ .zig_target = "aarch64-linux-gnu.2.17", .output_name = "aarch64-linux", .description = "Linux aarch64" },
24
+ .{ .zig_target = "x86_64-linux-musl", .output_name = "x86_64-linux-musl", .description = "Linux x86_64 (musl)" },
25
+ .{ .zig_target = "aarch64-linux-musl", .output_name = "aarch64-linux-musl", .description = "Linux aarch64 (musl)" },
26
+ .{ .zig_target = "x86_64-macos", .output_name = "x86_64-macos", .description = "macOS x86_64 (Intel)" },
27
+ .{ .zig_target = "aarch64-macos", .output_name = "aarch64-macos", .description = "macOS aarch64 (Apple Silicon)" },
28
+ .{ .zig_target = "x86_64-windows-gnu", .output_name = "x86_64-windows", .description = "Windows x86_64" },
29
+ .{ .zig_target = "aarch64-windows-gnu", .output_name = "aarch64-windows", .description = "Windows aarch64" },
30
+ // ── XINCLI: Android/Termux support ─────────────────────────────────
31
+ // Targets Android (Bionic libc) on aarch64 — the only Android arch
32
+ // Termux realistically runs on in 2026. The NDK sysroot must be passed
33
+ // via --sysroot; see the `build-android` workflow for the exact flags.
34
+ // The .so produced here is loaded by @xincli/opentui-core-android-arm64
35
+ // at runtime inside cli.mjs on the user's phone.
36
+ .{ .zig_target = "aarch64-linux-android", .output_name = "aarch64-android", .description = "Android aarch64 (Termux)" },
37
+ .{ .zig_target = "arm-linux-android", .output_name = "arm-android", .description = "Android armv7 (legacy Termux)" },
38
+ .{ .zig_target = "x86_64-linux-android", .output_name = "x86_64-android", .description = "Android x86_64 (emulator)" },
39
+ };
40
+
41
+ const DEFAULT_MACOS_SDK_PATH = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk";
42
+
43
+ const LIB_NAME = "opentui";
44
+ const ROOT_SOURCE_FILE = "lib.zig";
45
+
46
+ const YOGA_CXX_FLAGS = [_][]const u8{
47
+ "-std=c++20",
48
+ "-fexceptions",
49
+ "-frtti",
50
+ };
51
+
52
+ const YOGA_CXX_SOURCES = [_][]const u8{
53
+ "yoga/YGConfig.cpp",
54
+ "yoga/YGEnums.cpp",
55
+ "yoga/YGNode.cpp",
56
+ "yoga/YGNodeLayout.cpp",
57
+ "yoga/YGNodeStyle.cpp",
58
+ "yoga/YGPixelGrid.cpp",
59
+ "yoga/YGValue.cpp",
60
+ "yoga/algorithm/AbsoluteLayout.cpp",
61
+ "yoga/algorithm/Baseline.cpp",
62
+ "yoga/algorithm/Cache.cpp",
63
+ "yoga/algorithm/CalculateLayout.cpp",
64
+ "yoga/algorithm/FlexLine.cpp",
65
+ "yoga/algorithm/PixelGrid.cpp",
66
+ "yoga/config/Config.cpp",
67
+ "yoga/debug/AssertFatal.cpp",
68
+ "yoga/debug/Log.cpp",
69
+ "yoga/event/event.cpp",
70
+ "yoga/node/LayoutResults.cpp",
71
+ "yoga/node/Node.cpp",
72
+ };
73
+
74
+ fn nativeExecutableTarget(b: *std.Build) std.Build.ResolvedTarget {
75
+ if (builtin.os.tag != .linux) {
76
+ return b.resolveTargetQuery(.{});
77
+ }
78
+
79
+ // ── XINCLI: On Android/Termux, use the host target directly (Bionic).
80
+ // The musl override below breaks on Android because the host is android
81
+ // ABI, and forcing musl creates an unresolvable target.
82
+ if (builtin.abi == .android) {
83
+ return b.resolveTargetQuery(.{});
84
+ }
85
+
86
+ // Zig 0.15.2's ELF linker currently fails on newer glibc startup objects
87
+ // that ship .sframe relocations. Keep shipped libraries on linux-gnu, but
88
+ // use musl for local native executables so test/debug/bench still work.
89
+ var query = b.graph.host.query;
90
+ query.abi = .musl;
91
+ query.glibc_version = null;
92
+ return b.resolveTargetQuery(query);
93
+ }
94
+
95
+ fn pathExists(path: []const u8) bool {
96
+ if (path.len == 0) return false;
97
+ std.fs.cwd().access(path, .{}) catch return false;
98
+ return true;
99
+ }
100
+
101
+ fn isMacOSSDKPath(path: []const u8) bool {
102
+ const trimmed_path = std.mem.trimRight(u8, path, "/");
103
+ if (trimmed_path.len == 0) return false;
104
+
105
+ const base_name = std.fs.path.basename(trimmed_path);
106
+ return std.mem.startsWith(u8, base_name, "MacOSX") and std.mem.endsWith(u8, base_name, ".sdk");
107
+ }
108
+
109
+ fn macOSSDKHasFramework(b: *std.Build, sdk_path: []const u8, framework: []const u8) bool {
110
+ return pathExists(b.pathJoin(&.{ sdk_path, "System", "Library", "Frameworks", b.fmt("{s}.framework", .{framework}) }));
111
+ }
112
+
113
+ fn isMacOSSDKAvailable(b: *std.Build, sdk_path: []const u8) bool {
114
+ return isMacOSSDKPath(sdk_path) and
115
+ pathExists(b.pathJoin(&.{ sdk_path, "usr", "lib" })) and
116
+ macOSSDKHasFramework(b, sdk_path, "CoreFoundation") and
117
+ macOSSDKHasFramework(b, sdk_path, "CoreAudio") and
118
+ macOSSDKHasFramework(b, sdk_path, "AudioToolbox");
119
+ }
120
+
121
+ fn resolveMacOSSDKPath(b: *std.Build) ?[]const u8 {
122
+ if (b.option([]const u8, "macos-sdk", "Path to a macOS SDK for CoreAudio headers and framework linking")) |sdk_path| {
123
+ if (isMacOSSDKAvailable(b, sdk_path)) return sdk_path;
124
+ std.debug.print("macOS SDK path '{s}' must be a MacOSX*.sdk with the required frameworks\n", .{sdk_path});
125
+ return null;
126
+ }
127
+
128
+ const env_vars = [_][]const u8{ "SDKROOT", "MACOS_SDK_PATH", "MACOSX_SDK_PATH" };
129
+ for (env_vars) |env_var| {
130
+ if (b.graph.env_map.get(env_var)) |sdk_path| {
131
+ if (isMacOSSDKAvailable(b, sdk_path)) return sdk_path;
132
+ }
133
+ }
134
+
135
+ if (builtin.os.tag == .macos and std.zig.system.darwin.isSdkInstalled(b.allocator)) {
136
+ const sdk_target = b.resolveTargetQuery(.{ .cpu_arch = .aarch64, .os_tag = .macos });
137
+ if (std.zig.system.darwin.getSdk(b.allocator, &sdk_target.result)) |sdk_path| {
138
+ if (isMacOSSDKAvailable(b, sdk_path)) return sdk_path;
139
+ }
140
+ }
141
+
142
+ if (isMacOSSDKAvailable(b, DEFAULT_MACOS_SDK_PATH)) return DEFAULT_MACOS_SDK_PATH;
143
+ return null;
144
+ }
145
+
146
+ fn printMissingMacOSSDK(target_description: []const u8) void {
147
+ std.debug.print(
148
+ "macOS SDK not found for {s}. Set SDKROOT, MACOS_SDK_PATH, or -Dmacos-sdk=/path/to/MacOSX.sdk.\n",
149
+ .{target_description},
150
+ );
151
+ }
152
+
153
+ fn addMiniaudioShim(
154
+ b: *std.Build,
155
+ artifact: *std.Build.Step.Compile,
156
+ target: std.Build.ResolvedTarget,
157
+ macos_sdk_path: ?[]const u8,
158
+ ) void {
159
+ const c_flags: []const []const u8 = switch (target.result.os.tag) {
160
+ .macos => blk: {
161
+ const flags = b.allocator.alloc([]const u8, 4) catch @panic("OOM");
162
+ flags[0] = "-std=c99";
163
+ flags[1] = "-DMA_NO_RUNTIME_LINKING";
164
+ flags[2] = "-isysroot";
165
+ flags[3] = macos_sdk_path.?;
166
+ break :blk flags;
167
+ },
168
+ else => &.{"-std=c99"},
169
+ };
170
+
171
+ artifact.addIncludePath(b.path("."));
172
+ // ── XINCLI: skip linkLibC() for android ───────────────────────────
173
+ // linkLibC() emits -lm -lc -ldl that Zig tries to resolve via its own
174
+ // libc resolution. On Termux/Android, Zig can't find Bionic properly
175
+ // (it falls back to glibc defaults), so -lm -lc -ldl fail.
176
+ // For android, we link the .so files directly via addObjectFile in
177
+ // buildTarget() instead. For all other targets, linkLibC() works fine.
178
+ if (target.result.abi != .android) {
179
+ artifact.linkLibC();
180
+ }
181
+ artifact.addCSourceFile(.{
182
+ .file = b.path("miniaudio_shim.c"),
183
+ .flags = c_flags,
184
+ });
185
+ }
186
+
187
+ fn addMacOSSDKSearchPaths(b: *std.Build, artifact: *std.Build.Step.Compile, sdk_path: []const u8) void {
188
+ const include_path = b.pathJoin(&.{ sdk_path, "usr", "include" });
189
+ const framework_path = b.pathJoin(&.{ sdk_path, "System", "Library", "Frameworks" });
190
+ const lib_path = b.pathJoin(&.{ sdk_path, "usr", "lib" });
191
+
192
+ artifact.addSystemIncludePath(.{ .cwd_relative = include_path });
193
+ artifact.addSystemFrameworkPath(.{ .cwd_relative = framework_path });
194
+ artifact.addFrameworkPath(.{ .cwd_relative = framework_path });
195
+ artifact.addLibraryPath(.{ .cwd_relative = lib_path });
196
+ }
197
+
198
+ fn addMacOSSystemLibraries(b: *std.Build, artifact: *std.Build.Step.Compile, sdk_path: []const u8) void {
199
+ artifact.linkFramework("CoreFoundation");
200
+ artifact.linkFramework("CoreAudio");
201
+ artifact.linkFramework("AudioToolbox");
202
+ artifact.linkSystemLibrary("pthread");
203
+ addMacOSSDKSearchPaths(b, artifact, sdk_path);
204
+ }
205
+
206
+ fn addNativeAudioDependencies(
207
+ b: *std.Build,
208
+ artifact: *std.Build.Step.Compile,
209
+ target: std.Build.ResolvedTarget,
210
+ macos_sdk_path: ?[]const u8,
211
+ ) void {
212
+ addMiniaudioShim(b, artifact, target, macos_sdk_path);
213
+
214
+ switch (target.result.os.tag) {
215
+ .macos => addMacOSSystemLibraries(b, artifact, macos_sdk_path.?),
216
+ .linux => {
217
+ // ── XINCLI: Android is linux + .android ABI in Zig, not a
218
+ // separate OS tag. For Android, link OpenSLES directly by path
219
+ // instead of using linkSystemLibrary — the system library
220
+ // resolution fails because --sysroot makes Zig double the
221
+ // addLibraryPath paths. addObjectFile bypasses the search
222
+ // entirely and links the .so directly.
223
+ if (target.result.abi == .android) {
224
+ if (std.posix.getenv("XINCLI_ANDROID_LIB_PATH")) |lib_path| {
225
+ const opensles_path = b.pathJoin(&.{ lib_path, "libOpenSLES.so" });
226
+ artifact.addObjectFile(.{ .cwd_relative = opensles_path });
227
+ } else {
228
+ // Fallback: try linkSystemLibrary (may fail without paths)
229
+ artifact.linkSystemLibrary("OpenSLES");
230
+ }
231
+ } else {
232
+ artifact.linkSystemLibrary("dl");
233
+ artifact.linkSystemLibrary("pthread");
234
+ }
235
+ },
236
+ else => {},
237
+ }
238
+ }
239
+
240
+ fn addYogaDependencies(
241
+ b: *std.Build,
242
+ artifact: *std.Build.Step.Compile,
243
+ target: std.Build.ResolvedTarget,
244
+ ) void {
245
+ const yoga_dep = b.dependency("yoga", .{});
246
+
247
+ // ── XINCLI: skip linkLibCpp() for android ─────────────────────────
248
+ // linkLibCpp() emits -lc++ which fails on Termux (only libc++_shared.so
249
+ // exists, not libc++.so). For android, we link libc++_shared.so directly
250
+ // via addObjectFile in buildTarget(). For other targets, linkLibCpp() works.
251
+ if (target.result.abi != .android) {
252
+ artifact.linkLibCpp();
253
+ }
254
+
255
+ // ── XINCLI: add libc++ include path for android C++ compilation ────
256
+ // Yoga's C++ files #include <type_traits>, <cstddef>, <cmath>, etc.
257
+ // from the C++ standard library. linkLibCpp() normally adds libc++'s
258
+ // include path, but we skipped it for android.
259
+ //
260
+ // Also: add a math.h wrapper dir FIRST in the search path. The wrapper
261
+ // #includes the real Bionic math.h then #undefs isinf/isnan/fabs/abs
262
+ // macros that break std::isinf in C++ context.
263
+ if (target.result.abi == .android) {
264
+ // libc++ headers — use addIncludePath (-I) NOT addSystemIncludePath
265
+ // (-isystem) so C++ Standard Library headers are searched BEFORE
266
+ // the C Standard Library. <cerrno> needs libc++'s <errno.h> wrapper
267
+ // to be found before the C <errno.h>, otherwise it errors out.
268
+ if (std.posix.getenv("XINCLI_ANDROID_LIBCXX_INCLUDE")) |cxx_inc| {
269
+ artifact.addIncludePath(.{ .cwd_relative = cxx_inc });
270
+ }
271
+ if (std.posix.getenv("XINCLI_ANDROID_LIBCXX_INCLUDE2")) |cxx_inc2| {
272
+ artifact.addIncludePath(.{ .cwd_relative = cxx_inc2 });
273
+ }
274
+ }
275
+
276
+ artifact.addIncludePath(yoga_dep.path(""));
277
+
278
+ // ── XINCLI: Android C++ flags ──────────────────────────────────────
279
+ // Bionic's math.h macros are handled by patching yoga's source (sed
280
+ // replaces std::isinf → __builtin_isinf etc.) in build-native-termux.sh.
281
+ //
282
+ // CRITICAL: Use -nostdinc++ to skip Zig's bundled libc++ headers (which
283
+ // use __ndk1 namespace on android). Instead, use Termux's libc++ headers
284
+ // (at $PREFIX/include/c++/v1/) which use __1 namespace — matching
285
+ // Termux's libc++_shared.so that we link against.
286
+ if (target.result.abi == .android) {
287
+ const android_cxx_flags = [_][]const u8{
288
+ "-std=c++20",
289
+ "-fexceptions",
290
+ "-frtti",
291
+ };
292
+ artifact.addCSourceFiles(.{
293
+ .root = yoga_dep.path(""),
294
+ .files = &YOGA_CXX_SOURCES,
295
+ .flags = &android_cxx_flags,
296
+ });
297
+ } else {
298
+ artifact.addCSourceFiles(.{
299
+ .root = yoga_dep.path(""),
300
+ .files = &YOGA_CXX_SOURCES,
301
+ .flags = &YOGA_CXX_FLAGS,
302
+ });
303
+ }
304
+ }
305
+
306
+ /// Apply dependencies to a module
307
+ fn applyDependencies(
308
+ b: *std.Build,
309
+ module: *std.Build.Module,
310
+ optimize: std.builtin.OptimizeMode,
311
+ target: std.Build.ResolvedTarget,
312
+ build_options: *std.Build.Step.Options,
313
+ ) void {
314
+ module.addOptions("build_options", build_options);
315
+
316
+ // ── XINCLI: Android uucode stub ────────────────────────────────────
317
+ // uucode_build_tables is a build-time native executable that fails to
318
+ // link on Termux. The stub provides neutral/default return values.
319
+ // Grapheme breaking won't work for complex Unicode, but the core
320
+ // renderer compiles, links, and LOADS successfully via dlopen.
321
+ if (target.result.abi == .android) {
322
+ const uucode_stub = b.createModule(.{
323
+ .root_source_file = b.path("uucode-stub.zig"),
324
+ });
325
+ module.addImport("uucode", uucode_stub);
326
+ return;
327
+ }
328
+
329
+ if (b.lazyDependency("uucode", .{
330
+ .target = target,
331
+ .optimize = optimize,
332
+ .fields = @as([]const []const u8, &.{
333
+ "grapheme_break",
334
+ "east_asian_width",
335
+ "general_category",
336
+ "is_emoji_presentation",
337
+ }),
338
+ })) |uucode_dep| {
339
+ module.addImport("uucode", uucode_dep.module("uucode"));
340
+ }
341
+ }
342
+
343
+ fn checkZigVersion() void {
344
+ const current_version = builtin.zig_version;
345
+ var is_supported = false;
346
+
347
+ for (SUPPORTED_ZIG_VERSIONS) |supported| {
348
+ if (current_version.major == supported.major and
349
+ current_version.minor == supported.minor and
350
+ current_version.patch == supported.patch)
351
+ {
352
+ is_supported = true;
353
+ break;
354
+ }
355
+ }
356
+
357
+ if (!is_supported) {
358
+ std.debug.print("\x1b[31mError: Unsupported Zig version {}.{}.{}\x1b[0m\n", .{
359
+ current_version.major,
360
+ current_version.minor,
361
+ current_version.patch,
362
+ });
363
+ std.debug.print("Supported Zig versions:\n", .{});
364
+ for (SUPPORTED_ZIG_VERSIONS) |supported| {
365
+ std.debug.print(" - {}.{}.{}\n", .{
366
+ supported.major,
367
+ supported.minor,
368
+ supported.patch,
369
+ });
370
+ }
371
+ std.debug.print("\nPlease install a supported Zig version to continue.\n", .{});
372
+ std.process.exit(1);
373
+ }
374
+ }
375
+
376
+ pub fn build(b: *std.Build) void {
377
+ checkZigVersion();
378
+
379
+ const optimize = b.standardOptimizeOption(.{});
380
+ const bench_optimize = b.option(std.builtin.OptimizeMode, "bench-optimize", "Optimize mode for benchmarks") orelse .ReleaseFast;
381
+ const debug_use_llvm = b.option(bool, "debug-llvm", "Use LLVM backend for debug/test artifacts");
382
+ const target_option = b.option([]const u8, "target", "Build for specific target (e.g., 'x86_64-linux-gnu.2.17').");
383
+ const build_all = b.option(bool, "all", "Build for all supported targets") orelse false;
384
+ const gpa_safe_stats = b.option(bool, "gpa-safe-stats", "Enable GPA safety checks for trustworthy allocator stats") orelse false;
385
+ const macos_sdk_path = resolveMacOSSDKPath(b);
386
+ const build_options = b.addOptions();
387
+ build_options.addOption(bool, "gpa_safe_stats", gpa_safe_stats);
388
+
389
+ if (target_option) |target_str| {
390
+ // Build single target
391
+ buildSingleTarget(b, target_str, optimize, build_options, macos_sdk_path) catch |err| {
392
+ std.debug.print("Error building target '{s}': {}\n", .{ target_str, err });
393
+ std.process.exit(1);
394
+ };
395
+ } else if (build_all) {
396
+ // Build all supported targets
397
+ buildAllTargets(b, optimize, build_options, macos_sdk_path) catch |err| {
398
+ std.debug.print("Error building all targets: {}\n", .{err});
399
+ std.process.exit(1);
400
+ };
401
+ } else {
402
+ // Build for native target only (default)
403
+ buildNativeTarget(b, optimize, build_options, macos_sdk_path) catch |err| {
404
+ std.debug.print("Error building native target: {}\n", .{err});
405
+ std.process.exit(1);
406
+ };
407
+ }
408
+
409
+ // Test step (native only)
410
+ const test_step = b.step("test", "Run unit tests");
411
+ const native_target = nativeExecutableTarget(b);
412
+ const test_mod = b.createModule(.{
413
+ .root_source_file = b.path("test.zig"),
414
+ .target = native_target,
415
+ .optimize = .Debug,
416
+ });
417
+ applyDependencies(b, test_mod, .Debug, native_target, build_options);
418
+ const test_artifact = b.addTest(.{
419
+ .root_module = test_mod,
420
+ .filters = if (b.option([]const u8, "test-filter", "Skip tests that do not match filter")) |f| &.{f} else &.{},
421
+ .use_llvm = debug_use_llvm,
422
+ });
423
+
424
+ if (native_target.result.os.tag == .macos and macos_sdk_path == null) {
425
+ printMissingMacOSSDK("native macOS tests");
426
+ std.process.exit(1);
427
+ }
428
+ addNativeAudioDependencies(b, test_artifact, native_target, macos_sdk_path);
429
+ addYogaDependencies(b, test_artifact, native_target);
430
+
431
+ const run_test = b.addRunArtifact(test_artifact);
432
+ test_step.dependOn(&run_test.step);
433
+
434
+ // Bench step (native only)
435
+ const bench_step = b.step("bench", "Run benchmarks");
436
+ const bench_mod = b.createModule(.{
437
+ .root_source_file = b.path("bench.zig"),
438
+ .target = native_target,
439
+ .optimize = bench_optimize,
440
+ });
441
+ applyDependencies(b, bench_mod, bench_optimize, native_target, build_options);
442
+ const bench_exe = b.addExecutable(.{
443
+ .name = "opentui-bench",
444
+ .root_module = bench_mod,
445
+ });
446
+ const run_bench = b.addRunArtifact(bench_exe);
447
+ if (b.args) |args| {
448
+ run_bench.addArgs(args);
449
+ }
450
+ bench_step.dependOn(&run_bench.step);
451
+
452
+ const bench_ffi_step = b.step("bench-ffi", "Build NativeSpanFeed benchmark library");
453
+ const bench_ffi_mod = b.createModule(.{
454
+ .root_source_file = b.path("native-span-feed-bench-lib.zig"),
455
+ .target = native_target,
456
+ .optimize = bench_optimize,
457
+ });
458
+ applyDependencies(b, bench_ffi_mod, bench_optimize, native_target, build_options);
459
+ const bench_ffi_lib = b.addLibrary(.{
460
+ .name = "native_span_feed_bench",
461
+ .root_module = bench_ffi_mod,
462
+ .linkage = .dynamic,
463
+ });
464
+ if (native_target.result.os.tag == .macos and macos_sdk_path == null) {
465
+ printMissingMacOSSDK("native macOS benchmark FFI library");
466
+ std.process.exit(1);
467
+ }
468
+ addNativeAudioDependencies(b, bench_ffi_lib, native_target, macos_sdk_path);
469
+ addYogaDependencies(b, bench_ffi_lib, native_target);
470
+ const install_bench_ffi = b.addInstallArtifact(bench_ffi_lib, .{});
471
+ bench_ffi_step.dependOn(&install_bench_ffi.step);
472
+ bench_step.dependOn(bench_ffi_step);
473
+
474
+ // Debug step (native only)
475
+ const debug_step = b.step("debug", "Run debug executable");
476
+ const debug_mod = b.createModule(.{
477
+ .root_source_file = b.path("debug-view.zig"),
478
+ .target = native_target,
479
+ .optimize = .Debug,
480
+ });
481
+ applyDependencies(b, debug_mod, .Debug, native_target, build_options);
482
+ const debug_exe = b.addExecutable(.{
483
+ .name = "opentui-debug",
484
+ .root_module = debug_mod,
485
+ .use_llvm = debug_use_llvm,
486
+ });
487
+ const run_debug = b.addRunArtifact(debug_exe);
488
+ debug_step.dependOn(&run_debug.step);
489
+ }
490
+
491
+ fn buildAllTargets(
492
+ b: *std.Build,
493
+ optimize: std.builtin.OptimizeMode,
494
+ build_options: *std.Build.Step.Options,
495
+ macos_sdk_path: ?[]const u8,
496
+ ) !void {
497
+ for (SUPPORTED_TARGETS) |supported_target| {
498
+ try buildTarget(
499
+ b,
500
+ supported_target.zig_target,
501
+ supported_target.output_name,
502
+ supported_target.description,
503
+ optimize,
504
+ build_options,
505
+ macos_sdk_path,
506
+ );
507
+ }
508
+ }
509
+
510
+ fn buildNativeTarget(
511
+ b: *std.Build,
512
+ optimize: std.builtin.OptimizeMode,
513
+ build_options: *std.Build.Step.Options,
514
+ macos_sdk_path: ?[]const u8,
515
+ ) !void {
516
+ // Find the matching supported target for the native platform
517
+ const native_arch = @tagName(builtin.cpu.arch);
518
+ const native_os = @tagName(builtin.os.tag);
519
+
520
+ for (SUPPORTED_TARGETS) |supported_target| {
521
+ // Check if this target matches the native platform
522
+ if (std.mem.indexOf(u8, supported_target.zig_target, native_arch) != null and
523
+ std.mem.indexOf(u8, supported_target.zig_target, native_os) != null)
524
+ {
525
+ try buildTarget(
526
+ b,
527
+ supported_target.zig_target,
528
+ supported_target.output_name,
529
+ supported_target.description,
530
+ optimize,
531
+ build_options,
532
+ macos_sdk_path,
533
+ );
534
+ return;
535
+ }
536
+ }
537
+
538
+ std.debug.print("No matching supported target for native platform ({s}-{s})\n", .{ native_arch, native_os });
539
+ return error.UnsupportedNativeTarget;
540
+ }
541
+
542
+ fn buildSingleTarget(
543
+ b: *std.Build,
544
+ target_str: []const u8,
545
+ optimize: std.builtin.OptimizeMode,
546
+ build_options: *std.Build.Step.Options,
547
+ macos_sdk_path: ?[]const u8,
548
+ ) !void {
549
+ // Check if it matches a known target, use its output_name
550
+ for (SUPPORTED_TARGETS) |supported_target| {
551
+ if (std.mem.eql(u8, target_str, supported_target.zig_target)) {
552
+ try buildTarget(
553
+ b,
554
+ supported_target.zig_target,
555
+ supported_target.output_name,
556
+ supported_target.description,
557
+ optimize,
558
+ build_options,
559
+ macos_sdk_path,
560
+ );
561
+ return;
562
+ }
563
+ }
564
+ // Custom target - use target string as output name
565
+ const description = try std.fmt.allocPrint(b.allocator, "Custom target: {s}", .{target_str});
566
+ try buildTarget(b, target_str, target_str, description, optimize, build_options, macos_sdk_path);
567
+ }
568
+
569
+ fn buildTarget(
570
+ b: *std.Build,
571
+ zig_target: []const u8,
572
+ output_name: []const u8,
573
+ description: []const u8,
574
+ optimize: std.builtin.OptimizeMode,
575
+ build_options: *std.Build.Step.Options,
576
+ macos_sdk_path: ?[]const u8,
577
+ ) !void {
578
+ const target_query = try std.Target.Query.parse(.{ .arch_os_abi = zig_target });
579
+ const target = b.resolveTargetQuery(target_query);
580
+
581
+ if (target.result.os.tag == .macos and macos_sdk_path == null) {
582
+ printMissingMacOSSDK(description);
583
+ return error.MissingMacOSSDK;
584
+ }
585
+
586
+ const module = b.createModule(.{
587
+ .root_source_file = b.path(ROOT_SOURCE_FILE),
588
+ .target = target,
589
+ .optimize = optimize,
590
+ // ── XINCLI: link_libc=false for android ─────────────────────────
591
+ // We set link_libc=true before to make std.heap (free/malloc) compile.
592
+ // But that ALSO makes Zig emit -lc -lm -ldl → NEEDED: libc.so etc.
593
+ // → TLS crash on dlopen.
594
+ //
595
+ // Set link_libc=false. std.heap won't compile with "C allocator only
596
+ // available when linking against libc" — BUT we can work around that
597
+ // by providing the extern declarations ourselves in a shim module.
598
+ // OR: just accept that std.heap won't work and use a custom allocator.
599
+ //
600
+ // Actually, the simplest: set link_libc=false but add -lc -lm -ldl
601
+ // as --allow-shlib-undefined (no NEEDED, just allow undefined symbols).
602
+ // Zig's default for shared libs is to allow undefined symbols.
603
+ .link_libc = target.result.abi == .android,
604
+ // ── XINCLI: Don't let Zig link its own libc++ runtime ────────────
605
+ // Zig bundles pre-compiled libc++ bitcode (in --zig-lib-dir/libcxx/)
606
+ // that uses __ndk1 namespace on android. When Zig sees C++ code
607
+ // (yoga's .cpp files), it automatically links this runtime — producing
608
+ // __ndk1 mangled symbols that don't exist in Termux's __1 libc++.
609
+ //
610
+ // Set link_libcpp=false to stop Zig from linking its own libc++.
611
+ // We link Termux's libc++_shared.so ourselves via addObjectFile.
612
+ .link_libcpp = target.result.abi != .android,
613
+ });
614
+
615
+ // ── XINCLI: @cImport include path for android ──────────────────────
616
+ // We skipped linkLibC() for android (it emits -lm -lc -ldl that fail).
617
+ // The libc file's include_dir points at Termux's real $PREFIX/include
618
+ // (set by build-native-termux.sh). @cImport reads it from there.
619
+ //
620
+ // The arch-specific asm/ headers live at $PREFIX/include/aarch64-linux-android/
621
+ // which isn't in the default search path. Add it via addSystemIncludePath
622
+ // so @cImport finds <asm/sigcontext.h> etc. This ONLY affects @cImport
623
+ // (C) — C++ compilation doesn't use module include paths for system headers.
624
+ if (target.result.abi == .android) {
625
+ if (std.posix.getenv("XINCLI_ANDROID_ASM_INCLUDE")) |asm_inc| {
626
+ module.addSystemIncludePath(.{ .cwd_relative = asm_inc });
627
+ }
628
+ }
629
+
630
+ applyDependencies(b, module, optimize, target, build_options);
631
+
632
+ const lib = b.addLibrary(.{
633
+ .name = LIB_NAME,
634
+ .root_module = module,
635
+ .linkage = .dynamic,
636
+ });
637
+
638
+ // No special flags needed — Zig bundles compiler_rt (ARM atomics)
639
+
640
+ // ── XINCLI: Android native build (Termux) ───────────────────────────
641
+ // On Termux, Zig's --sysroot flag makes ld.lld look at <sysroot>/usr/lib/
642
+ // which doesn't exist (Termux's libs are at <sysroot>/lib/). We add the
643
+ // real lib paths explicitly via addLibraryPath, reading them from the
644
+ // XINCLI_ANDROID_LIB_SEARCH_PATHS env var (colon-separated) set by
645
+ // build-native-termux.sh.
646
+ //
647
+ // This is done in build.zig (not via LDFLAGS) because Zig's ld.lld
648
+ // doesn't respect the LDFLAGS env var — only flags passed through the
649
+ // build system reach the linker.
650
+
651
+ addNativeAudioDependencies(b, lib, target, macos_sdk_path);
652
+ addYogaDependencies(b, lib, target);
653
+
654
+ // Add Termux lib search paths for the linker
655
+ if (target.result.abi == .android) {
656
+ if (std.posix.getenv("XINCLI_ANDROID_LIB_SEARCH_PATHS")) |paths| {
657
+ var it = std.mem.splitScalar(u8, paths, ':');
658
+ while (it.next()) |path| {
659
+ if (path.len > 0) {
660
+ lib.addLibraryPath(.{ .cwd_relative = path });
661
+ }
662
+ }
663
+ }
664
+
665
+ // ── XINCLI: Set RPATH — $PREFIX/lib FIRST, then /system/lib64 ───
666
+ // Order matters! $PREFIX/lib must come FIRST so the linker finds:
667
+ // - Termux's libc++_shared.so (has __ndk1 symbols)
668
+ // Before:
669
+ // - System's libc++_shared.so (does NOT have __ndk1 symbols)
670
+ //
671
+ // /system/lib64 is still searched for Bionic libc/libm/libdl —
672
+ // those don't exist in $PREFIX/lib (well, they do now from our
673
+ // dd copies, but the system versions are already-loaded so no TLS crash).
674
+ if (std.posix.getenv("XINCLI_ANDROID_LIB_PATH")) |termux_lib| {
675
+ lib.addRPath(.{ .cwd_relative = termux_lib });
676
+ }
677
+ lib.addRPath(.{ .cwd_relative = "/system/lib64" });
678
+
679
+ // Link Termux's libc++_shared.so — creates NEEDED: libc++_shared.so.
680
+ // RUNPATH includes $PREFIX/lib so the linker finds Termux's version
681
+ // (which has __ndk1 symbols + __gxx_personality_v0).
682
+ if (std.posix.getenv("XINCLI_ANDROID_LIBCXX_PATH")) |cxx_path| {
683
+ lib.addObjectFile(.{ .cwd_relative = cxx_path });
684
+ }
685
+ }
686
+
687
+ const install_dir = b.addInstallArtifact(lib, .{
688
+ .dest_dir = .{
689
+ .override = .{
690
+ .custom = try std.fmt.allocPrint(b.allocator, "../lib/{s}", .{output_name}),
691
+ },
692
+ },
693
+ });
694
+
695
+ const build_step_name = try std.fmt.allocPrint(b.allocator, "build-{s}", .{output_name});
696
+ const build_step = b.step(build_step_name, try std.fmt.allocPrint(b.allocator, "Build for {s}", .{description}));
697
+ build_step.dependOn(&install_dir.step);
698
+
699
+ b.getInstallStep().dependOn(&install_dir.step);
700
+ }