@tomisakae/tomibot
Version:
TomiBot - AI Chatbot CLI với Google Genkit. Một chatbot AI thông minh chạy trên command line với giao diện đẹp.
243 lines • 8.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DisplayUtils = void 0;
const chalk_1 = __importDefault(require("chalk"));
const boxen_1 = __importDefault(require("boxen"));
const figlet_1 = __importDefault(require("figlet"));
const gradient_string_1 = __importDefault(require("gradient-string"));
const cli_table3_1 = __importDefault(require("cli-table3"));
const config_1 = require("../../core/config");
class DisplayUtils {
static clearScreen() {
console.clear();
}
static moveCursor(x, y) {
process.stdout.write(`\x1b[${y};${x}H`);
}
static async createTitle(text, font = 'Big') {
return new Promise((resolve, reject) => {
(0, figlet_1.default)(text, { font: font }, (err, data) => {
if (err) {
reject(err);
}
else {
const coloredTitle = config_1.config.uiConfig.colors
? gradient_string_1.default.rainbow(data || '')
: data || '';
resolve(coloredTitle);
}
});
});
}
static createBox(content, options = {}) {
const { title, borderColor = 'cyan', padding = 1, margin = 1, width, } = options;
const boxenOptions = {
padding,
margin,
borderStyle: 'round',
width,
};
if (config_1.config.uiConfig.colors) {
boxenOptions.borderColor = borderColor;
}
if (title) {
boxenOptions.title = title;
boxenOptions.titleAlignment = 'center';
}
return (0, boxen_1.default)(content, boxenOptions);
}
static createInfoBox(content, title) {
return this.createBox(content, {
title: title || 'ℹ️ Information',
borderColor: 'blue',
});
}
static createWarningBox(content, title) {
return this.createBox(content, {
title: title || '⚠️ Warning',
borderColor: 'yellow',
});
}
static createErrorBox(content, title) {
return this.createBox(content, {
title: title || '❌ Error',
borderColor: 'red',
});
}
static createSuccessBox(content, title) {
return this.createBox(content, {
title: title || '✅ Success',
borderColor: 'green',
});
}
static createTable(headers, rows, options = {}) {
const { style = 'unicode', colors = config_1.config.uiConfig.colors, compact = false, } = options;
const tableOptions = {
head: colors ? headers.map(h => chalk_1.default.cyan.bold(h)) : headers,
style: {
head: [],
border: colors ? ['cyan'] : [],
compact,
},
};
if (style === 'unicode') {
tableOptions.chars = {
top: '═',
'top-mid': '╤',
'top-left': '╔',
'top-right': '╗',
bottom: '═',
'bottom-mid': '╧',
'bottom-left': '╚',
'bottom-right': '╝',
left: '║',
'left-mid': '╟',
mid: '─',
'mid-mid': '┼',
right: '║',
'right-mid': '╢',
middle: '│',
};
}
const table = new cli_table3_1.default(tableOptions);
rows.forEach(row => {
table.push(colors ? row.map(cell => chalk_1.default.white(cell)) : row);
});
return table.toString();
}
static success(message) {
return config_1.config.uiConfig.colors
? chalk_1.default.green('✅ ') + chalk_1.default.green.bold(message)
: `✅ ${message}`;
}
static error(message) {
return config_1.config.uiConfig.colors
? chalk_1.default.red('❌ ') + chalk_1.default.red.bold(message)
: `❌ ${message}`;
}
static warning(message) {
return config_1.config.uiConfig.colors
? chalk_1.default.yellow('⚠️ ') + chalk_1.default.yellow.bold(message)
: `⚠️ ${message}`;
}
static info(message) {
return config_1.config.uiConfig.colors
? chalk_1.default.blue('ℹ️ ') + chalk_1.default.blue.bold(message)
: `ℹ️ ${message}`;
}
static highlight(text) {
return config_1.config.uiConfig.colors ? chalk_1.default.cyan.bold(text) : text;
}
static dim(text) {
return config_1.config.uiConfig.colors ? chalk_1.default.gray(text) : text;
}
static createProgressBar(current, total, width = 20, showPercentage = true) {
const percentage = Math.round((current / total) * 100);
const filled = Math.round((current / total) * width);
const empty = width - filled;
const bar = config_1.config.uiConfig.colors
? chalk_1.default.green('█'.repeat(filled)) + chalk_1.default.gray('░'.repeat(empty))
: '█'.repeat(filled) + '░'.repeat(empty);
const percentageText = showPercentage ? ` ${percentage}%` : '';
const countText = ` (${current}/${total})`;
return `${bar}${percentageText}${countText}`;
}
static createLoadingSpinner(frame = 0) {
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
const spinner = frames[frame % frames.length] || '⠋';
return config_1.config.uiConfig.colors ? chalk_1.default.cyan(spinner) : spinner;
}
static separator(char = '─', length = 50, color = 'gray') {
const line = char.repeat(length);
if (!config_1.config.uiConfig.colors) {
return line;
}
const chalkColor = chalk_1.default[color];
return typeof chalkColor === 'function' ? chalkColor(line) : line;
}
static divider(title, width = 60) {
if (!title) {
return this.separator('═', width, 'cyan');
}
const titleLength = title.length;
const sideLength = Math.floor((width - titleLength - 2) / 2);
const leftSide = '═'.repeat(sideLength);
const rightSide = '═'.repeat(width - titleLength - 2 - sideLength);
const dividerText = `${leftSide} ${title} ${rightSide}`;
return config_1.config.uiConfig.colors ? chalk_1.default.cyan(dividerText) : dividerText;
}
static badge(text, type = 'info') {
if (!config_1.config.uiConfig.colors) {
return `[${text}]`;
}
const colors = {
success: chalk_1.default.black.bgGreen,
error: chalk_1.default.white.bgRed,
warning: chalk_1.default.black.bgYellow,
info: chalk_1.default.white.bgBlue,
};
return colors[type](` ${text} `);
}
static wrapText(text, width = 80) {
const words = text.split(' ');
const lines = [];
let currentLine = '';
for (const word of words) {
if ((currentLine + word).length > width) {
if (currentLine) {
lines.push(currentLine.trim());
currentLine = word + ' ';
}
else {
lines.push(word);
}
}
else {
currentLine += word + ' ';
}
}
if (currentLine) {
lines.push(currentLine.trim());
}
return lines.join('\n');
}
static centerText(text, width = 80) {
const lines = text.split('\n');
return lines
.map(line => {
const padding = Math.max(0, Math.floor((width - line.length) / 2));
return ' '.repeat(padding) + line;
})
.join('\n');
}
static rainbow(text) {
if (!config_1.config.uiConfig.colors) {
return text;
}
return (0, gradient_string_1.default)([
'#ff0000',
'#ff8000',
'#ffff00',
'#80ff00',
'#00ff00',
'#00ff80',
'#00ffff',
'#0080ff',
'#0000ff',
'#8000ff',
'#ff00ff',
'#ff0080',
])(text);
}
static gradient(text, colors = ['#00ff88', '#00aaff']) {
if (!config_1.config.uiConfig.colors) {
return text;
}
return (0, gradient_string_1.default)(colors)(text);
}
}
exports.DisplayUtils = DisplayUtils;
//# sourceMappingURL=display.util.js.map