The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 07:15 UTC
| 1 | import { diffLines } from "diff"; |
| 2 | import type { Colors } from "./highlight.ts"; |
| 3 | |
| 4 | /** One editor line of a patch: unchanged, added (green) or removed (red). */ |
| 5 | export interface Row { |
| 6 | kind: "same" | "add" | "del"; |
| 7 | text: string; |
| 8 | /** Token colour per character. */ |
| 9 | colors: string[]; |
| 10 | /** Line number in the file after the patch (removed lines have none). */ |
| 11 | number?: number; |
| 12 | } |
| 13 | |
| 14 | export interface PatchView { |
| 15 | rows: Row[]; |
| 16 | /** First and last changed row, to scroll the edit into view. */ |
| 17 | first: number; |
| 18 | last: number; |
| 19 | /** The biggest run of changed rows, `[first, last]`: shown when the whole edit doesn't fit. */ |
| 20 | main: [number, number]; |
| 21 | } |
| 22 | |
| 23 | const splitLines = (value: string) => { |
| 24 | const lines = value.split("\n"); |
| 25 | if (lines.at(-1) === "") lines.pop(); |
| 26 | return lines; |
| 27 | }; |
| 28 | |
| 29 | /** The file as editor rows: the whole `after`, with removed `before` lines interleaved. */ |
| 30 | export const patchView = ( |
| 31 | before: string, |
| 32 | after: string, |
| 33 | beforeColors: Colors, |
| 34 | afterColors: Colors, |
| 35 | ): PatchView => { |
| 36 | const rows: Row[] = []; |
| 37 | let b = 0; |
| 38 | let a = 0; |
| 39 | let number = 1; |
| 40 | let first = -1; |
| 41 | let last = -1; |
| 42 | for (const change of diffLines(before, after)) { |
| 43 | for (const text of splitLines(change.value)) { |
| 44 | if (change.removed) { |
| 45 | rows.push({ kind: "del", text, colors: beforeColors.slice(b, b + text.length) }); |
| 46 | b += text.length + 1; |
| 47 | } else if (change.added) { |
| 48 | rows.push({ |
| 49 | kind: "add", |
| 50 | text, |
| 51 | colors: afterColors.slice(a, a + text.length), |
| 52 | number: number++, |
| 53 | }); |
| 54 | a += text.length + 1; |
| 55 | } else { |
| 56 | rows.push({ |
| 57 | kind: "same", |
| 58 | text, |
| 59 | colors: afterColors.slice(a, a + text.length), |
| 60 | number: number++, |
| 61 | }); |
| 62 | a += text.length + 1; |
| 63 | b += text.length + 1; |
| 64 | } |
| 65 | if (change.added || change.removed) { |
| 66 | if (first < 0) first = rows.length - 1; |
| 67 | last = rows.length - 1; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | let main: [number, number] = [Math.max(0, first), Math.max(0, first)]; |
| 72 | for (let i = 0; i < rows.length; i++) { |
| 73 | if (rows[i]!.kind === "same") continue; |
| 74 | let j = i; |
| 75 | while (j + 1 < rows.length && rows[j + 1]!.kind !== "same") j++; |
| 76 | if (j - i > main[1] - main[0]) main = [i, j]; |
| 77 | i = j; |
| 78 | } |
| 79 | return { rows, first: Math.max(0, first), last: Math.max(0, last), main }; |
| 80 | }; |
| 81 | |
| 82 | /** A file with nothing changing. */ |
| 83 | export const staticView = (text: string, colors: Colors): PatchView => |
| 84 | patchView(text, text, colors, colors); |