-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.ts
More file actions
51 lines (43 loc) · 1.6 KB
/
Copy pathprogress.ts
File metadata and controls
51 lines (43 loc) · 1.6 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
export class ProgressBar {
private total: number;
private current: number = 0;
private barLength: number;
private startTime: number;
constructor(total: number) {
this.total = total;
this.startTime = Date.now();
// Adjust bar length based on terminal width
const terminalWidth = process.stdout.columns || 80;
this.barLength = Math.min(40, Math.max(20, terminalWidth - 60));
}
increment(): void {
this.current++;
this.render();
}
private render(): void {
const percentage = Math.floor((this.current / this.total) * 100);
const filledLength = Math.floor((this.barLength * this.current) / this.total);
const bar = "█".repeat(filledLength) + "░".repeat(this.barLength - filledLength);
const elapsed = Date.now() - this.startTime;
const rate = this.current / (elapsed / 1000);
const eta = this.current === 0 ? 0 : (this.total - this.current) / rate;
const etaFormatted = eta > 60
? `${Math.floor(eta / 60)}m ${Math.floor(eta % 60)}s`
: `${Math.floor(eta)}s`;
// Clear line and write progress
if (process.stdout.isTTY) {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
}
process.stdout.write(
`[${bar}] ${percentage}% | ${this.current}/${this.total} | ETA: ${etaFormatted}`
);
if (this.current === this.total) {
const totalTime = elapsed / 1000;
const totalFormatted = totalTime > 60
? `${Math.floor(totalTime / 60)}m ${Math.floor(totalTime % 60)}s`
: `${totalTime.toFixed(1)}s`;
process.stdout.write(` | Time: ${totalFormatted}\n`);
}
}
}