@ideascol/cli-maker
Version:
A simple library to help create CLIs
135 lines (134 loc) • 5.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProgressBar = exports.ProgressIndicator = void 0;
exports.stripAnsiCodes = stripAnsiCodes;
exports.showSuccess = showSuccess;
exports.showError = showError;
exports.showWarning = showWarning;
exports.showInfo = showInfo;
exports.createTable = createTable;
exports.formatParameterTable = formatParameterTable;
const colors_1 = require("./colors");
function stripAnsiCodes(str) {
// biome-ignore lint: This regex is safe and commonly used for ANSI escape codes
return str.replace(/\u001b\[[0-9;]*[mG]/g, '');
}
class ProgressIndicator {
constructor() {
this.interval = null;
this.frameIndex = 0;
}
start(message = 'Processing...') {
process.stdout.write(`${colors_1.Colors.Info}${colors_1.Colors.SpinnerFrames[0]} ${message}${colors_1.Colors.Reset}`);
this.interval = setInterval(() => {
this.frameIndex = (this.frameIndex + 1) % colors_1.Colors.SpinnerFrames.length;
process.stdout.write(`\r${colors_1.Colors.Info}${colors_1.Colors.SpinnerFrames[this.frameIndex]} ${message}${colors_1.Colors.Reset}`);
}, 100);
}
update(message) {
if (this.interval) {
process.stdout.write(`\r${colors_1.Colors.Info}${colors_1.Colors.SpinnerFrames[this.frameIndex]} ${message}${colors_1.Colors.Reset}`);
}
}
stop(success = true, finalMessage) {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
const icon = success ? '✅' : '❌';
const color = success ? colors_1.Colors.Success : colors_1.Colors.Error;
const message = finalMessage || (success ? 'Done!' : 'Failed!');
process.stdout.write('\r\x1b[K');
process.stdout.write(`${color}${icon} ${message}${colors_1.Colors.Reset}\n`);
}
}
success(message = 'Completed successfully!') {
this.stop(true, message);
}
error(message = 'Operation failed!') {
this.stop(false, message);
}
}
exports.ProgressIndicator = ProgressIndicator;
class ProgressBar {
constructor(total, barLength = 40) {
this.current = 0;
this.barLength = 40;
this.message = '';
this.total = total;
this.barLength = barLength;
}
render() {
const percentage = Math.floor((this.current / this.total) * 100);
const filledLength = Math.floor((this.current / this.total) * this.barLength);
const emptyLength = this.barLength - filledLength;
const filledBar = '█'.repeat(filledLength);
const emptyBar = '░'.repeat(emptyLength);
const bar = `${colors_1.Colors.FgCyan}${filledBar}${colors_1.Colors.FgGray}${emptyBar}${colors_1.Colors.Reset}`;
const percentageText = `${colors_1.Colors.Bright}${percentage}%${colors_1.Colors.Reset}`;
const messageText = this.message ? ` ${colors_1.Colors.FgGray}${this.message}${colors_1.Colors.Reset}` : '';
process.stdout.write(`\r\x1b[K[${bar}] ${percentageText}${messageText}`);
}
start(message = '') {
this.message = message;
this.current = 0;
this.render();
}
update(current, message) {
this.current = Math.min(current, this.total);
if (message !== undefined) {
this.message = message;
}
this.render();
}
increment(step = 1, message) {
this.update(this.current + step, message);
}
complete(message = 'Completed!') {
this.current = this.total;
this.render();
process.stdout.write(`\n${colors_1.Colors.Success}✅ ${message}${colors_1.Colors.Reset}\n`);
}
error(message = 'Failed!') {
process.stdout.write(`\n${colors_1.Colors.Error}❌ ${message}${colors_1.Colors.Reset}\n`);
}
}
exports.ProgressBar = ProgressBar;
function showSuccess(message) {
console.log(`${colors_1.Colors.Success}✅ ${message}${colors_1.Colors.Reset}`);
}
function showError(message) {
console.log(`${colors_1.Colors.Error}❌ ${message}${colors_1.Colors.Reset}`);
}
function showWarning(message) {
console.log(`${colors_1.Colors.Warning}⚠️ ${message}${colors_1.Colors.Reset}`);
}
function showInfo(message) {
console.log(`${colors_1.Colors.Info}ℹ️ ${message}${colors_1.Colors.Reset}`);
}
function createTable(headers, rows) {
if (rows.length === 0)
return '';
const allRows = [headers, ...rows];
const colWidths = headers.map((_, i) => Math.max(...allRows.map(row => stripAnsiCodes(row[i] || '').length)));
const createRow = (row) => {
return row.map((cell, i) => {
const cleanCell = stripAnsiCodes(cell);
return cell.padEnd(colWidths[i] + (cell.length - cleanCell.length));
}).join(' │ ');
};
const separator = colWidths.map(width => '─'.repeat(width)).join('─┼─');
let table = createRow(headers) + '\n';
table += separator + '\n';
table += rows.map(createRow).join('\n');
return table;
}
function formatParameterTable(params) {
const headers = ['Parameter', 'Type', 'Required', 'Description'];
const rows = params.map(param => [
colors_1.Colors.FgGreen + param.name + colors_1.Colors.Reset,
colors_1.Colors.FgBlue + (param.type || 'text') + colors_1.Colors.Reset,
param.required ? colors_1.Colors.FgRed + 'Yes' + colors_1.Colors.Reset : colors_1.Colors.FgGray + 'No' + colors_1.Colors.Reset,
param.description + (param.options ? `\n${colors_1.Colors.FgGray}Options: ${param.options.join(', ')}${colors_1.Colors.Reset}` : '')
]);
return createTable(headers, rows);
}