| 44 | } |
| 45 | |
| 46 | export function createProgressBar(total: number, label = ''): ProgressBar { |
| 47 | const isTTY = process.stderr.isTTY; |
| 48 | const width = 30; |
| 49 | |
| 50 | return { |
| 51 | update(current: number) { |
| 52 | if (!isTTY) return; |
| 53 | const pct = Math.min(1, current / total); |
| 54 | const filled = Math.round(width * pct); |
| 55 | const empty = width - filled; |
| 56 | const bar = '█'.repeat(filled) + '░'.repeat(empty); |
| 57 | const pctStr = `${Math.round(pct * 100)}%`; |
| 58 | process.stderr.write(`\r${label} ${bar} ${pctStr}`); |
| 59 | }, |
| 60 | finish() { |
| 61 | if (isTTY) { |
| 62 | process.stderr.write('\n'); |
| 63 | } |
| 64 | }, |
| 65 | }; |
| 66 | } |