dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
90 lines • 2.99 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Spinner = void 0;
const terminal_1 = require("./terminal");
class Spinner {
constructor(text = '') {
this.interval = null;
this.currentFrame = 0;
this.isSpinning = false;
this.startTime = 0;
this.frames = ['◐', '◓', '◑', '◒'];
this.text = text;
}
start(text) {
if (text)
this.text = text;
if (this.isSpinning)
return;
if (terminal_1.Terminal.supports('isCI') || !process.stdout.isTTY) {
terminal_1.Terminal.writeLine(`${this.text}...`);
return;
}
this.isSpinning = true;
this.startTime = Date.now();
this.render();
this.interval = setInterval(() => {
this.currentFrame = (this.currentFrame + 1) % this.frames.length;
this.render();
}, 80);
}
stop(success, message) {
if (!this.isSpinning)
return;
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
const duration = Date.now() - this.startTime;
const durationText = duration > 1000 ? ` (${(duration / 1000).toFixed(1)}s)` : '';
let symbol = '';
if (success !== undefined) {
symbol = success ?
terminal_1.Terminal.style('✓', terminal_1.Terminal.colors.green) :
terminal_1.Terminal.style('✖', terminal_1.Terminal.colors.red);
}
const finalText = message || this.text;
terminal_1.Terminal.writeLine(`${symbol} ${finalText}${durationText}`);
this.isSpinning = false;
}
render() {
if (!this.isSpinning)
return;
const frame = terminal_1.Terminal.style(this.frames[this.currentFrame], terminal_1.Terminal.colors.yellow);
terminal_1.Terminal.writeLine(`${frame} ${this.text}`);
}
updateText(text) {
this.text = text;
if (this.isSpinning) {
this.render();
}
}
static async wrap(promise, text, options = {}) {
const spinner = new Spinner(text);
spinner.start();
try {
const result = await promise;
spinner.stop(true, options.successText);
return result;
}
catch (error) {
const errorMessage = options.errorText || this.getErrorMessage(error);
spinner.stop(false, errorMessage);
throw error;
}
}
static getErrorMessage(error) {
if (error instanceof Error) {
return error.message;
}
if (typeof error === 'string') {
return error;
}
if (error && typeof error === 'object' && 'message' in error) {
return String(error.message);
}
return 'An unknown error occurred';
}
}
exports.Spinner = Spinner;
//# sourceMappingURL=spinner.js.map