UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

148 lines 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.detectTerminalCapabilities = detectTerminalCapabilities; exports.validateTerminalSize = validateTerminalSize; exports.isColorTerminal = isColorTerminal; exports.getOptimalLayout = getOptimalLayout; exports.formatBytes = formatBytes; exports.formatDuration = formatDuration; exports.truncateText = truncateText; exports.padText = padText; exports.wrapText = wrapText; function detectTerminalCapabilities() { const width = process.stdout.columns || 80; const height = process.stdout.rows || 24; let colorDepth = 1; const colorterm = process.env['COLORTERM']; const term = process.env['TERM'] || ''; if (colorterm === 'truecolor' || colorterm === '24bit') { colorDepth = 24; } else if (term.includes('256') || colorterm === '256') { colorDepth = 8; } else if (term.includes('color')) { colorDepth = 4; } const supportsUnicode = !!(process.env['LC_ALL']?.includes('UTF-8') || process.env['LC_CTYPE']?.includes('UTF-8') || process.env['LANG']?.includes('UTF-8') || term.includes('utf8') || term.includes('utf-8')); const supportsMouse = !!(term.includes('xterm') || term.includes('screen') || term.includes('tmux') || process.env['TERM_PROGRAM']); return { width, height, colorDepth, supportsUnicode, supportsMouse, platform: process.platform, terminalType: term, }; } function validateTerminalSize(minWidth, minHeight) { const capabilities = detectTerminalCapabilities(); const errors = []; if (capabilities.width < minWidth) { errors.push(`Terminal width too small: ${capabilities.width} (minimum: ${minWidth})`); } if (capabilities.height < minHeight) { errors.push(`Terminal height too small: ${capabilities.height} (minimum: ${minHeight})`); } return { valid: errors.length === 0, width: capabilities.width, height: capabilities.height, errors, }; } function isColorTerminal() { const capabilities = detectTerminalCapabilities(); return capabilities.colorDepth > 1; } function getOptimalLayout(width, height) { if (width >= 120 && height >= 30) { return 'default'; } else if (width >= 80 && height >= 24) { return 'compact'; } else if (width >= 60 && height >= 20) { return 'single'; } throw new Error(`Terminal size too small: ${width}x${height}. Minimum required: 60x20`); } function formatBytes(bytes) { const sizes = ['B', 'KB', 'MB', 'GB']; if (bytes === 0) return '0 B'; const i = Math.floor(Math.log(bytes) / Math.log(1024)); const size = bytes / Math.pow(1024, i); return `${size.toFixed(1)} ${sizes[i]}`; } function formatDuration(milliseconds) { const seconds = Math.floor(milliseconds / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); if (days > 0) { return `${days}d ${hours % 24}h ${minutes % 60}m`; } else if (hours > 0) { return `${hours}h ${minutes % 60}m ${seconds % 60}s`; } else if (minutes > 0) { return `${minutes}m ${seconds % 60}s`; } else { return `${seconds}s`; } } function truncateText(text, maxLength) { if (text.length <= maxLength) { return text; } return text.substring(0, maxLength - 3) + '...'; } function padText(text, width, align = 'left') { if (text.length >= width) { return text.substring(0, width); } const padding = width - text.length; switch (align) { case 'center': { const leftPad = Math.floor(padding / 2); const rightPad = padding - leftPad; return ' '.repeat(leftPad) + text + ' '.repeat(rightPad); } case 'right': return ' '.repeat(padding) + text; case 'left': default: return text + ' '.repeat(padding); } } function wrapText(text, width) { const words = text.split(' '); const lines = []; let currentLine = ''; for (const word of words) { if (currentLine.length + word.length + 1 <= width) { currentLine += (currentLine ? ' ' : '') + word; } else { if (currentLine) { lines.push(currentLine); } currentLine = word; } } if (currentLine) { lines.push(currentLine); } return lines; } //# sourceMappingURL=terminal.js.map