The best way to control Claude Code, Codex and more from any computer or phone

main
ts 61 lines 2.49 KB
Raw
1 import type { ProjectCloneStage } from "@t3tools/contracts";
2
3 export interface GitCloneProgressLine {
4 readonly stage: ProjectCloneStage;
5 readonly percent: number | null;
6 readonly detail: string | null;
7 }
8
9 const STAGE_PREFIXES: ReadonlyArray<readonly [RegExp, ProjectCloneStage]> = [
10 [/^remote: Enumerating objects/, "counting"],
11 [/^remote: Counting objects/, "counting"],
12 [/^remote: Compressing objects/, "counting"],
13 [/^Receiving objects/, "receiving"],
14 [/^Resolving deltas/, "resolving"],
15 [/^Updating files/, "checkout"],
16 [/^Checking out files/, "checkout"],
17 [/^Filtering content/, "checkout"],
18 ];
19
20 // Linear on hostile remote output: the tail is trimmed in code (optional `\s*`/`.*?` groups backtrack
21 // cubically), and `s` lets it span U+2028 so a failed `$` cannot retry at every later counter.
22 const PERCENT = /:\s+(\d+)%\s+\((\d+)\/(\d+)\)(.*)$/s;
23 const DONE_SUFFIX = ", done.";
24
25 /** Pass each bare-CR progress redraw as a separate line. */
26 export function parseGitCloneProgressLine(line: string): GitCloneProgressLine | null {
27 const trimmed = line.trim();
28 const stageEntry = STAGE_PREFIXES.find(([pattern]) => pattern.test(trimmed));
29 if (!stageEntry) return null;
30 const stage = stageEntry[1];
31 const match = PERCENT.exec(trimmed);
32 if (!match) return { stage, percent: null, detail: null };
33 let tail = (match[4] ?? "").trimEnd();
34 if (tail.endsWith(DONE_SUFFIX)) tail = tail.slice(0, -DONE_SUFFIX.length).trimEnd();
35 if (tail.length > 0 && !tail.startsWith(",")) return { stage, percent: null, detail: null };
36 const percent = Number(match[1]);
37 const rawDetail = tail.slice(1).trim();
38 const detail = rawDetail.length > 0 && rawDetail !== "done." ? rawDetail : null;
39 return {
40 stage,
41 percent: Number.isFinite(percent) ? Math.max(0, Math.min(100, percent)) : null,
42 detail,
43 };
44 }
45
46 /** Git counts filtered files only as each one lands, so this prefix marks its Git LFS phase. */
47 export function isGitFilterProgressLine(line: string): boolean {
48 return line.trimStart().startsWith("Filtering content");
49 }
50
51 /** Git prints nothing while a Git LFS object downloads; this reports the bytes of the unfinished ones. */
52 export function gitLfsDownloadProgressLine(
53 partialBytes: number,
54 percent: number | null,
55 ): GitCloneProgressLine {
56 const size =
57 partialBytes >= 1024 ** 3
58 ? `${(partialBytes / 1024 ** 3).toFixed(2)} GiB`
59 : `${(partialBytes / 1024 ** 2).toFixed(1)} MiB`;
60 return { stage: "checkout", percent, detail: `Downloading Git LFS objects: ${size} in progress` };
61 }