|
| 1 | +/** |
| 2 | + * Emoji Detection and Display Utilities |
| 3 | + * |
| 4 | + * Provides terminal emoji support detection and emoji stripping |
| 5 | + * functionality for clean display on non-emoji terminals. |
| 6 | + * |
| 7 | + * Industry Standard Approach: |
| 8 | + * - Always store Unicode emojis in Git commits |
| 9 | + * - Strip emojis from Labcommitr's UI display when terminal doesn't support them |
| 10 | + * - This ensures GitHub and emoji-capable terminals show emojis correctly |
| 11 | + */ |
| 12 | + |
| 13 | +import { platform } from "os"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Detects whether the current terminal supports emoji display |
| 17 | + * |
| 18 | + * Uses industry-standard heuristics: |
| 19 | + * - Disable in CI environments (CI=true) |
| 20 | + * - Disable for dumb terminals (TERM=dumb) |
| 21 | + * - Disable on older Windows terminals |
| 22 | + * - Check for NO_COLOR environment variable |
| 23 | + * - Allow user override via FORCE_EMOJI_DETECTION |
| 24 | + * |
| 25 | + * @returns Whether emojis should be displayed in the terminal |
| 26 | + */ |
| 27 | +export function detectEmojiSupport(): boolean { |
| 28 | + // User override (highest priority) |
| 29 | + const forceDetection = process.env.FORCE_EMOJI_DETECTION; |
| 30 | + if (forceDetection !== undefined) { |
| 31 | + return forceDetection.toLowerCase() === "true" || forceDetection === "1"; |
| 32 | + } |
| 33 | + |
| 34 | + // NO_COLOR standard (https://no-color.org/) |
| 35 | + if (process.env.NO_COLOR) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + // CI environments typically don't support emojis well |
| 40 | + if (process.env.CI === "true" || process.env.CI === "1") { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + // Dumb terminals don't support emojis |
| 45 | + const term = process.env.TERM; |
| 46 | + if (term === "dumb" || term === "unknown") { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + // Windows terminal detection |
| 51 | + const isWindows = platform() === "win32"; |
| 52 | + if (isWindows) { |
| 53 | + // Modern Windows Terminal (10+) supports emojis |
| 54 | + // Older cmd.exe and PowerShell may not |
| 55 | + // Check for Windows Terminal specific environment variables |
| 56 | + const wtSession = process.env.WT_SESSION; |
| 57 | + if (wtSession) { |
| 58 | + // Windows Terminal detected - supports emojis |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + // Check for ConEmu or other modern terminals |
| 63 | + const conEmu = process.env.CONEMUANSI; |
| 64 | + if (conEmu === "ON") { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + // For older Windows terminals, be conservative |
| 69 | + // Check if we're in a TTY (interactive terminal) |
| 70 | + if (!process.stdout.isTTY) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + // Default to false for older Windows (can be overridden by FORCE_EMOJI_DETECTION) |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + // Unix-like systems: check TERM variable |
| 79 | + // Most modern terminals support emojis |
| 80 | + if (term) { |
| 81 | + // Known non-emoji terminals |
| 82 | + const nonEmojiTerms = ["linux", "vt100", "vt220", "xterm-mono"]; |
| 83 | + if (nonEmojiTerms.includes(term.toLowerCase())) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + // Modern terminals typically support emojis |
| 88 | + // xterm-256color, screen-256color, tmux-256color, etc. |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + // Default: assume emoji support if we have a TTY |
| 93 | + return process.stdout.isTTY === true; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Strips Unicode emojis from a string for display on non-emoji terminals |
| 98 | + * |
| 99 | + * Uses Unicode emoji pattern matching to remove emoji characters |
| 100 | + * while preserving the rest of the text. |
| 101 | + * |
| 102 | + * @param text - Text that may contain emojis |
| 103 | + * @returns Text with emojis removed |
| 104 | + */ |
| 105 | +export function stripEmojis(text: string): string { |
| 106 | + // Unicode emoji pattern matching |
| 107 | + // Matches emoji characters including: |
| 108 | + // - Emoticons (😀-🙏) |
| 109 | + // - Symbols & Pictographs (🌀-🗿) |
| 110 | + // - Transport & Map Symbols (🚀-) |
| 111 | + // - Flags (country flags) |
| 112 | + // - Regional indicators |
| 113 | + // - Variation selectors |
| 114 | + const emojiPattern = |
| 115 | + /[\p{Emoji}\p{Emoji_Presentation}\p{Emoji_Modifier_Base}\p{Emoji_Modifier}\p{Emoji_Component}]/gu; |
| 116 | + |
| 117 | + return text.replace(emojiPattern, "").trim(); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Conditionally strips emojis from text based on terminal support |
| 122 | + * |
| 123 | + * If terminal supports emojis, returns original text. |
| 124 | + * If terminal doesn't support emojis, returns text with emojis stripped. |
| 125 | + * |
| 126 | + * @param text - Text that may contain emojis |
| 127 | + * @param terminalSupportsEmojis - Whether terminal supports emoji display |
| 128 | + * @returns Text with emojis conditionally stripped |
| 129 | + */ |
| 130 | +export function formatForDisplay( |
| 131 | + text: string, |
| 132 | + terminalSupportsEmojis: boolean, |
| 133 | +): string { |
| 134 | + if (terminalSupportsEmojis) { |
| 135 | + return text; |
| 136 | + } |
| 137 | + return stripEmojis(text); |
| 138 | +} |
| 139 | + |
0 commit comments