Repository navigation
Expand file tree
/
Copy pathprogress.ts
More file actions
230 lines (208 loc) 路 5.48 KB
/
Copy pathprogress.ts
File metadata and controls
230 lines (208 loc) 路 5.48 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { spinner } from "@clack/prompts";
import { createOscProgressController, supportsOscProgress } from "osc-progress";
import {
clearActiveProgressLine,
registerActiveProgressLine,
unregisterActiveProgressLine,
} from "../terminal/progress-line.js";
import { theme } from "../terminal/theme.js";
const DEFAULT_DELAY_MS = 0;
let activeProgress = 0;
type ProgressOptions = {
label: string;
indeterminate?: boolean;
total?: number;
enabled?: boolean;
delayMs?: number;
stream?: NodeJS.WriteStream;
fallback?: "spinner" | "line" | "log" | "none";
};
export type ProgressReporter = {
setLabel: (label: string) => void;
setPercent: (percent: number) => void;
tick: (delta?: number) => void;
done: () => void;
};
export type ProgressTotalsUpdate = {
completed: number;
total: number;
label?: string;
};
const noopReporter: ProgressReporter = {
setLabel: () => {},
setPercent: () => {},
tick: () => {},
done: () => {},
};
export function createCliProgress(options: ProgressOptions): ProgressReporter {
if (options.enabled === false) {
return noopReporter;
}
if (activeProgress > 0) {
return noopReporter;
}
const stream = options.stream ?? process.stderr;
const isTty = stream.isTTY;
const allowLog = !isTty && options.fallback === "log";
if (!isTty && !allowLog) {
return noopReporter;
}
const delayMs = typeof options.delayMs === "number" ? options.delayMs : DEFAULT_DELAY_MS;
const canOsc = isTty && supportsOscProgress(process.env, isTty);
const allowSpinner = isTty && (options.fallback === undefined || options.fallback === "spinner");
const allowLine = isTty && options.fallback === "line";
let started = false;
let label = options.label;
const total = options.total ?? null;
let completed = 0;
let percent = 0;
let indeterminate =
options.indeterminate ?? (options.total === undefined || options.total === null);
activeProgress += 1;
if (isTty) {
registerActiveProgressLine(stream);
}
const controller = canOsc
? createOscProgressController({
env: process.env,
isTty: stream.isTTY,
write: (chunk: string) => stream.write(chunk),
})
: null;
const spin = allowSpinner ? spinner() : null;
const renderLine = allowLine
? () => {
if (!started) {
return;
}
const suffix = indeterminate ? "" : ` ${percent}%`;
clearActiveProgressLine();
stream.write(`${theme.accent(label)}${suffix}`);
}
: null;
const renderLog = allowLog
? (() => {
let lastLine = "";
let lastAt = 0;
const throttleMs = 250;
return () => {
if (!started) {
return;
}
const suffix = indeterminate ? "" : ` ${percent}%`;
const nextLine = `${label}${suffix}`;
const now = Date.now();
if (nextLine === lastLine && now - lastAt < throttleMs) {
return;
}
lastLine = nextLine;
lastAt = now;
stream.write(`${nextLine}\n`);
};
})()
: null;
let timer: NodeJS.Timeout | null = null;
const applyState = () => {
if (!started) {
return;
}
if (controller) {
if (indeterminate) {
controller.setIndeterminate(label);
} else {
controller.setPercent(label, percent);
}
}
if (spin) {
spin.message(theme.accent(label));
}
if (renderLine) {
renderLine();
}
if (renderLog) {
renderLog();
}
};
const start = () => {
if (started) {
return;
}
started = true;
if (spin) {
spin.start(theme.accent(label));
}
applyState();
};
if (delayMs === 0) {
start();
} else {
timer = setTimeout(start, delayMs);
}
const setLabel = (next: string) => {
label = next;
applyState();
};
const setPercent = (nextPercent: number) => {
percent = Math.max(0, Math.min(100, Math.round(nextPercent)));
indeterminate = false;
applyState();
};
const tick = (delta = 1) => {
if (!total) {
return;
}
completed = Math.min(total, completed + delta);
const nextPercent = total > 0 ? Math.round((completed / total) * 100) : 0;
setPercent(nextPercent);
};
const done = () => {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (!started) {
activeProgress = Math.max(0, activeProgress - 1);
return;
}
if (controller) {
controller.clear();
}
if (spin) {
spin.stop();
}
clearActiveProgressLine();
if (isTty) {
unregisterActiveProgressLine(stream);
}
activeProgress = Math.max(0, activeProgress - 1);
};
return { setLabel, setPercent, tick, done };
}
export async function withProgress<T>(
options: ProgressOptions,
work: (progress: ProgressReporter) => Promise<T>,
): Promise<T> {
const progress = createCliProgress(options);
try {
return await work(progress);
} finally {
progress.done();
}
}
export async function withProgressTotals<T>(
options: ProgressOptions,
work: (update: (update: ProgressTotalsUpdate) => void, progress: ProgressReporter) => Promise<T>,
): Promise<T> {
return await withProgress(options, async (progress) => {
const update = ({ completed, total, label }: ProgressTotalsUpdate) => {
if (label) {
progress.setLabel(label);
}
if (!Number.isFinite(total) || total <= 0) {
return;
}
progress.setPercent((completed / total) * 100);
};
return await work(update, progress);
});
}