xonecas.io
70160
Ce

cel-tui / commit a687a5a

feat: improve syntax highlight example layout

xonecas2026-04-10 a687a5a1dedb493527b341b7d551dab71ecb9d5b parent c04271a

2 files changed

TODO.md+8−0
@@ -32,6 +32,14 @@ Remaining work, known bugs, and planned improvements.
3232
3333 - ❌ **Markdown heading inline styling** — Headings (`#`, `##`, `###`) still strip inline formatting to plain text. Since headings are short and single-line, this is low priority. Paragraphs, list items, and blockquotes now render inline formatting via wrapping HStack.
3434
35+## Syntax Highlighting
36+
37+- ❌ **Shell / terminal language highlighting needs a better backend** — `SyntaxHighlight` currently uses `lowlight` / `highlight.js`. Local checks in this repo show `bash`, `shell`, `shellsession`, and `zsh` are registered, but `fish` and `tmux` are not. Bash quality is also a known upstream weak spot: highlight.js issues call out heredocs, redirects, line continuations, and switch / prompt mis-highlighting, and maintainers explicitly describe bash as "a very simple grammar" that needs a cha …
38+ 1. **Stay on lowlight + register extra grammars/aliases** — lowest migration cost and keeps the current synchronous model, but highlight.js pushes niche language support to third-party grammars and a quick npm search did not turn up obvious maintained `fish` / `tmux` packages.
39+ 2. **Prototype Shiki backend** — TextMate / VS Code grammar ecosystem, documented `shellsession` and `ansi` support, and a JavaScript regex engine option that avoids WASM. Costs: async initialization, more dependency / bundle weight, and a new TextMate-scope-to-cel-style mapping layer.
40+ 3. **Prototype starry-night or tree-sitter backend** — starry-night gives GitHub-style TextMate coverage but is async + WASM-heavy; tree-sitter has active bash / fish / tmux / zsh grammars, but would require building our own highlight-query + theme-mapping pipeline.
41+ Fix: hide highlighter behind an internal adapter, add fixture-based comparison tests for `bash`, `shellsession`, `zsh`, `fish`, `tmux`, and `ansi`, then prototype Shiki first and only switch after measuring startup cost, memory, and token quality against lowlight on those fixtures.
42+
3543 ## Toolchain
3644
3745 - 🔧 **Biome formatter clashes with Prettier on generic type arguments** — When a chained `.method<TypeArgs>(longString)` call exceeds `lineWidth`, biome's formatter breaks at the function args `(` while prettier breaks at the type args `<`. They never converge. Current config has biome formatter enabled (`indentStyle: "space"`), which means both formatters fight on this pattern. Fix: either disable biome's formatter (set `formatter.enabled: false` — `organizeImports` still works as an err …
examples/syntax-highlight.ts+94−16
@@ -1,7 +1,7 @@
11 /**
22 * cel-tui: SyntaxHighlight demo
33 *
4- * Streams TypeScript code into the SyntaxHighlight component.
4+ * Streams TypeScript and Bash code into SyntaxHighlight.
55 * Shows synchronous lowlight highlighting plus append-only incremental updates.
66 *
77 * Run: bun run examples/syntax-highlight.ts
@@ -12,12 +12,16 @@ import {
1212 Divider,
1313 Spacer,
1414 SyntaxHighlight,
15+ VDivider,
1516 type SyntaxHighlightTheme,
1617 } from "@cel-tui/components";
1718
1819 const LANGUAGE = "typescript";
20+const BASH_LANGUAGE = "bash";
1921 const MIN_COLS = 52;
2022 const MIN_ROWS = 16;
23+const STACKED_MIN_ROWS = 20;
24+const SIDE_BY_SIDE_MIN_COLS = 100;
2125 const CHUNK_SIZE = 3;
2226 const TICK_MS = 18;
2327
@@ -109,7 +113,28 @@ const FULL_CODE = [
109113 "Long ass line to see the wrapping behaviuour on the example.Long ass line to see the wrapping behaviuour on the example.Long ass line to see the wrapping behaviuour on the example.Long ass line to see the wrapping behaviuour on the example.Long ass line to see the wrapping behaviuour on the example.Long ass line to see the wrapping behaviuour on the example.",
110114 ].join("\n");
111115
116+const BASH_CODE = [
117+ "#!/usr/bin/env bash",
118+ "set -euo pipefail",
119+ "",
120+ 'project_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)',
121+ "",
122+ "build() {",
123+ " local target=${1:-debug}",
124+ ' echo "==> building ${target} from ${project_root}"',
125+ " bun run typecheck",
126+ " bun test",
127+ "}",
128+ "",
129+ "for target in debug release; do",
130+ ' if [[ "$target" == "release" ]]; then',
131+ ' build "$target"',
132+ " fi",
133+ "done",
134+].join("\n");
135+
112136 let content = "";
137+let bashContent = "";
113138 let paused = false;
114139 let themeIndex = 0;
115140 let streamTimer: ReturnType<typeof setTimeout> | null = null;
@@ -128,7 +153,9 @@ function stopStreaming() {
128153 }
129154
130155 function isDone(): boolean {
131- return content.length >= FULL_CODE.length;
156+ return (
157+ content.length >= FULL_CODE.length && bashContent.length >= BASH_CODE.length
158+ );
132159 }
133160
134161 function startStreaming() {
@@ -144,7 +171,17 @@ function streamNext() {
144171 return;
145172 }
146173
147- content += FULL_CODE.slice(content.length, content.length + CHUNK_SIZE);
174+ const nextContentLength = Math.min(
175+ FULL_CODE.length,
176+ content.length + CHUNK_SIZE,
177+ );
178+ const nextBashLength = Math.min(
179+ BASH_CODE.length,
180+ Math.round((nextContentLength / FULL_CODE.length) * BASH_CODE.length),
181+ );
182+
183+ content = FULL_CODE.slice(0, nextContentLength);
184+ bashContent = BASH_CODE.slice(0, nextBashLength);
148185 cel.render();
149186
150187 if (isDone()) {
@@ -157,6 +194,7 @@ function streamNext() {
157194
158195 function restart() {
159196 content = "";
197+ bashContent = "";
160198 paused = false;
161199 startStreaming();
162200 cel.render();
@@ -175,14 +213,48 @@ function toggleTheme() {
175213 cel.render();
176214 }
177215
216+function codePane(
217+ title: string,
218+ status: string,
219+ content: string,
220+ language: string,
221+ theme?: SyntaxHighlightTheme,
222+) {
223+ return VStack({ flex: 1 }, [
224+ HStack(
225+ {
226+ height: 1,
227+ padding: { x: 1 },
228+ bgColor: "color08",
229+ },
230+ [
231+ Text(title, { bold: true, fgColor: "color06" }),
232+ Spacer(),
233+ Text(status, { fgColor: "color15" }),
234+ ],
235+ ),
236+ VStack(
237+ {
238+ flex: 1,
239+ overflow: "scroll",
240+ scrollbar: true,
241+ padding: { x: 1 },
242+ },
243+ [SyntaxHighlight(content, language, { theme })],
244+ ),
245+ ]);
246+}
247+
178248 startStreaming();
179249
180250 cel.init(new ProcessTerminal());
181251 cel.viewport(() => {
182252 const cols = process.stdout.columns || 80;
183253 const rows = process.stdout.rows || 24;
254+ const useSideBySide = cols >= SIDE_BY_SIDE_MIN_COLS;
255+ const minRows = useSideBySide ? MIN_ROWS : STACKED_MIN_ROWS;
184256
185- if (cols < MIN_COLS || rows < MIN_ROWS) {
257+ if (cols < MIN_COLS || rows < minRows) {
186258 return VStack(
187259 {
188260 height: "100%",
@@ -198,7 +270,7 @@ cel.viewport(() => {
198270 Text("│ │", { fgColor: "color03" }),
199271 Text("│ Please resize to at least │", { fgColor: "color03" }),
200272 Text(
201- `│ ${String(MIN_COLS).padStart(3)}×${String(MIN_ROWS).padStart(2)} characters. │`,
273+ `│ ${String(MIN_COLS).padStart(3)}×${String(minRows).padStart(2)} characters. │`,
202274 { fgColor: "color03" },
203275 ),
204276 Text("│ │", { fgColor: "color03" }),
@@ -211,6 +283,17 @@ cel.viewport(() => {
211283 const status = isDone() ? "done" : paused ? "paused" : "streaming";
212284 const statusColor = isDone() ? "color02" : paused ? "color03" : "color06";
213285 const theme = THEME_OPTIONS[themeIndex]!;
286+ const panes = useSideBySide
287+ ? HStack({ flex: 1 }, [
288+ codePane("TypeScript", "streaming", content, LANGUAGE, theme.theme),
289+ VDivider({ fgColor: "color08" }),
290+ codePane("Bash", "streaming", bashContent, BASH_LANGUAGE, theme.theme),
291+ ])
292+ : VStack({ flex: 1 }, [
293+ codePane("TypeScript", "streaming", content, LANGUAGE, theme.theme),
294+ Divider({ fgColor: "color08" }),
295+ codePane("Bash", "streaming", bashContent, BASH_LANGUAGE, theme.theme),
296+ ]);
214297
215298 return VStack(
216299 {
@@ -227,12 +310,15 @@ cel.viewport(() => {
227310 Text("SyntaxHighlight Demo", { bold: true, fgColor: "color06" }),
228311 Spacer(),
229312 Text(`theme: ${theme.label}`, { fgColor: "color03" }),
230- Text(LANGUAGE, { fgColor: "color08" }),
313+ Text(`${LANGUAGE} + ${BASH_LANGUAGE}`, { fgColor: "color08" }),
231314 ]),
232315 Divider({ fgColor: "color08" }),
233316 HStack({ padding: { x: 1 }, gap: 2 }, [
234317 Text(`status: ${status}`, { fgColor: statusColor, bold: true }),
235- Text(`chars: ${content.length}/${FULL_CODE.length}`, {
318+ Text(`ts chars: ${content.length}/${FULL_CODE.length}`, {
319+ fgColor: "color08",
320+ }),
321+ Text(`bash chars: ${bashContent.length}/${BASH_CODE.length}`, {
236322 fgColor: "color08",
237323 }),
238324 ]),
@@ -243,15 +329,7 @@ cel.viewport(() => {
243329 }),
244330 ]),
245331 Divider({ fgColor: "color08" }),
246- VStack(
247- {
248- flex: 1,
249- overflow: "scroll",
250- scrollbar: true,
251- padding: { x: 1 },
252- },
253- [SyntaxHighlight(content, LANGUAGE, { theme: theme.theme })],
254- ),
332+ panes,
255333 Divider({ fgColor: "color08" }),
256334 VStack({ padding: { x: 1 } }, [
257335 HStack({ gap: 1 }, [