dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
123 lines • 5.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProgressBar = void 0;
const terminal_1 = require("./terminal");
class ProgressBar {
constructor(total, options = {}) {
var _a, _b, _c, _d, _e, _f, _g;
this.frame = 0;
this.frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
this.current = 0;
this.startTime = Date.now();
if (total <= 0) {
throw new Error('Total must be greater than 0');
}
this.total = total;
this.options = {
title: (_a = options.title) !== null && _a !== void 0 ? _a : '',
width: (_b = options.width) !== null && _b !== void 0 ? _b : Math.min(terminal_1.Terminal.getWindowSize().columns - 30, 50),
showPercentage: (_c = options.showPercentage) !== null && _c !== void 0 ? _c : true,
showCount: (_d = options.showCount) !== null && _d !== void 0 ? _d : true,
barColor: (_e = options.barColor) !== null && _e !== void 0 ? _e : 'cyan',
backgroundColor: (_f = options.backgroundColor) !== null && _f !== void 0 ? _f : 'muted',
spinner: (_g = options.spinner) !== null && _g !== void 0 ? _g : true
};
this.title = this.options.title;
this.width = this.options.width;
this.chars = terminal_1.Terminal.getChars('progress');
}
update(current, status) {
if (current < 0 || current > this.total) {
throw new Error('Current value must be between 0 and total');
}
this.current = current;
// Simple output for CI environments
if (terminal_1.Terminal.supports('isCI')) {
terminal_1.Terminal.write(`${this.title}: ${this.current}/${this.total}${status ? ` - ${status}` : ''}`, true // newLine
);
return;
}
this.render(status);
}
render(status) {
const percentage = Math.round((this.current / this.total) * 100);
const filled = Math.round((this.current / this.total) * this.width);
const empty = this.width - filled;
// Create the progress bar segments
const filledBar = `${terminal_1.Terminal.colors[this.options.barColor]}${this.chars.complete.repeat(filled)}`;
const emptyBar = `${terminal_1.Terminal.colors[this.options.backgroundColor]}${this.chars.incomplete.repeat(empty)}${terminal_1.Terminal.colors.reset}`;
const elements = [this.title];
// Add progress bar
elements.push(`${filledBar}${emptyBar}`);
// Add percentage if enabled
if (this.options.showPercentage) {
elements.push(`${percentage}%`);
}
// Add count if enabled
if (this.options.showCount) {
elements.push(`${this.current}/${this.total}`);
}
// Add status if provided
if (status) {
elements.push(status);
}
// Add elapsed time for long operations
const elapsed = Date.now() - this.startTime;
if (elapsed > 5000) { // Only show time for operations longer than 5s
elements.push(`${(elapsed / 1000).toFixed(1)}s`);
}
terminal_1.Terminal.eraseLine();
terminal_1.Terminal.cursorTo(0);
terminal_1.Terminal.write(elements.filter(Boolean).join(' '), false, true);
}
/**
* Stop and clean up the progress bar
*/
stop() {
if (this.current < this.total) {
this.update(this.total);
}
terminal_1.Terminal.write('\n', true);
}
/**
* Mark progress as failed
*/
fail(message) {
terminal_1.Terminal.eraseLine();
terminal_1.Terminal.cursorTo(0);
terminal_1.Terminal.write(`${terminal_1.Terminal.colors.error}✗${terminal_1.Terminal.colors.reset} ${message || 'Failed'}`, true);
}
/**
* Mark progress as complete
*/
complete(message) {
this.update(this.total);
terminal_1.Terminal.write(` ${terminal_1.Terminal.colors.success}✓${terminal_1.Terminal.colors.reset} ${message || 'Complete'}`, true);
}
/**
* Create a promise-based progress bar
*/
static async wrap(promise, total, options) {
const progress = new ProgressBar(total, options);
if (options.onProgress) {
const originalOnProgress = options.onProgress;
options.onProgress = (current) => {
progress.update(current);
originalOnProgress(current);
};
}
try {
const result = await promise;
progress.complete(options.successText);
return result;
}
catch (error) {
const errorMessage = options.errorText ||
(error instanceof Error ? error.message : 'An error occurred');
progress.fail(errorMessage);
throw error;
}
}
}
exports.ProgressBar = ProgressBar;
//# sourceMappingURL=progress.js.map