image-asset-manager
Version:
A comprehensive image asset management tool for frontend projects
98 lines • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpinnerDisplay = exports.ProgressDisplay = void 0;
exports.formatBytes = formatBytes;
exports.formatDuration = formatDuration;
class ProgressDisplay {
constructor() {
this.lastOutput = "";
}
show(options) {
const { total, current, message = "", showPercentage = true, showBar = true, barLength = 30, } = options;
const percentage = Math.round((current / total) * 100);
const completed = Math.round((current / total) * barLength);
const remaining = barLength - completed;
let output = "";
if (message) {
output += `${message} `;
}
if (showBar) {
const bar = "█".repeat(completed) + "░".repeat(remaining);
output += `[${bar}] `;
}
if (showPercentage) {
output += `${percentage}% `;
}
output += `(${current}/${total})`;
// 清除上一行输出
if (this.lastOutput) {
process.stdout.write("\r" + " ".repeat(this.lastOutput.length) + "\r");
}
process.stdout.write(output);
this.lastOutput = output;
// 如果完成,换行
if (current >= total) {
process.stdout.write("\n");
this.lastOutput = "";
}
}
clear() {
if (this.lastOutput) {
process.stdout.write("\r" + " ".repeat(this.lastOutput.length) + "\r");
this.lastOutput = "";
}
}
finish(message) {
this.clear();
if (message) {
console.log(message);
}
}
}
exports.ProgressDisplay = ProgressDisplay;
class SpinnerDisplay {
constructor() {
this.frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
this.currentFrame = 0;
this.interval = null;
this.message = "";
}
start(message) {
this.message = message;
this.interval = setInterval(() => {
process.stdout.write(`\r${this.frames[this.currentFrame]} ${this.message}`);
this.currentFrame = (this.currentFrame + 1) % this.frames.length;
}, 100);
}
stop(finalMessage) {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
// 清除当前行
process.stdout.write("\r" + " ".repeat(this.message.length + 2) + "\r");
if (finalMessage) {
console.log(finalMessage);
}
}
update(message) {
this.message = message;
}
}
exports.SpinnerDisplay = SpinnerDisplay;
function formatBytes(bytes) {
if (bytes === 0)
return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
}
function formatDuration(ms) {
if (ms < 1000)
return `${ms}ms`;
if (ms < 60000)
return `${(ms / 1000).toFixed(1)}s`;
return `${Math.floor(ms / 60000)}m ${Math.floor((ms % 60000) / 1000)}s`;
}
//# sourceMappingURL=progress.js.map