@controlplane/cli
Version:
Control Plane Corporation CLI
81 lines • 2.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProgressBar = void 0;
const helpers_1 = require("./helpers");
const cli_progress_1 = require("cli-progress");
class ProgressBar {
constructor() {
this.isActive = false;
this.progressBar = new cli_progress_1.SingleBar({
format: '{bar} {percentage}% {speed}| {displayValue}/{displayTotal} {unit}',
}, cli_progress_1.Presets.shades_grey);
this.progressInterval = undefined;
this.speed = '';
this.startTime = 0;
this.totalReadableSize = { value: '', unit: 'Bytes' };
this.totalSize = 0;
this.value = 0;
this.valueReadableSize = { value: '', unit: 'Bytes' };
}
// Public Methods //
isComplete() {
return this.value / this.totalSize === 1;
}
start(totalSize) {
this.totalSize = totalSize;
this.totalReadableSize = (0, helpers_1.humanReadableSize)(this.totalSize);
this.progressBar.start(this.totalSize, 0, this.payload);
this.isActive = true;
// Start calculating upload speed
this.startTime = Date.now();
// Calculate the speed in megabytes per second
this.progressInterval = setInterval(() => {
let speedInMbPerSecond = 0;
const elapsedTimeInSeconds = (Date.now() - this.startTime) / 1000;
// Avoid dividing by zero
if (elapsedTimeInSeconds > 0) {
speedInMbPerSecond = this.value / elapsedTimeInSeconds / (1024 * 1024);
}
this.speed = `| Speed: ${speedInMbPerSecond.toFixed(2)} MB/s `;
this.render();
}, 1000);
}
stop() {
if (!this.isActive) {
return;
}
this.isActive = false;
this.progressBar.stop();
clearInterval(this.progressInterval);
}
update(bytes) {
this.value += bytes;
// Skip updating progress bar if it hasn't started
if (!this.isActive) {
return;
}
// Update value readable size for display
this.valueReadableSize = (0, helpers_1.humanReadableSize)(this.value);
// Update the progress bar
this.render();
// Stop if upload has completed
if (this.value >= this.totalSize) {
this.stop();
}
}
// Private Methods //
render() {
this.progressBar.update(this.value, this.payload);
}
// - Getters
get payload() {
return {
speed: this.speed,
displayValue: this.valueReadableSize.value,
displayTotal: this.totalReadableSize.value,
unit: this.totalReadableSize.unit,
};
}
}
exports.ProgressBar = ProgressBar;
//# sourceMappingURL=progress-bar.js.map