@@ -0,0 +1,278 @@
1+/**
2+ * cel-tui: Diff / patch highlighting
3+ *
4+ * Demonstrates clew's `diff` / `patch` language support through the
5+ * SyntaxHighlight component, including git-style patches and output shaped
6+ * like `createPatch` from the npm `diff` package.
7+ *
8+ * Run: bun run examples/diff-highlight.ts
9+ */
10+
11+import { cel, HStack, ProcessTerminal, Text, VStack } from "@cel-tui/core";
12+import {
13+ Divider,
14+ Spacer,
15+ SyntaxHighlight,
16+ type SyntaxHighlightTheme,
17+ VDivider,
18+} from "@cel-tui/components";
19+import { warningBox } from "./warning-box";
20+
21+const MIN_COLS = 58;
22+const MIN_ROWS = 16;
23+const SIDE_BY_SIDE_MIN_COLS = 104;
24+
25+interface PatchSample {
26+ title: string;
27+ language: "diff" | "patch";
28+ description: string;
29+ source: string;
30+}
31+
32+const DIFF_THEME: SyntaxHighlightTheme = {
33+ name: "diff-highlight-demo",
34+ type: "dark",
35+ fg: "#e5e5e5",
36+ bg: "#000000",
37+ tokenColors: [
38+ { settings: { foreground: "#e5e5e5" } },
39+ {
40+ scope: ["diff.header", "diff.hunk"],
41+ settings: { foreground: "#29b8db", fontStyle: "bold" },
42+ },
43+ {
44+ scope: "diff.file.old",
45+ settings: { foreground: "#f14c4c", fontStyle: "bold" },
46+ },
47+ {
48+ scope: "diff.file.new",
49+ settings: { foreground: "#23d18b", fontStyle: "bold" },
50+ },
51+ {
52+ scope: "diff.inserted",
53+ settings: { foreground: "#23d18b" },
54+ },
55+ {
56+ scope: "diff.deleted",
57+ settings: { foreground: "#f14c4c" },
58+ },
59+ {
60+ scope: "diff.context",
61+ settings: { foreground: "#e5e5e5" },
62+ },
63+ {
64+ scope: "diff.no-newline",
65+ settings: { foreground: "#666666", fontStyle: "italic" },
66+ },
67+ {
68+ scope: ["operator", "diff.marker"],
69+ settings: { foreground: "#bc3fbc" },
70+ },
71+ ],
72+};
73+
74+const THEME_OPTIONS: ReadonlyArray<{
75+ label: string;
76+ theme?: SyntaxHighlightTheme;
77+}> = [
78+ { label: "terminal ansi16" },
79+ { label: "diff tuned", theme: DIFF_THEME },
80+ { label: "dark-plus", theme: "dark-plus" },
81+];
82+
83+const SAMPLES: readonly PatchSample[] = [
84+ {
85+ title: "Git patch",
86+ language: "diff",
87+ description:
88+ "A unified git patch with file metadata, hunk headers, additions, removals, and context lines.",
89+ source: [
90+ "diff --git a/packages/clew/src/index.ts b/packages/clew/src/index.ts",
91+ "index a734b9a..0928140 100644",
92+ "--- a/packages/clew/src/index.ts",
93+ "+++ b/packages/clew/src/index.ts",
94+ "@@ -34,7 +34,7 @@ export function clew(content: string, options: ClewOptions) {",
95+ ' * Streaming is the default mode, and `stability` defaults to "eager".',
96+ " *",
97+ " * The current implementation supports TypeScript / JavaScript ids, Python,",
98+ "- * Bash, JSON, and Markdown.",
99+ "+ * Bash, JSON, Markdown, and diff/patch.",
100+ " */",
101+ " export function clew(content: string, options: ClewStreamOptions): ClewStream;",
102+ ].join("\n"),
103+ },
104+ {
105+ title: "createPatch output",
106+ language: "patch",
107+ description:
108+ "A patch shaped like createPatch(filename, oldText, newText) from the npm diff package.",
109+ source: [
110+ "Index: greeting.txt",
111+ "===================================================================",
112+ "--- greeting.txt",
113+ "+++ greeting.txt",
114+ "@@ -1,6 +1,7 @@",
115+ " hello from cel-tui",
116+ "-old tokenizer path",
117+ "+new diff tokenizer path",
118+ " context survives unchanged",
119+ "+patch aliases are supported too",
120+ " final line",
121+ "\\ No newline at end of file",
122+ ].join("\n"),
123+ },
124+];
125+
126+let sampleIndex = 0;
127+let themeIndex = 1;
128+
129+function currentSample(): PatchSample {
130+ return SAMPLES[sampleIndex] ?? SAMPLES[0]!;
131+}
132+
133+function currentTheme() {
134+ return THEME_OPTIONS[themeIndex] ?? THEME_OPTIONS[0]!;
135+}
136+
137+function selectSample(delta: number) {
138+ sampleIndex = (sampleIndex + delta + SAMPLES.length) % SAMPLES.length;
139+ cel.render();
140+}
141+
142+function toggleTheme() {
143+ themeIndex = (themeIndex + 1) % THEME_OPTIONS.length;
144+ cel.render();
145+}
146+
147+function quit() {
148+ cel.stop();
149+ process.exit(0);
150+}
151+
152+function sampleChip(sample: PatchSample, index: number) {
153+ const selected = index === sampleIndex;
154+
155+ return Text(` ${sample.language} `, {
156+ fgColor: selected ? "color15" : "color08",
157+ bgColor: selected ? "color06" : undefined,
158+ bold: selected || undefined,
159+ });
160+}
161+
162+function infoPane() {
163+ const sample = currentSample();
164+ const theme = currentTheme();
165+
166+ return VStack({ padding: { x: 1 }, gap: 1 }, [
167+ Text("Diff highlight demo", { bold: true, fgColor: "color06" }),
168+ Text("SyntaxHighlight(sample, language, { theme })", {
169+ fgColor: "color08",
170+ italic: true,
171+ }),
172+ Divider({ fgColor: "color08" }),
173+ Text(sample.title, { bold: true, fgColor: "color03" }),
174+ Text(sample.description, { fgColor: "color08", wrap: "word" }),
175+ Text(`language: ${sample.language}`, { fgColor: "color08" }),
176+ Text(`theme: ${theme.label}`, { fgColor: "color08" }),
177+ Divider({ fgColor: "color08" }),
178+ Text("Try it", { bold: true, fgColor: "color06" }),
179+ Text("Left/Right or Tab switch patch", { fgColor: "color08" }),
180+ Text("Ctrl+T cycle theme", { fgColor: "color08" }),
181+ Text("Ctrl+Q quit", { fgColor: "color08" }),
182+ ]);
183+}
184+
185+function patchPane() {
186+ const sample = currentSample();
187+ const theme = currentTheme();
188+
189+ return VStack({ flex: 1 }, [
190+ HStack({ padding: { x: 1 }, gap: 1 }, [
191+ Text(sample.title, { bold: true, fgColor: "color06" }),
192+ Spacer(),
193+ Text(`SyntaxHighlight(..., \"${sample.language}\")`, {
194+ fgColor: "color08",
195+ }),
196+ ]),
197+ Divider({ fgColor: "color08" }),
198+ VStack(
199+ {
200+ flex: 1,
201+ overflow: "scroll",
202+ scrollbar: true,
203+ padding: { x: 1 },
204+ },
205+ [SyntaxHighlight(sample.source, sample.language, { theme: theme.theme })],
206+ ),
207+ ]);
208+}
209+
210+function mainView(cols: number) {
211+ if (cols >= SIDE_BY_SIDE_MIN_COLS) {
212+ return HStack({ flex: 1 }, [
213+ VStack({ width: 36, overflow: "scroll", scrollbar: true }, [infoPane()]),
214+ VDivider({ fgColor: "color08" }),
215+ patchPane(),
216+ ]);
217+ }
218+
219+ return VStack({ flex: 1 }, [
220+ infoPane(),
221+ Divider({ fgColor: "color08" }),
222+ patchPane(),
223+ ]);
224+}
225+
226+cel.init(new ProcessTerminal());
227+cel.viewport(() => {
228+ const cols = process.stdout.columns || 80;
229+ const rows = process.stdout.rows || 24;
230+
231+ if (cols < MIN_COLS || rows < MIN_ROWS) {
232+ return VStack(
233+ {
234+ height: "100%",
235+ justifyContent: "center",
236+ alignItems: "center",
237+ onKeyPress: (key) => {
238+ if (key === "ctrl+q" || key === "ctrl+c") quit();
239+ },
240+ },
241+ [
242+ ...warningBox([
243+ " Terminal too small :(",
244+ "",
245+ " Please resize to at",
246+ ` least ${String(MIN_COLS).padStart(3)}×${String(MIN_ROWS).padStart(2)} chars.`,
247+ "",
248+ " Ctrl+Q to quit",
249+ ]),
250+ ],
251+ );
252+ }
253+
254+ return VStack(
255+ {
256+ height: "100%",
257+ onKeyPress: (key) => {
258+ if (key === "ctrl+q" || key === "ctrl+c") quit();
259+ if (key === "ctrl+t") toggleTheme();
260+ if (key === "left" || key === "shift+tab") selectSample(-1);
261+ if (key === "right" || key === "tab") selectSample(1);
262+ },
263+ },
264+ [
265+ HStack({ padding: { x: 1 }, gap: 2 }, [
266+ Text("Diff / patch highlighting", { bold: true, fgColor: "color06" }),
267+ Spacer(),
268+ Text(`theme: ${currentTheme().label}`, { fgColor: "color03" }),
269+ ]),
270+ Divider({ fgColor: "color08" }),
271+ HStack({ padding: { x: 1 }, gap: 1, flexWrap: "wrap" }, [
272+ ...SAMPLES.map(sampleChip),
273+ ]),
274+ Divider({ fgColor: "color08" }),
275+ mainView(cols),
276+ ],
277+ );
278+});