Spaces:
Paused
Paused
File size: 4,066 Bytes
1da8cb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | type StageStatus = "pending" | "running" | "done" | "failed" | "skipped";
interface StageInfo {
name: string;
status: StageStatus;
duration?: number;
detail?: string;
}
const SPINNER_FRAMES = [
"\u280b",
"\u2819",
"\u2839",
"\u2838",
"\u283c",
"\u2834",
"\u2826",
"\u2827",
"\u2807",
"\u280f",
];
export class ProgressDisplay {
private stages: StageInfo[] = [];
private startTime: number = Date.now();
private hasRendered: boolean = false;
private tickInterval: ReturnType<typeof setInterval> | null = null;
private spinnerFrame: number = 0;
private extraLines: number = 0;
addStage(name: string): number {
const index = this.stages.length;
this.stages.push({ name, status: "pending" });
return index;
}
start(index: number): void {
const stage = this.stages[index];
if (stage) {
stage.status = "running";
this.render();
this.ensureTick();
}
}
complete(index: number, detail?: string): void {
const stage = this.stages[index];
if (stage) {
stage.status = "done";
stage.duration = (Date.now() - this.startTime) / 1000;
stage.detail = detail;
this.render();
this.maybeStopTick();
}
}
fail(index: number, detail?: string): void {
const stage = this.stages[index];
if (stage) {
stage.status = "failed";
stage.duration = (Date.now() - this.startTime) / 1000;
stage.detail = detail;
this.render();
this.maybeStopTick();
}
}
skip(index: number, detail?: string): void {
const stage = this.stages[index];
if (stage) {
stage.status = "skipped";
stage.detail = detail;
this.render();
this.maybeStopTick();
}
}
private ensureTick(): void {
if (this.tickInterval) return;
this.tickInterval = setInterval(() => {
this.spinnerFrame = (this.spinnerFrame + 1) % SPINNER_FRAMES.length;
this.render();
}, 80);
}
private maybeStopTick(): void {
if (!this.stages.some((s) => s.status === "running") && this.tickInterval) {
clearInterval(this.tickInterval);
this.tickInterval = null;
}
}
/** Call after printing extra output (e.g. cost estimate) between renders
* so the next render moves the cursor past those lines too. */
addExtraLines(count: number): void {
this.extraLines += count;
}
private render(): void {
// Move cursor up to overwrite previous output (only after first render)
if (this.hasRendered) {
const lines = this.stages.length + this.extraLines;
this.extraLines = 0;
if (lines > 0) {
process.stdout.write(`\x1b[${lines}A`);
}
}
this.hasRendered = true;
const now = Date.now();
for (const stage of this.stages) {
let icon: string;
let time: string;
if (stage.status === "running") {
icon = SPINNER_FRAMES[this.spinnerFrame] ?? SPINNER_FRAMES[0]!;
time = `${((now - this.startTime) / 1000).toFixed(1)}s`;
} else if (stage.status === "pending") {
icon = " ";
time = " ";
} else {
icon = STATUS_ICONS[stage.status];
time = stage.duration ? `${stage.duration.toFixed(1)}s` : "";
}
const detail = stage.detail ? ` (${stage.detail})` : "";
const line = `${icon} ${stage.name.padEnd(14)} ${time.padStart(6)}${detail}`;
process.stdout.write(`\x1b[2K${line}\n`);
}
}
summary(): void {
if (this.tickInterval) {
clearInterval(this.tickInterval);
this.tickInterval = null;
}
const elapsed = ((Date.now() - this.startTime) / 1000).toFixed(1);
const done = this.stages.filter((s) => s.status === "done").length;
const failed = this.stages.filter((s) => s.status === "failed").length;
console.log(
`\nCompleted ${done}/${this.stages.length} stages in ${elapsed}s${failed > 0 ? ` (${failed} failed)` : ""}`,
);
}
}
const STATUS_ICONS: Record<StageStatus, string> = {
done: "\u2714",
failed: "\u2718",
skipped: "\u23ed",
pending: " ",
running: "",
};
|