"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProgressIndicator = void 0;
const logger_1 = require("./logger");
/**
* Lightweight progress tracking for long-running operations.
*/
class ProgressIndicator {
label;
totalSteps;
currentStep;
startTime;
intervalMs;
intervalId;
constructor(options = {}) {
this.label = options.label ?? "Progress";
this.totalSteps = options.totalSteps ?? 100;
this.currentStep = 0;
this.startTime = Date.now();
this.intervalMs = options.intervalMs ?? 1000;
}
start() {
this.startTime = Date.now();
this.currentStep = 0;
logger_1.logger.info(`${this.label}: started`);
}
step(increment = 1) {
this.currentStep = Math.min(this.currentStep + increment, this.totalSteps);
const pct = ((this.currentStep / this.totalSteps) * 100).toFixed(1);
const elapsed = ((Date.now() - this.startTime) / 1000).toFixed(1);
logger_1.logger.info(`${this.label}: ${pct}% (${this.currentStep}/${this.totalSteps}) — ${elapsed}s`);
return this.currentStep;
}
finish() {
const elapsed = ((Date.now() - this.startTime) / 1000).toFixed(1);
logger_1.logger.info(`${this.label}: completed in ${elapsed}s`);
if (this.intervalId)
clearInterval(this.intervalId);
}
/**
* Run a callback with progress steps automatically tracked.
*/
static async track(options, fn) {
const progress = new ProgressIndicator(options);
progress.start();
try {
const result = await fn(progress);
progress.finish();
return result;
}
catch (err) {
progress.finish();
throw err;
}
}
}
exports.ProgressIndicator = ProgressIndicator;
//# sourceMappingURL=progressIndicator.js.map