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