xonecas.io
70160
Ce

cel-tui / commit 19e5301

feat(components): add streaming syntax highlighter

xonecas2026-04-10 19e5301f77ee9f3f3fbfb29c28de8105936d35f8 parent b4d0c5b

6 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

bun.lock+96−4
@@ -21,6 +21,8 @@
2121 "dependencies": {
2222 "@cel-tui/core": "workspace:*",
2323 "@cel-tui/types": "workspace:*",
24+ "shiki": "^4.0.2",
25+ "shiki-stream": "^0.1.4",
2426 "yoctomarkdown": "^0.1.0",
2527 },
2628 "peerDependencies": {
@@ -73,13 +75,19 @@
7375
7476 "@gerrit0/mini-shiki": ["@gerrit0/mini-shiki@3.23.0", "", { "dependencies": { "@shikijs/engine-oniguruma": "^3.23.0", "@shikijs/langs": "^3.23.0", "@shikijs/themes": "^3.23.0", "@shikijs/types": "^3.23.0", "@shikijs/vscode-textmate": "^10.0.2" } }, "sha512-bEMORlG0cqdjVyCEuU0cDQbORWX+kYCeo0kV1lbxF5bt4r7SID2l9bqsxJEM0zndaxpOUT7riCyIVEuqq/Ynxg=="],
7577
76- "@shikijs/engine-oniguruma": ["@shikijs/engine-oniguruma@3.23.0", "", { "dependencies": { "@shikijs/types": "3.23.0", "@shikijs/vscode-textmate": "^10.0.2" } }, "sha512-1nWINwKXxKKLqPibT5f4pAFLej9oZzQTsby8942OTlsJzOBZ0MWKiwzMsd+jhzu8YPCHAswGnnN1YtQfirL35g=="],
78+ "@shikijs/core": ["@shikijs/core@4.0.2", "", { "dependencies": { "@shikijs/primitive": "4.0.2", "@shikijs/types": "4.0.2", "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4", "hast-util-to-html": "^9.0.5" } }, "sha512-hxT0YF4ExEqB8G/qFdtJvpmHXBYJ2lWW7qTHDarVkIudPFE6iCIrqdgWxGn5s+ppkGXI0aEGlibI0PAyzP3zlw=="],
7779
78- "@shikijs/langs": ["@shikijs/langs@3.23.0", "", { "dependencies": { "@shikijs/types": "3.23.0" } }, "sha512-2Ep4W3Re5aB1/62RSYQInK9mM3HsLeB91cHqznAJMuylqjzNVAVCMnNWRHFtcNHXsoNRayP9z1qj4Sq3nMqYXg=="],
80+ "@shikijs/engine-javascript": ["@shikijs/engine-javascript@4.0.2", "", { "dependencies": { "@shikijs/types": "4.0.2", "@shikijs/vscode-textmate": "^10.0.2", "oniguruma-to-es": "^4.3.4" } }, "sha512-7PW0Nm49DcoUIQEXlJhNNBHyoGMjalRETTCcjMqEaMoJRLljy1Bi/EGV3/qLBgLKQejdspiiYuHGQW6dX94Nag=="],
7981
80- "@shikijs/themes": ["@shikijs/themes@3.23.0", "", { "dependencies": { "@shikijs/types": "3.23.0" } }, "sha512-5qySYa1ZgAT18HR/ypENL9cUSGOeI2x+4IvYJu4JgVJdizn6kG4ia5Q1jDEOi7gTbN4RbuYtmHh0W3eccOrjMA=="],
82+ "@shikijs/engine-oniguruma": ["@shikijs/engine-oniguruma@4.0.2", "", { "dependencies": { "@shikijs/types": "4.0.2", "@shikijs/vscode-textmate": "^10.0.2" } }, "sha512-UpCB9Y2sUKlS9z8juFSKz7ZtysmeXCgnRF0dlhXBkmQnek7lAToPte8DkxmEYGNTMii72zU/lyXiCB6StuZeJg=="],
8183
82- "@shikijs/types": ["@shikijs/types@3.23.0", "", { "dependencies": { "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4" } }, "sha512-3JZ5HXOZfYjsYSk0yPwBrkupyYSLpAE26Qc0HLghhZNGTZg/SKxXIIgoxOpmmeQP0RRSDJTk1/vPfw9tbw+jSQ=="],
84+ "@shikijs/langs": ["@shikijs/langs@4.0.2", "", { "dependencies": { "@shikijs/types": "4.0.2" } }, "sha512-KaXby5dvoeuZzN0rYQiPMjFoUrz4hgwIE+D6Du9owcHcl6/g16/yT5BQxSW5cGt2MZBz6Hl0YuRqf12omRfUUg=="],
85+
86+ "@shikijs/primitive": ["@shikijs/primitive@4.0.2", "", { "dependencies": { "@shikijs/types": "4.0.2", "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4" } }, "sha512-M6UMPrSa3fN5ayeJwFVl9qWofl273wtK1VG8ySDZ1mQBfhCpdd8nEx7nPZ/tk7k+TYcpqBZzj/AnwxT9lO+HJw=="],
87+
88+ "@shikijs/themes": ["@shikijs/themes@4.0.2", "", { "dependencies": { "@shikijs/types": "4.0.2" } }, "sha512-mjCafwt8lJJaVSsQvNVrJumbnnj1RI8jbUKrPKgE6E3OvQKxnuRoBaYC51H4IGHePsGN/QtALglWBU7DoKDFnA=="],
89+
90+ "@shikijs/types": ["@shikijs/types@4.0.2", "", { "dependencies": { "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4" } }, "sha512-qzbeRooUTPnLE+sHD/Z8DStmaDgnbbc/pMrU203950aRqjX/6AFHeDYT+j00y2lPdz0ywJKx7o/7qnqTivtlXg=="],
8391
8492 "@shikijs/vscode-textmate": ["@shikijs/vscode-textmate@10.0.2", "", {}, "sha512-83yeghZ2xxin3Nj8z1NMd/NCuca+gsYXswywDy5bHvwlWL8tpTQmzGeUuHd9FC3E/SBEMvzJRwWEOz5gGes9Qg=="],
8593
@@ -87,10 +95,14 @@
8795
8896 "@types/hast": ["@types/hast@3.0.4", "", { "dependencies": { "@types/unist": "*" } }, "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ=="],
8997
98+ "@types/mdast": ["@types/mdast@4.0.4", "", { "dependencies": { "@types/unist": "*" } }, "sha512-kGaNbPh1k7AFzgpud/gMdvIm5xuECykRR+JnWKQno9TAXVa6WIVCGTPvYGekIDL4uwCZQSYbUxNBSb1aUo79oA=="],
99+
90100 "@types/node": ["@types/node@25.5.2", "", { "dependencies": { "undici-types": "~7.18.0" } }, "sha512-tO4ZIRKNC+MDWV4qKVZe3Ql/woTnmHDr5JD8UI5hn2pwBrHEwOEMZK7WlNb5RKB6EoJ02gwmQS9OrjuFnZYdpg=="],
91101
92102 "@types/unist": ["@types/unist@3.0.3", "", {}, "sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q=="],
93103
104+ "@ungap/structured-clone": ["@ungap/structured-clone@1.3.0", "", {}, "sha512-WmoN8qaIAo7WTYWbAZuG8PYEhn5fkz7dZrqTBZ7dtt//lL2Gwms1IcnQ5yHqjDfX8Ft5j4YzDM23f87zBfDe9g=="],
105+
94106 "ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="],
95107
96108 "argparse": ["argparse@2.0.1", "", {}, "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="],
@@ -101,10 +113,28 @@
101113
102114 "bun-types": ["bun-types@1.3.11", "", { "dependencies": { "@types/node": "*" } }, "sha512-1KGPpoxQWl9f6wcZh57LvrPIInQMn2TQ7jsgxqpRzg+l0QPOFvJVH7HmvHo/AiPgwXy+/Thf6Ov3EdVn1vOabg=="],
103115
116+ "ccount": ["ccount@2.0.1", "", {}, "sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg=="],
117+
118+ "character-entities-html4": ["character-entities-html4@2.1.0", "", {}, "sha512-1v7fgQRj6hnSwFpq1Eu0ynr/CDEw0rXo2B61qXrLNdHZmPKgb7fqS1a2JwF0rISo9q77jDI8VMEHoApn8qDoZA=="],
119+
120+ "character-entities-legacy": ["character-entities-legacy@3.0.0", "", {}, "sha512-RpPp0asT/6ufRm//AJVwpViZbGM/MkjQFxJccQRHmISF/22NBtsHqAWmL+/pmkPWoIUJdWyeVleTl1wydHATVQ=="],
121+
122+ "comma-separated-tokens": ["comma-separated-tokens@2.0.3", "", {}, "sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg=="],
123+
124+ "dequal": ["dequal@2.0.3", "", {}, "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA=="],
125+
126+ "devlop": ["devlop@1.1.0", "", { "dependencies": { "dequal": "^2.0.0" } }, "sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA=="],
127+
104128 "entities": ["entities@4.5.0", "", {}, "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw=="],
105129
106130 "get-east-asian-width": ["get-east-asian-width@1.5.0", "", {}, "sha512-CQ+bEO+Tva/qlmw24dCejulK5pMzVnUOFOijVogd3KQs07HnRIgp8TGipvCCRT06xeYEbpbgwaCxglFyiuIcmA=="],
107131
132+ "hast-util-to-html": ["hast-util-to-html@9.0.5", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/unist": "^3.0.0", "ccount": "^2.0.0", "comma-separated-tokens": "^2.0.0", "hast-util-whitespace": "^3.0.0", "html-void-elements": "^3.0.0", "mdast-util-to-hast": "^13.0.0", "property-information": "^7.0.0", "space-separated-tokens": "^2.0.0", "stringify-entities": "^4.0.0", "zwitch": "^2.0.4" } }, "sha512-OguPdidb+fbHQSU4Q4ZiLKnzWo8Wwsf5bZfbvu7//a9oTYoqD/fWpe96NuHkoS9h0ccGOTe0C4NGXdtS0iO …
133+
134+ "hast-util-whitespace": ["hast-util-whitespace@3.0.0", "", { "dependencies": { "@types/hast": "^3.0.0" } }, "sha512-88JUN06ipLwsnv+dVn+OIYOvAuvBMy/Qoi6O7mQHxdPXpjy+Cd6xRkWwux7DKO+4sYILtLBRIKgsdpS2gQc7qw=="],
135+
136+ "html-void-elements": ["html-void-elements@3.0.0", "", {}, "sha512-bEqo66MRXsUGxWHV5IP0PUiAWwoEjba4VCzg0LjFJBpchPaTfyfCKTG6bc5F8ucKec3q5y6qOdGyYTSBEvhCrg=="],
137+
108138 "lefthook": ["lefthook@2.1.5", "", { "optionalDependencies": { "lefthook-darwin-arm64": "2.1.5", "lefthook-darwin-x64": "2.1.5", "lefthook-freebsd-arm64": "2.1.5", "lefthook-freebsd-x64": "2.1.5", "lefthook-linux-arm64": "2.1.5", "lefthook-linux-x64": "2.1.5", "lefthook-openbsd-arm64": "2.1.5", "lefthook-openbsd-x64": "2.1.5", "lefthook-windows-arm64": "2.1.5", "lefthook-windows-x64": "2.1.5" }, "bin": { "lefthook": "bin/index.js" } }, "sha512-yB9IFWurFllusbPZqvG0EavTmpNXPya2MuO7Li7YT78xAj3u …
109139
110140 "lefthook-darwin-arm64": ["lefthook-darwin-arm64@2.1.5", "", { "os": "darwin", "cpu": "arm64" }, "sha512-VITTaw8PxxyE26gkZ8UcwIa5ZrWnKNRGLeeSrqri40cQdXvLTEoMq2tjjw7eiL9UcB0waRReDdzydevy9GOPUQ=="],
@@ -133,20 +163,54 @@
133163
134164 "markdown-it": ["markdown-it@14.1.1", "", { "dependencies": { "argparse": "^2.0.1", "entities": "^4.4.0", "linkify-it": "^5.0.0", "mdurl": "^2.0.0", "punycode.js": "^2.3.1", "uc.micro": "^2.1.0" }, "bin": { "markdown-it": "bin/markdown-it.mjs" } }, "sha512-BuU2qnTti9YKgK5N+IeMubp14ZUKUUw7yeJbkjtosvHiP0AZ5c8IAgEMk79D0eC8F23r4Ac/q8cAIFdm2FtyoA=="],
135165
166+ "mdast-util-to-hast": ["mdast-util-to-hast@13.2.1", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/mdast": "^4.0.0", "@ungap/structured-clone": "^1.0.0", "devlop": "^1.0.0", "micromark-util-sanitize-uri": "^2.0.0", "trim-lines": "^3.0.0", "unist-util-position": "^5.0.0", "unist-util-visit": "^5.0.0", "vfile": "^6.0.0" } }, "sha512-cctsq2wp5vTsLIcaymblUriiTcZd0CwWtCbLvrOzYCDZoWyMNV8sZ7krj09FSnsiJi3WVsHLM4k6Dq/yaPyCXA=="],
167+
136168 "mdurl": ["mdurl@2.0.0", "", {}, "sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w=="],
137169
170+ "micromark-util-character": ["micromark-util-character@2.1.1", "", { "dependencies": { "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-wv8tdUTJ3thSFFFJKtpYKOYiGP2+v96Hvk4Tu8KpCAsTMs6yi+nVmGh1syvSCsaxz45J6Jbw+9DD6g97+NV67Q=="],
171+
172+ "micromark-util-encode": ["micromark-util-encode@2.0.1", "", {}, "sha512-c3cVx2y4KqUnwopcO9b/SCdo2O67LwJJ/UyqGfbigahfegL9myoEFoDYZgkT7f36T0bLrM9hZTAaAyH+PCAXjw=="],
173+
174+ "micromark-util-sanitize-uri": ["micromark-util-sanitize-uri@2.0.1", "", { "dependencies": { "micromark-util-character": "^2.0.0", "micromark-util-encode": "^2.0.0", "micromark-util-symbol": "^2.0.0" } }, "sha512-9N9IomZ/YuGGZZmQec1MbgxtlgougxTodVwDzzEouPKo3qFWvymFHWcnDi2vzV1ff6kas9ucW+o3yzJK9YB1AQ=="],
175+
176+ "micromark-util-symbol": ["micromark-util-symbol@2.0.1", "", {}, "sha512-vs5t8Apaud9N28kgCrRUdEed4UJ+wWNvicHLPxCa9ENlYuAY31M0ETy5y1vA33YoNPDFTghEbnh6efaE8h4x0Q=="],
177+
178+ "micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="],
179+
138180 "minimatch": ["minimatch@10.2.5", "", { "dependencies": { "brace-expansion": "^5.0.5" } }, "sha512-MULkVLfKGYDFYejP07QOurDLLQpcjk7Fw+7jXS2R2czRQzR56yHRveU5NDJEOviH+hETZKSkIk5c+T23GjFUMg=="],
139181
140182 "mitata": ["mitata@1.0.34", "", {}, "sha512-Mc3zrtNBKIMeHSCQ0XqRLo1vbdIx1wvFV9c8NJAiyho6AjNfMY8bVhbS12bwciUdd1t4rj8099CH3N3NFahaUA=="],
141183
184+ "oniguruma-parser": ["oniguruma-parser@0.12.1", "", {}, "sha512-8Unqkvk1RYc6yq2WBYRj4hdnsAxVze8i7iPfQr8e4uSP3tRv0rpZcbGUDvxfQQcdwHt/e9PrMvGCsa8OqG9X3w=="],
185+
186+ "oniguruma-to-es": ["oniguruma-to-es@4.3.5", "", { "dependencies": { "oniguruma-parser": "^0.12.1", "regex": "^6.1.0", "regex-recursion": "^6.0.2" } }, "sha512-Zjygswjpsewa0NLTsiizVuMQZbp0MDyM6lIt66OxsF21npUDlzpHi1Mgb/qhQdkb+dWFTzJmFbEWdvZgRho8eQ=="],
187+
142188 "prettier": ["prettier@3.8.1", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg=="],
143189
190+ "property-information": ["property-information@7.1.0", "", {}, "sha512-TwEZ+X+yCJmYfL7TPUOcvBZ4QfoT5YenQiJuX//0th53DE6w0xxLEtfK3iyryQFddXuvkIk51EEgrJQ0WJkOmQ=="],
191+
144192 "punycode.js": ["punycode.js@2.3.1", "", {}, "sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA=="],
145193
194+ "regex": ["regex@6.1.0", "", { "dependencies": { "regex-utilities": "^2.3.0" } }, "sha512-6VwtthbV4o/7+OaAF9I5L5V3llLEsoPyq9P1JVXkedTP33c7MfCG0/5NOPcSJn0TzXcG9YUrR0gQSWioew3LDg=="],
195+
196+ "regex-recursion": ["regex-recursion@6.0.2", "", { "dependencies": { "regex-utilities": "^2.3.0" } }, "sha512-0YCaSCq2VRIebiaUviZNs0cBz1kg5kVS2UKUfNIx8YVs1cN3AV7NTctO5FOKBA+UT2BPJIWZauYHPqJODG50cg=="],
197+
198+ "regex-utilities": ["regex-utilities@2.3.0", "", {}, "sha512-8VhliFJAWRaUiVvREIiW2NXXTmHs4vMNnSzuJVhscgmGav3g9VDxLrQndI3dZZVVdp0ZO/5v0xmX516/7M9cng=="],
199+
200+ "shiki": ["shiki@4.0.2", "", { "dependencies": { "@shikijs/core": "4.0.2", "@shikijs/engine-javascript": "4.0.2", "@shikijs/engine-oniguruma": "4.0.2", "@shikijs/langs": "4.0.2", "@shikijs/themes": "4.0.2", "@shikijs/types": "4.0.2", "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4" } }, "sha512-eAVKTMedR5ckPo4xne/PjYQYrU3qx78gtJZ+sHlXEg5IHhhoQhMfZVzetTYuaJS0L2Ef3AcCRzCHV8T0WI6nIQ=="],
201+
202+ "shiki-stream": ["shiki-stream@0.1.4", "", { "dependencies": { "@shikijs/core": "^3.0.0" }, "peerDependencies": { "react": "^19.0.0", "solid-js": "^1.9.0", "vue": "^3.2.0" }, "optionalPeers": ["react", "solid-js", "vue"] }, "sha512-4pz6JGSDmVTTkPJ/ueixHkFAXY4ySCc+unvCaDZV7hqq/sdJZirRxgIXSuNSKgiFlGTgRR97sdu2R8K55sPsrw=="],
203+
204+ "space-separated-tokens": ["space-separated-tokens@2.0.2", "", {}, "sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q=="],
205+
146206 "string-width": ["string-width@8.2.0", "", { "dependencies": { "get-east-asian-width": "^1.5.0", "strip-ansi": "^7.1.2" } }, "sha512-6hJPQ8N0V0P3SNmP6h2J99RLuzrWz2gvT7VnK5tKvrNqJoyS9W4/Fb8mo31UiPvy00z7DQXkP2hnKBVav76thw=="],
147207
208+ "stringify-entities": ["stringify-entities@4.0.4", "", { "dependencies": { "character-entities-html4": "^2.0.0", "character-entities-legacy": "^3.0.0" } }, "sha512-IwfBptatlO+QCJUo19AqvrPNqlVMpW9YEL2LIVY+Rpv2qsjCGxaDLNRgeGsQWJhfItebuJhsGSLjaBbNSQ+ieg=="],
209+
148210 "strip-ansi": ["strip-ansi@7.2.0", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="],
149211
212+ "trim-lines": ["trim-lines@3.0.1", "", {}, "sha512-kRj8B+YHZCc9kQYdWfJB2/oUl9rA99qbowYYBtr4ui4mZyAQ2JpvVBd/6U2YloATfqBhBTSMhTpgBHtU0Mf3Rg=="],
213+
150214 "typedoc": ["typedoc@0.28.18", "", { "dependencies": { "@gerrit0/mini-shiki": "^3.23.0", "lunr": "^2.3.9", "markdown-it": "^14.1.1", "minimatch": "^10.2.4", "yaml": "^2.8.2" }, "peerDependencies": { "typescript": "5.0.x || 5.1.x || 5.2.x || 5.3.x || 5.4.x || 5.5.x || 5.6.x || 5.7.x || 5.8.x || 5.9.x || 6.0.x" }, "bin": { "typedoc": "bin/typedoc" } }, "sha512-NTWTUOFRQ9+SGKKTuWKUioUkjxNwtS3JDRPVKZAXGHZy2wCA8bdv2iJiyeePn0xkmK+TCCqZFT0X7+2+FLjngA=="],
151215
152216 "typedoc-plugin-markdown": ["typedoc-plugin-markdown@4.11.0", "", { "peerDependencies": { "typedoc": "0.28.x" } }, "sha512-2iunh2ALyfyh204OF7h2u0kuQ84xB3jFZtFyUr01nThJkLvR8oGGSSDlyt2gyO4kXhvUxDcVbO0y43+qX+wFbw=="],
@@ -157,10 +221,38 @@
157221
158222 "undici-types": ["undici-types@7.18.2", "", {}, "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w=="],
159223
224+ "unist-util-is": ["unist-util-is@6.0.1", "", { "dependencies": { "@types/unist": "^3.0.0" } }, "sha512-LsiILbtBETkDz8I9p1dQ0uyRUWuaQzd/cuEeS1hoRSyW5E5XGmTzlwY1OrNzzakGowI9Dr/I8HVaw4hTtnxy8g=="],
225+
226+ "unist-util-position": ["unist-util-position@5.0.0", "", { "dependencies": { "@types/unist": "^3.0.0" } }, "sha512-fucsC7HjXvkB5R3kTCO7kUjRdrS0BJt3M/FPxmHMBOm8JQi2BsHAHFsy27E0EolP8rp0NzXsJ+jNPyDWvOJZPA=="],
227+
228+ "unist-util-stringify-position": ["unist-util-stringify-position@4.0.0", "", { "dependencies": { "@types/unist": "^3.0.0" } }, "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ=="],
229+
230+ "unist-util-visit": ["unist-util-visit@5.1.0", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-is": "^6.0.0", "unist-util-visit-parents": "^6.0.0" } }, "sha512-m+vIdyeCOpdr/QeQCu2EzxX/ohgS8KbnPDgFni4dQsfSCtpz8UqDyY5GjRru8PDKuYn7Fq19j1CQ+nJSsGKOzg=="],
231+
232+ "unist-util-visit-parents": ["unist-util-visit-parents@6.0.2", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-is": "^6.0.0" } }, "sha512-goh1s1TBrqSqukSc8wrjwWhL0hiJxgA8m4kFxGlQ+8FYQ3C/m11FcTs4YYem7V664AhHVvgoQLk890Ssdsr2IQ=="],
233+
234+ "vfile": ["vfile@6.0.3", "", { "dependencies": { "@types/unist": "^3.0.0", "vfile-message": "^4.0.0" } }, "sha512-KzIbH/9tXat2u30jf+smMwFCsno4wHVdNmzFyL+T/L3UGqqk6JKfVqOFOZEpZSHADH1k40ab6NUIXZq422ov3Q=="],
235+
236+ "vfile-message": ["vfile-message@4.0.3", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-stringify-position": "^4.0.0" } }, "sha512-QTHzsGd1EhbZs4AsQ20JX1rC3cOlt/IWJruk893DfLRr57lcnOeMaWG4K0JrRta4mIJZKth2Au3mM3u03/JWKw=="],
237+
160238 "yaml": ["yaml@2.8.3", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-AvbaCLOO2Otw/lW5bmh9d/WEdcDFdQp2Z2ZUH3pX9U2ihyUY0nvLv7J6TrWowklRGPYbB/IuIMfYgxaCPg5Bpg=="],
161239
162240 "yoctocolors": ["yoctocolors@2.1.2", "", {}, "sha512-CzhO+pFNo8ajLM2d2IW/R93ipy99LWjtwblvC1RsoSUMZgyLbYFr221TnSNT7GjGdYui6P459mw9JH/g/zW2ug=="],
163241
164242 "yoctomarkdown": ["yoctomarkdown@0.1.0", "", { "dependencies": { "string-width": "^8.2.0", "strip-ansi": "^7.2.0", "yoctocolors": "^2.1.2" }, "peerDependencies": { "typescript": "^5.9.3" }, "bin": { "yoctomarkdown": "src/cli.ts" } }, "sha512-N5ccZ2dQA3ZJSbImSeSxqfDHC/aBcyPWc5WI57corl4gB2Vsr4W11jswPhVNNI6R49ajFDNBNLLIqJTp5KtcQA=="],
243+
244+ "zwitch": ["zwitch@2.0.4", "", {}, "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A=="],
245+
246+ "@gerrit0/mini-shiki/@shikijs/engine-oniguruma": ["@shikijs/engine-oniguruma@3.23.0", "", { "dependencies": { "@shikijs/types": "3.23.0", "@shikijs/vscode-textmate": "^10.0.2" } }, "sha512-1nWINwKXxKKLqPibT5f4pAFLej9oZzQTsby8942OTlsJzOBZ0MWKiwzMsd+jhzu8YPCHAswGnnN1YtQfirL35g=="],
247+
248+ "@gerrit0/mini-shiki/@shikijs/langs": ["@shikijs/langs@3.23.0", "", { "dependencies": { "@shikijs/types": "3.23.0" } }, "sha512-2Ep4W3Re5aB1/62RSYQInK9mM3HsLeB91cHqznAJMuylqjzNVAVCMnNWRHFtcNHXsoNRayP9z1qj4Sq3nMqYXg=="],
249+
250+ "@gerrit0/mini-shiki/@shikijs/themes": ["@shikijs/themes@3.23.0", "", { "dependencies": { "@shikijs/types": "3.23.0" } }, "sha512-5qySYa1ZgAT18HR/ypENL9cUSGOeI2x+4IvYJu4JgVJdizn6kG4ia5Q1jDEOi7gTbN4RbuYtmHh0W3eccOrjMA=="],
251+
252+ "@gerrit0/mini-shiki/@shikijs/types": ["@shikijs/types@3.23.0", "", { "dependencies": { "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4" } }, "sha512-3JZ5HXOZfYjsYSk0yPwBrkupyYSLpAE26Qc0HLghhZNGTZg/SKxXIIgoxOpmmeQP0RRSDJTk1/vPfw9tbw+jSQ=="],
253+
254+ "shiki-stream/@shikijs/core": ["@shikijs/core@3.23.0", "", { "dependencies": { "@shikijs/types": "3.23.0", "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4", "hast-util-to-html": "^9.0.5" } }, "sha512-NSWQz0riNb67xthdm5br6lAkvpDJRTgB36fxlo37ZzM2yq0PQFFzbd8psqC2XMPgCzo1fW6cVi18+ArJ44wqgA=="],
255+
256+ "shiki-stream/@shikijs/core/@shikijs/types": ["@shikijs/types@3.23.0", "", { "dependencies": { "@shikijs/vscode-textmate": "^10.0.2", "@types/hast": "^3.0.4" } }, "sha512-3JZ5HXOZfYjsYSk0yPwBrkupyYSLpAE26Qc0HLghhZNGTZg/SKxXIIgoxOpmmeQP0RRSDJTk1/vPfw9tbw+jSQ=="],
165257 }
166258 }
examples/syntax-highlight.ts+189−0
@@ -0,0 +1,189 @@
1+/**
2+ * cel-tui: SyntaxHighlight demo
3+ *
4+ * Streams TypeScript code into the SyntaxHighlight component.
5+ * Shows lazy Shiki loading plus append-only incremental highlighting.
6+ *
7+ * Run: bun run examples/syntax-highlight.ts
8+ */
9+
10+import { cel, VStack, HStack, Text, ProcessTerminal } from "@cel-tui/core";
11+import { Divider, Spacer, SyntaxHighlight } from "@cel-tui/components";
12+
13+const LANGUAGE = "typescript";
14+const MIN_COLS = 52;
15+const MIN_ROWS = 16;
16+const CHUNK_SIZE = 3;
17+const TICK_MS = 18;
18+
19+const FULL_CODE = [
20+ "/**",
21+ " * Streaming syntax highlight demo.",
22+ " */",
23+ "interface User {",
24+ " id: number;",
25+ " name: string;",
26+ "}",
27+ "",
28+ "const users: User[] = [",
29+ ' { id: 1, name: "Ada" },',
30+ ' { id: 2, name: "Linus" },',
31+ "];",
32+ "",
33+ "function greet(user: User): string {",
34+ " return `Hello, ${user.name}!`;",
35+ "}",
36+ "",
37+ "/* Multi-line comment to test streamed grammar state */",
38+ "for (const user of users) {",
39+ " console.log(greet(user));",
40+ "}",
41+].join("\n");
42+
43+let content = "";
44+let paused = false;
45+let streamTimer: ReturnType<typeof setTimeout> | null = null;
46+
47+function quit() {
48+ stopStreaming();
49+ cel.stop();
50+ process.exit(0);
51+}
52+
53+function stopStreaming() {
54+ if (streamTimer) {
55+ clearTimeout(streamTimer);
56+ streamTimer = null;
57+ }
58+}
59+
60+function isDone(): boolean {
61+ return content.length >= FULL_CODE.length;
62+}
63+
64+function startStreaming() {
65+ stopStreaming();
66+ if (isDone() || paused) return;
67+ streamTimer = setTimeout(streamNext, TICK_MS);
68+}
69+
70+function streamNext() {
71+ if (paused || isDone()) {
72+ streamTimer = null;
73+ cel.render();
74+ return;
75+ }
76+
77+ content += FULL_CODE.slice(content.length, content.length + CHUNK_SIZE);
78+ cel.render();
79+
80+ if (isDone()) {
81+ streamTimer = null;
82+ return;
83+ }
84+
85+ streamTimer = setTimeout(streamNext, TICK_MS);
86+}
87+
88+function restart() {
89+ content = "";
90+ paused = false;
91+ startStreaming();
92+ cel.render();
93+}
94+
95+function togglePause() {
96+ if (isDone()) return;
97+ paused = !paused;
98+ if (!paused) startStreaming();
99+ else stopStreaming();
100+ cel.render();
101+}
102+
103+startStreaming();
104+
105+cel.init(new ProcessTerminal());
106+cel.viewport(() => {
107+ const cols = process.stdout.columns || 80;
108+ const rows = process.stdout.rows || 24;
109+
110+ if (cols < MIN_COLS || rows < MIN_ROWS) {
111+ return VStack(
112+ {
113+ height: "100%",
114+ justifyContent: "center",
115+ alignItems: "center",
116+ onKeyPress: (key) => {
117+ if (key === "ctrl+q" || key === "ctrl+c") quit();
118+ },
119+ },
120+ [
121+ Text("┌───────────────────────────────┐", { fgColor: "color03" }),
122+ Text("│ Terminal too small :( │", { fgColor: "color03" }),
123+ Text("│ │", { fgColor: "color03" }),
124+ Text("│ Please resize to at least │", { fgColor: "color03" }),
125+ Text(
126+ `│ ${String(MIN_COLS).padStart(3)}×${String(MIN_ROWS).padStart(2)} characters. │`,
127+ { fgColor: "color03" },
128+ ),
129+ Text("│ │", { fgColor: "color03" }),
130+ Text("│ Ctrl+Q to quit │", { fgColor: "color03" }),
131+ Text("└───────────────────────────────┘", { fgColor: "color03" }),
132+ ],
133+ );
134+ }
135+
136+ const status = isDone() ? "done" : paused ? "paused" : "streaming";
137+ const statusColor = isDone() ? "color02" : paused ? "color03" : "color06";
138+
139+ return VStack(
140+ {
141+ height: "100%",
142+ onKeyPress: (key) => {
143+ if (key === "ctrl+q" || key === "ctrl+c") quit();
144+ if (key === "ctrl+r") restart();
145+ if (key === "ctrl+p") togglePause();
146+ },
147+ },
148+ [
149+ HStack({ padding: { x: 1 } }, [
150+ Text("SyntaxHighlight Demo", { bold: true, fgColor: "color06" }),
151+ Spacer(),
152+ Text(LANGUAGE, { fgColor: "color08" }),
153+ ]),
154+ Divider({ fgColor: "color08" }),
155+ HStack({ padding: { x: 1 }, gap: 2 }, [
156+ Text(`status: ${status}`, { fgColor: statusColor, bold: true }),
157+ Text(`chars: ${content.length}/${FULL_CODE.length}`, {
158+ fgColor: "color08",
159+ }),
160+ ]),
161+ HStack({ padding: { x: 1 } }, [
162+ Text("Shiki loads lazily. Colors appear after first render.", {
163+ fgColor: "color08",
164+ italic: true,
165+ }),
166+ ]),
167+ Divider({ fgColor: "color08" }),
168+ VStack(
169+ {
170+ flex: 1,
171+ overflow: "scroll",
172+ scrollbar: true,
173+ padding: { x: 1 },
174+ },
175+ [SyntaxHighlight(content, LANGUAGE)],
176+ ),
177+ Divider({ fgColor: "color08" }),
178+ HStack({ padding: { x: 1 }, gap: 1 }, [
179+ Text("Ctrl+R", { fgColor: "color07", bgColor: "color08", bold: true }),
180+ Text("restart", { fgColor: "color08" }),
181+ Text(" "),
182+ Text("Ctrl+P", { fgColor: "color07", bgColor: "color08", bold: true }),
183+ Text("pause/resume", { fgColor: "color08" }),
184+ Spacer(),
185+ Text("Ctrl+Q quit", { fgColor: "color08" }),
186+ ]),
187+ ],
188+ );
189+});
packages/components/package.json+2−0
@@ -34,6 +34,8 @@
3434 "dependencies": {
3535 "@cel-tui/types": "workspace:*",
3636 "@cel-tui/core": "workspace:*",
37+ "shiki": "^4.0.2",
38+ "shiki-stream": "^0.1.4",
3739 "yoctomarkdown": "^0.1.0"
3840 },
3941 "peerDependencies": {
packages/components/src/index.ts+1−0
@@ -34,3 +34,4 @@ export {
3434 type BlockToken,
3535 type InlineSpan,
3636 } from "./markdown.js";
37+export { SyntaxHighlight } from "./syntax-highlight.js";
packages/components/src/syntax-highlight.test.ts+126−0
@@ -0,0 +1,126 @@
1+import { describe, expect, test } from "bun:test";
2+import type { ContainerNode, Node, TextNode } from "@cel-tui/types";
3+import { SyntaxHighlight } from "./syntax-highlight.js";
4+
5+function asContainer(node: Node): ContainerNode {
6+ expect(node.type === "vstack" || node.type === "hstack").toBe(true);
7+ return node as ContainerNode;
8+}
9+
10+function asText(node: Node): TextNode {
11+ expect(node.type).toBe("text");
12+ return node as TextNode;
13+}
14+
15+function lineText(line: Node): string {
16+ const container = asContainer(line);
17+ return container.children.map((child) => asText(child).content).join("");
18+}
19+
20+function firstText(line: Node): TextNode {
21+ const container = asContainer(line);
22+ return asText(container.children[0]!);
23+}
24+
25+function hasStyledText(node: ContainerNode): boolean {
26+ return node.children.some((line) => {
27+ const container = asContainer(line);
28+ return container.children.some((child) => {
29+ const text = asText(child);
30+ return (
31+ text.props.fgColor !== undefined ||
32+ text.props.bgColor !== undefined ||
33+ text.props.bold === true ||
34+ text.props.italic === true ||
35+ text.props.underline === true
36+ );
37+ });
38+ });
39+}
40+
41+async function waitForHighlight(
42+ content: string,
43+ language: string,
44+): Promise<ContainerNode> {
45+ const deadline = Date.now() + 10_000;
46+
47+ while (true) {
48+ const node = SyntaxHighlight(content, language);
49+ if (hasStyledText(node)) {
50+ return node;
51+ }
52+
53+ if (Date.now() > deadline) {
54+ throw new Error(`Timed out waiting for ${language} highlighter`);
55+ }
56+
57+ await new Promise((resolve) => setTimeout(resolve, 10));
58+ }
59+}
60+
61+describe("SyntaxHighlight", () => {
62+ test("renders plain text for unknown languages", () => {
63+ const node = SyntaxHighlight("plain text", "definitely-not-real");
64+
65+ expect(node.type).toBe("vstack");
66+ expect(node.children).toHaveLength(1);
67+ expect(lineText(node.children[0]!)).toBe("plain text");
68+ expect(hasStyledText(node)).toBe(false);
69+ });
70+
71+ test("eventually highlights javascript after lazy language load", async () => {
72+ const node = await waitForHighlight("const value = 42", "javascript");
73+
74+ expect(node.type).toBe("vstack");
75+ expect(node.children).toHaveLength(1);
76+
77+ const firstLine = asContainer(node.children[0]!);
78+ const firstToken = asText(firstLine.children[0]!);
79+ expect(firstToken.content).toBe("const");
80+ expect(firstToken.props.fgColor).toBeDefined();
81+ });
82+
83+ test("keeps one rendered child per source line", async () => {
84+ await waitForHighlight("const first = 1", "javascript");
85+ const node = SyntaxHighlight(
86+ "const first = 1\nconst second = 2",
87+ "javascript",
88+ );
89+
90+ expect(node.children).toHaveLength(2);
91+ expect(lineText(node.children[0]!)).toBe("const first = 1");
92+ expect(lineText(node.children[1]!)).toBe("const second = 2");
93+ });
94+
95+ test("updates append-only content without duplicating previous text", async () => {
96+ await waitForHighlight('const message = "hel', "javascript");
97+ const node = SyntaxHighlight('const message = "hello"', "javascript");
98+
99+ expect(node.children).toHaveLength(1);
100+ expect(lineText(node.children[0]!)).toBe('const message = "hello"');
101+ });
102+
103+ test("preserves grammar state across appended multiline comment", async () => {
104+ await waitForHighlight("/* hello", "javascript");
105+ const node = SyntaxHighlight("/* hello\nworld */", "javascript");
106+
107+ expect(node.children).toHaveLength(2);
108+ expect(lineText(node.children[0]!)).toBe("/* hello");
109+ expect(lineText(node.children[1]!)).toBe("world */");
110+
111+ const firstLine = firstText(node.children[0]!);
112+ const secondLine = firstText(node.children[1]!);
113+ expect(firstLine.props.fgColor).toBeDefined();
114+ expect(secondLine.props.fgColor).toBe(firstLine.props.fgColor);
115+ });
116+
117+ test("falls back to full rehighlight on non-append edits", async () => {
118+ await waitForHighlight("const value = 42", "javascript");
119+ const node = SyntaxHighlight("function value() {}", "javascript");
120+
121+ expect(node.children).toHaveLength(1);
122+ expect(lineText(node.children[0]!)).toBe("function value() {}");
123+ expect(firstText(node.children[0]!).content).toBe("function");
124+ expect(firstText(node.children[0]!).props.fgColor).toBeDefined();
125+ });
126+});
packages/components/src/syntax-highlight.ts+509−0
@@ -0,0 +1,509 @@
1+import type { Color, ContainerNode, Node, StyleProps } from "@cel-tui/types";
2+import { cel, HStack, Text, VStack } from "@cel-tui/core";
3+import type { BuiltinLanguage, Highlighter, ThemedToken } from "shiki";
4+import type {
5+ ShikiStreamTokenizer,
6+ ShikiStreamTokenizerEnqueueResult,
7+} from "shiki-stream";
8+
9+const INTERNAL_THEME = "min-dark";
10+const MAX_STATES_PER_LANGUAGE = 4;
11+
12+const FONT_STYLE_ITALIC = 1;
13+const FONT_STYLE_BOLD = 2;
14+const FONT_STYLE_UNDERLINE = 4;
15+
16+const ANSI_SLOT_HEX: ReadonlyArray<readonly [Color, string]> = [
17+ ["color00", "#000000"],
18+ ["color01", "#cd3131"],
19+ ["color02", "#0dbc79"],
20+ ["color03", "#e5e510"],
21+ ["color04", "#2472c8"],
22+ ["color05", "#bc3fbc"],
23+ ["color06", "#11a8cd"],
24+ ["color07", "#e5e5e5"],
25+ ["color08", "#666666"],
26+ ["color09", "#f14c4c"],
27+ ["color10", "#23d18b"],
28+ ["color11", "#f5f543"],
29+ ["color12", "#3b8eea"],
30+ ["color13", "#d670d6"],
31+ ["color14", "#29b8db"],
32+ ["color15", "#ffffff"],
33+];
34+
35+type SyntaxHighlightStatus = "ready" | "loading" | "error" | "unsupported";
36+
37+interface SyntaxHighlightRuntime {
38+ bundledLanguages: Record<string, unknown>;
39+ createHighlighter: (typeof import("shiki"))["createHighlighter"];
40+ ShikiStreamTokenizer: (typeof import("shiki-stream"))["ShikiStreamTokenizer"];
41+}
42+
43+const rgbCache = new Map<string, readonly [number, number, number]>();
44+const colorSlotCache = new Map<string, Color>();
45+const loadedLanguages = new Set<string>();
46+const loadingLanguages = new Map<string, Promise<void>>();
47+const languageErrors = new Map<string, Error>();
48+const statesByLanguage = new Map<string, SyntaxHighlightState[]>();
49+
50+let runtimePromise: Promise<SyntaxHighlightRuntime> | null = null;
51+let runtimeInstance: SyntaxHighlightRuntime | null = null;
52+let runtimeError: Error | null = null;
53+let highlighterPromise: Promise<Highlighter> | null = null;
54+let highlighterInstance: Highlighter | null = null;
55+let highlighterError: Error | null = null;
56+
57+interface SyntaxHighlightState {
58+ lastContent: string;
59+ tokenizer: ShikiStreamTokenizer;
60+ lastNode: ContainerNode | null;
61+}
62+
63+interface HighlightRun {
64+ content: string;
65+ style: StyleProps;
66+}
67+
68+function normalizeError(error: unknown): Error {
69+ return error instanceof Error ? error : new Error(String(error));
70+}
71+
72+function getRuntimePromise(): Promise<SyntaxHighlightRuntime> {
73+ if (runtimeError) {
74+ return Promise.reject(runtimeError);
75+ }
76+
77+ if (!runtimePromise) {
78+ runtimePromise = Promise.all([import("shiki"), import("shiki-stream")])
79+ .then(([shiki, shikiStream]) => {
80+ runtimeInstance = {
81+ bundledLanguages: shiki.bundledLanguages as Record<string, unknown>,
82+ createHighlighter: shiki.createHighlighter,
83+ ShikiStreamTokenizer: shikiStream.ShikiStreamTokenizer,
84+ };
85+ return runtimeInstance;
86+ })
87+ .catch((error) => {
88+ runtimeError = normalizeError(error);
89+ throw runtimeError;
90+ })
91+ .finally(() => {
92+ cel.render();
93+ });
94+ }
95+
96+ return runtimePromise;
97+}
98+
99+function getHighlighterPromise(): Promise<Highlighter> {
100+ if (highlighterError) {
101+ return Promise.reject(highlighterError);
102+ }
103+
104+ if (!highlighterPromise) {
105+ highlighterPromise = getRuntimePromise()
106+ .then((runtime) =>
107+ runtime.createHighlighter({
108+ themes: [INTERNAL_THEME],
109+ langs: [],
110+ }),
111+ )
112+ .then((highlighter) => {
113+ highlighterInstance = highlighter;
114+ return highlighter;
115+ })
116+ .catch((error) => {
117+ highlighterError = normalizeError(error);
118+ throw highlighterError;
119+ })
120+ .finally(() => {
121+ cel.render();
122+ });
123+ }
124+
125+ return highlighterPromise;
126+}
127+
128+function hasBundledLanguage(
129+ language: string,
130+ runtime: SyntaxHighlightRuntime,
131+): boolean {
132+ return Object.hasOwn(runtime.bundledLanguages, language);
133+}
134+
135+function ensureLanguageReady(language: string): SyntaxHighlightStatus {
136+ if (runtimeError || highlighterError || languageErrors.has(language)) {
137+ return "error";
138+ }
139+
140+ if (!runtimeInstance) {
141+ void getRuntimePromise();
142+ return "loading";
143+ }
144+
145+ if (!hasBundledLanguage(language, runtimeInstance)) {
146+ return "unsupported";
147+ }
148+
149+ if (loadedLanguages.has(language) && highlighterInstance) {
150+ return "ready";
151+ }
152+
153+ if (!loadingLanguages.has(language)) {
154+ const promise = getHighlighterPromise()
155+ .then((highlighter) =>
156+ highlighter.loadLanguage(language as BuiltinLanguage),
157+ )
158+ .then(() => {
159+ loadedLanguages.add(language);
160+ })
161+ .catch((error) => {
162+ languageErrors.set(language, normalizeError(error));
163+ })
164+ .finally(() => {
165+ loadingLanguages.delete(language);
166+ cel.render();
167+ });
168+
169+ loadingLanguages.set(language, promise);
170+ }
171+
172+ return "loading";
173+}
174+
175+function getStates(language: string): SyntaxHighlightState[] {
176+ let states = statesByLanguage.get(language);
177+ if (!states) {
178+ states = [];
179+ statesByLanguage.set(language, states);
180+ }
181+ return states;
182+}
183+
184+function touchState(language: string, state: SyntaxHighlightState): void {
185+ const states = getStates(language);
186+ const index = states.indexOf(state);
187+ if (index !== -1) {
188+ states.splice(index, 1);
189+ }
190+ states.push(state);
191+ if (states.length > MAX_STATES_PER_LANGUAGE) {
192+ states.shift();
193+ }
194+}
195+
196+function findState(
197+ language: string,
198+ content: string,
199+): SyntaxHighlightState | undefined {
200+ const states = getStates(language);
201+
202+ for (let i = states.length - 1; i >= 0; i--) {
203+ const state = states[i]!;
204+ if (state.lastContent === content) {
205+ return state;
206+ }
207+ }
208+
209+ let best: SyntaxHighlightState | undefined;
210+ let bestLength = -1;
211+
212+ for (let i = states.length - 1; i >= 0; i--) {
213+ const state = states[i]!;
214+ if (!content.startsWith(state.lastContent)) {
215+ continue;
216+ }
217+ if (state.lastContent.length > bestLength) {
218+ best = state;
219+ bestLength = state.lastContent.length;
220+ }
221+ }
222+
223+ return best;
224+}
225+
226+function createState(
227+ language: string,
228+ highlighter: Highlighter,
229+ runtime: SyntaxHighlightRuntime,
230+): SyntaxHighlightState {
231+ return {
232+ lastContent: "",
233+ tokenizer: new runtime.ShikiStreamTokenizer({
234+ highlighter,
235+ lang: language,
236+ theme: INTERNAL_THEME,
237+ }),
238+ lastNode: null,
239+ };
240+}
241+
242+function updateState(state: SyntaxHighlightState, content: string): void {
243+ if (content === state.lastContent) {
244+ return;
245+ }
246+
247+ if (!content.startsWith(state.lastContent)) {
248+ state.tokenizer.clear();
249+ state.lastContent = "";
250+ }
251+
252+ const appended = content.slice(state.lastContent.length);
253+ enqueueSync(state.tokenizer, appended);
254+ state.lastContent = content;
255+ state.lastNode = null;
256+}
257+
258+function enqueueSync(
259+ tokenizer: ShikiStreamTokenizer,
260+ chunk: string,
261+): ShikiStreamTokenizerEnqueueResult {
262+ const chunkLines = (tokenizer.lastUnstableCodeChunk + chunk).split("\n");
263+
264+ const stable: ThemedToken[] = [];
265+ let unstable: ThemedToken[] = [];
266+ const recall = tokenizer.tokensUnstable.length;
267+
268+ chunkLines.forEach((line, index) => {
269+ const isLastLine = index === chunkLines.length - 1;
270+
271+ const result = tokenizer.options.highlighter.codeToTokens(line, {
272+ ...tokenizer.options,
273+ grammarState: tokenizer.lastStableGrammarState,
274+ });
275+
276+ const tokens = [...(result.tokens[0] ?? [])];
277+ if (!isLastLine) {
278+ tokens.push({ content: "\n", offset: 0 });
279+ }
280+
281+ if (!isLastLine) {
282+ tokenizer.lastStableGrammarState = result.grammarState;
283+ stable.push(...tokens);
284+ } else {
285+ unstable = tokens;
286+ tokenizer.lastUnstableCodeChunk = line;
287+ }
288+ });
289+
290+ tokenizer.tokensStable.push(...stable);
291+ tokenizer.tokensUnstable = unstable;
292+
293+ return {
294+ recall,
295+ stable,
296+ unstable,
297+ };
298+}
299+
300+function parseHex(color: string): readonly [number, number, number] {
301+ const cached = rgbCache.get(color);
302+ if (cached) {
303+ return cached;
304+ }
305+
306+ const normalized = normalizeHex(color);
307+ const rgb: readonly [number, number, number] = [
308+ Number.parseInt(normalized.slice(1, 3), 16),
309+ Number.parseInt(normalized.slice(3, 5), 16),
310+ Number.parseInt(normalized.slice(5, 7), 16),
311+ ];
312+ rgbCache.set(color, rgb);
313+ return rgb;
314+}
315+
316+function normalizeHex(color: string): string {
317+ const lower = color.toLowerCase();
318+ if (lower.length === 4) {
319+ return `#${lower[1]}${lower[1]}${lower[2]}${lower[2]}${lower[3]}${lower[3]}`;
320+ }
321+ if (lower.length === 9) {
322+ return lower.slice(0, 7);
323+ }
324+ return lower;
325+}
326+
327+function colorDistance(
328+ a: readonly [number, number, number],
329+ b: readonly [number, number, number],
330+): number {
331+ const dr = a[0] - b[0];
332+ const dg = a[1] - b[1];
333+ const db = a[2] - b[2];
334+ return dr * dr + dg * dg + db * db;
335+}
336+
337+function colorToSlot(color: string | undefined): Color | undefined {
338+ if (!color) {
339+ return undefined;
340+ }
341+
342+ const normalized = normalizeHex(color);
343+ const cached = colorSlotCache.get(normalized);
344+ if (cached) {
345+ return cached;
346+ }
347+
348+ const rgb = parseHex(normalized);
349+ let best = ANSI_SLOT_HEX[0]![0];
350+ let bestDistance = Number.POSITIVE_INFINITY;
351+
352+ for (const [slot, hex] of ANSI_SLOT_HEX) {
353+ const distance = colorDistance(rgb, parseHex(hex));
354+ if (distance < bestDistance) {
355+ bestDistance = distance;
356+ best = slot;
357+ }
358+ }
359+
360+ colorSlotCache.set(normalized, best);
361+ return best;
362+}
363+
364+function tokenToStyle(token: ThemedToken): StyleProps {
365+ const fontStyle =
366+ typeof token.fontStyle === "number" && token.fontStyle > 0
367+ ? token.fontStyle
368+ : 0;
369+
370+ return {
371+ fgColor: colorToSlot(token.color),
372+ bgColor: colorToSlot(token.bgColor),
373+ italic: (fontStyle & FONT_STYLE_ITALIC) !== 0 || undefined,
374+ bold: (fontStyle & FONT_STYLE_BOLD) !== 0 || undefined,
375+ underline: (fontStyle & FONT_STYLE_UNDERLINE) !== 0 || undefined,
376+ };
377+}
378+
379+function sameStyle(a: StyleProps, b: StyleProps): boolean {
380+ return (
381+ a.fgColor === b.fgColor &&
382+ a.bgColor === b.bgColor &&
383+ a.bold === b.bold &&
384+ a.italic === b.italic &&
385+ a.underline === b.underline
386+ );
387+}
388+
389+function pushRun(
390+ line: HighlightRun[],
391+ content: string,
392+ style: StyleProps,
393+): void {
394+ if (content.length === 0) {
395+ return;
396+ }
397+
398+ const last = line[line.length - 1];
399+ if (last && sameStyle(last.style, style)) {
400+ last.content += content;
401+ return;
402+ }
403+
404+ line.push({ content, style });
405+}
406+
407+function tokensToLines(tokens: readonly ThemedToken[]): HighlightRun[][] {
408+ const lines: HighlightRun[][] = [[]];
409+
410+ for (const token of tokens) {
411+ const style = tokenToStyle(token);
412+ const pieces = token.content.split("\n");
413+
414+ for (let i = 0; i < pieces.length; i++) {
415+ const piece = pieces[i]!;
416+ pushRun(lines[lines.length - 1]!, piece, style);
417+ if (i < pieces.length - 1) {
418+ lines.push([]);
419+ }
420+ }
421+ }
422+
423+ return lines;
424+}
425+
426+function lineToNode(line: HighlightRun[]): Node {
427+ if (line.length === 0) {
428+ return HStack({}, [Text("")]);
429+ }
430+
431+ return HStack(
432+ {},
433+ line.map((run) => Text(run.content, run.style)),
434+ );
435+}
436+
437+function renderPlainContent(content: string): ContainerNode {
438+ const lines = content.split("\n");
439+ return VStack(
440+ {},
441+ lines.map((line) => HStack({}, [Text(line)])),
442+ );
443+}
444+
445+function renderHighlightedState(state: SyntaxHighlightState): ContainerNode {
446+ if (state.lastNode) {
447+ return state.lastNode;
448+ }
449+
450+ const lines = tokensToLines([
451+ ...state.tokenizer.tokensStable,
452+ ...state.tokenizer.tokensUnstable,
453+ ]);
454+
455+ state.lastNode = VStack({}, lines.map(lineToNode));
456+ return state.lastNode;
457+}
458+
459+/**
460+ * Render syntax-highlighted code as cel-tui primitives.
461+ *
462+ * Uses a long-lived Shiki highlighter internally and keeps a small
463+ * per-language streaming cache. When the same content grows by appending
464+ * more text across renders, only the new suffix is tokenized. Non-append
465+ * edits fall back to a full re-highlight of that snippet.
466+ *
467+ * Shiki runtime code loads lazily on first use. Until the requested
468+ * language is ready, or if loading fails, the component renders plain
469+ * text and schedules a re-render. Unknown language ids also render plain
470+ * text.
471+ *
472+ * @param content - Source code to render.
473+ * @param language - Shiki bundled language id or alias.
474+ * @returns A `VStack` containing one highlighted line per child.
475+ *
476+ * @example
477+ * let code = "const answer = 42";
478+ *
479+ * VStack({ overflow: "scroll" }, [
480+ * SyntaxHighlight(code, "javascript"),
481+ * ])
482+ *
483+ * @example
484+ * let code = "";
485+ * onChunk((chunk) => {
486+ * code += chunk;
487+ * cel.render();
488+ * });
489+ *
490+ * VStack({ overflow: "scroll" }, [
491+ * SyntaxHighlight(code, "typescript"),
492+ * ])
493+ */
494+export function SyntaxHighlight(
495+ content: string,
496+ language: string,
497+): ContainerNode {
Cut short: this commit's diff is over 1000 lines.