(final = false)
| 221 | } |
| 222 | |
| 223 | private render(final = false): void { |
| 224 | if (this.totalItems === 0 && this.processedItems === 0 && !this.startTime) |
| 225 | return // Avoid rendering if not started for 0 items |
| 226 | |
| 227 | const percent = |
| 228 | this.totalItems > 0 ? |
| 229 | Math.min(100, (this.processedItems / this.totalItems) * 100) |
| 230 | : 100 |
| 231 | const filledLength = Math.floor((this.barWidth * percent) / 100) |
| 232 | const emptyLength = this.barWidth - filledLength |
| 233 | |
| 234 | const bar = `[${"=".repeat(filledLength)}${" ".repeat(emptyLength)}]` |
| 235 | |
| 236 | const elapsedMs = Date.now() - this.startTime |
| 237 | const elapsedSec = elapsedMs / 1000 |
| 238 | const formattedElapsed = formatSeconds(elapsedSec) |
| 239 | |
| 240 | let etaStr = "" |
| 241 | if (this.processedItems > 0 && this.processedItems < this.totalItems) { |
| 242 | const avgTimePerItemMs = elapsedMs / this.processedItems |
| 243 | const remainingItems = this.totalItems - this.processedItems |
| 244 | const etaMs = remainingItems * avgTimePerItemMs |
| 245 | etaStr = ` (ETA: ${formatSeconds(etaMs / 1000)})` |
| 246 | } else if (this.processedItems >= this.totalItems && this.totalItems > 0) |
| 247 | etaStr = " (Done)" |
| 248 | |
| 249 | const summary = `${this.processedItems}/${this.totalItems} entries (${percent.toFixed(0)}%)` |
| 250 | const timeInfo = `${formattedElapsed} elapsed${etaStr}` |
| 251 | const line = `Processing: ${bar} ${summary} | ${timeInfo}` |
| 252 | |
| 253 | if (canRewriteStdoutLine()) { |
| 254 | process.stdout.clearLine(0) // Clear the current line |
| 255 | process.stdout.cursorTo(0) // Move cursor to the beginning of the line |
| 256 | process.stdout.write(line) |
| 257 | } else if (final) process.stdout?.write(`${line}\n`) |
| 258 | } |
| 259 | |
| 260 | stop(): void { |
| 261 | if (this.stopped) return |
no test coverage detected