UNPKG

i18ntk

Version:

i18n Tool Kit - Zero-dependency internationalization toolkit for setup, scanning, analysis, validation, auto translation, fixing, reporting, and runtime translation loading.

165 lines (140 loc) โ€ข 5.18 kB
/** * Terminal Icons Utility * * Provides Unicode emoji support with fallbacks for terminals that don't support them. * Automatically detects terminal capabilities and provides appropriate symbols. */ const os = require('os'); const { envManager } = require('./env-manager'); // Detect if terminal supports Unicode/emoji function supportsUnicode() { // Check if we're on Windows if (os.platform() !== 'win32') { return true; // Assume Unix-like systems support Unicode } // Check if we're in a TTY if (!process.stdout.isTTY) { return false; } try { // Try to detect Windows Terminal, VS Code terminal, or other modern terminals const terminal = envManager.get('TERM_PROGRAM') || envManager.get('WT_SESSION') || ''; // Modern terminals that support Unicode const unicodeTerminals = [ 'vscode', // VS Code integrated terminal 'hyper', // Hyper terminal 'tabby', // Tabby terminal 'fluent-terminal', // Fluent Terminal 'windows-terminal', // Windows Terminal 'wt' // Windows Terminal (WT_SESSION) ]; if (unicodeTerminals.some(term => terminal.toLowerCase().includes(term))) { return true; } // Check for Windows Terminal environment variable if (envManager.get('WT_SESSION')) { return true; } // Check for modern PowerShell or pwsh if (envManager.get('PSModulePath') || envManager.get('POWERSHELL_EDITION')) { return true; } // Default to false for older Windows terminals return false; } catch (error) { // If detection fails, default to safe option return false; } } // Icon definitions with fallbacks const ICONS = { // Setup and configuration wrench: { emoji: '๐Ÿ”ง', fallback: '[SETUP]' }, gear: { emoji: 'โš™๏ธ', fallback: '[CONFIG]' }, checkmark: { emoji: 'โœ…', fallback: '[OK]' }, cross: { emoji: 'โŒ', fallback: '[ERROR]' }, warning: { emoji: 'โš ๏ธ', fallback: '[WARN]' }, info: { emoji: 'โ„น๏ธ', fallback: '[INFO]' }, // Actions rocket: { emoji: '๐Ÿš€', fallback: '[START]' }, search: { emoji: '๐Ÿ”', fallback: '[SEARCH]' }, analyze: { emoji: '๐Ÿ“Š', fallback: '[ANALYZE]' }, fix: { emoji: '๐Ÿ”ง', fallback: '[FIX]' }, complete: { emoji: '๐ŸŽ‰', fallback: '[DONE]' }, clean: { emoji: '๐Ÿงน', fallback: '[CLEAN]' }, // Status success: { emoji: 'โœ…', fallback: '[SUCCESS]' }, error: { emoji: 'โŒ', fallback: '[ERROR]' }, pending: { emoji: 'โณ', fallback: '[PENDING]' }, processing: { emoji: 'โš™๏ธ', fallback: '[WORKING]' }, // Files and directories file: { emoji: '๐Ÿ“„', fallback: '[FILE]' }, folder: { emoji: '๐Ÿ“', fallback: '[DIR]' }, report: { emoji: '๐Ÿ“Š', fallback: '[REPORT]' }, backup: { emoji: '๐Ÿ’พ', fallback: '[BACKUP]' }, // Languages and frameworks javascript: { emoji: '๐ŸŸจ', fallback: '[JS]' }, python: { emoji: '๐Ÿ', fallback: '[PY]' }, java: { emoji: 'โ˜•', fallback: '[JAVA]' }, php: { emoji: '๐Ÿ˜', fallback: '[PHP]' }, go: { emoji: '๐Ÿน', fallback: '[GO]' }, react: { emoji: 'โš›๏ธ', fallback: '[REACT]' }, vue: { emoji: '๐Ÿ’š', fallback: '[VUE]' }, angular: { emoji: '๐Ÿ…ฐ๏ธ', fallback: '[ANGULAR]' }, // UI elements bullet: { emoji: 'โ€ข', fallback: '-' }, arrow: { emoji: 'โ†’', fallback: '->' }, separator: { emoji: 'โ•', fallback: '=' }, corner: { emoji: 'โ•”', fallback: '+' }, line: { emoji: 'โ•‘', fallback: '|' }, end: { emoji: 'โ•š', fallback: '+' } }; // Cache the Unicode support detection const _supportsUnicode = supportsUnicode(); /** * Get the appropriate icon for the current terminal * @param {string} iconName - Name of the icon from ICONS * @returns {string} - The icon or fallback text */ function getIcon(iconName) { const icon = ICONS[iconName]; if (!icon) { return `[${iconName.toUpperCase()}]`; } return _supportsUnicode ? icon.emoji : icon.fallback; } /** * Get all available icon names * @returns {string[]} - Array of icon names */ function getAvailableIcons() { return Object.keys(ICONS); } /** * Check if terminal supports Unicode * @returns {boolean} - True if Unicode is supported */ function isUnicodeSupported() { return _supportsUnicode; } /** * Force enable/disable Unicode support (for testing) * @param {boolean} enabled - Whether to enable Unicode */ function setUnicodeSupport(enabled) { // This is mainly for testing purposes // In production, detection should be automatic if (typeof enabled === 'boolean') { // Note: This won't actually change terminal capabilities, // just the detection result for this module console.warn('Warning: setUnicodeSupport() only affects this module\'s detection, not actual terminal capabilities'); } } // Export functions module.exports = { getIcon, getAvailableIcons, isUnicodeSupported, setUnicodeSupport, ICONS };