8){ self::$COLOR_BLACK = $colors >= 256 ? `tput setaf 16` : `tput setaf 0`; self::$COLOR_DARK_BLUE = $colors >= 256 ? `tput setaf 19` : `tput setaf 4`; self::$COLOR_DARK_GREEN = $colors >= 256 ? `tput setaf 34` : `tput setaf 2`; self::$COLOR_DARK_AQUA = $colors >= 256 ? `tput setaf 37` : `tput setaf 6`; self::$COLOR_DARK_RED = $colors >= 256 ? `tput setaf 124` : `tput setaf 1`; self::$COLOR_PURPLE = $colors >= 256 ? `tput setaf 127` : `tput setaf 5`; self::$COLOR_GOLD = $colors >= 256 ? `tput setaf 214` : `tput setaf 3`; self::$COLOR_GRAY = $colors >= 256 ? `tput setaf 145` : `tput setaf 7`; self::$COLOR_DARK_GRAY = $colors >= 256 ? `tput setaf 59` : `tput setaf 8`; self::$COLOR_BLUE = $colors >= 256 ? `tput setaf 63` : `tput setaf 12`; self::$COLOR_GREEN = $colors >= 256 ? `tput setaf 83` : `tput setaf 10`; self::$COLOR_AQUA = $colors >= 256 ? `tput setaf 87` : `tput setaf 14`; self::$COLOR_RED = $colors >= 256 ? `tput setaf 203` : `tput setaf 9`; self::$COLOR_LIGHT_PURPLE = $colors >= 256 ? `tput setaf 207` : `tput setaf 13`; self::$COLOR_YELLOW = $colors >= 256 ? `tput setaf 227` : `tput setaf 11`; self::$COLOR_WHITE = $colors >= 256 ? `tput setaf 231` : `tput setaf 15`; }else{ self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = `tput setaf 0`; self::$COLOR_RED = self::$COLOR_DARK_RED = `tput setaf 1`; self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = `tput setaf 2`; self::$COLOR_YELLOW = self::$COLOR_GOLD = `tput setaf 3`; self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = `tput setaf 4`; self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = `tput setaf 5`; self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = `tput setaf 6`; self::$COLOR_GRAY = self::$COLOR_WHITE = `tput setaf 7`; } } public static function init(?bool $enableFormatting = null) : void{ self::$formattingCodes = $enableFormatting ?? self::detectFormattingCodesSupport(); if(!self::$formattingCodes){ return; } switch(Utils::getOS()){ case "linux": case "mac": case "bsd": self::getEscapeCodes(); return; case "win": case "android": self::getFallbackEscapeCodes(); return; } //TODO: iOS } public static function isInit() : bool{ return self::$formattingCodes !== null; } /** * Returns a string with colorized ANSI Escape codes for the current terminal * Note that this is platform-dependent and might produce different results depending on the terminal type and/or OS. * * @param string|array $string * * @return string */ public static function toANSI($string) : string{ if(!is_array($string)){ $string = TextFormat::tokenize($string); } $newString = ""; foreach($string as $token){ switch($token){ case TextFormat::BOLD: $newString .= Terminal::$FORMAT_BOLD; break; case TextFormat::OBFUSCATED: $newString .= Terminal::$FORMAT_OBFUSCATED; break; case TextFormat::ITALIC: $newString .= Terminal::$FORMAT_ITALIC; break; case TextFormat::UNDERLINE: $newString .= Terminal::$FORMAT_UNDERLINE; break; case TextFormat::STRIKETHROUGH: $newString .= Terminal::$FORMAT_STRIKETHROUGH; break; case TextFormat::RESET: $newString .= Terminal::$FORMAT_RESET; break; //Colors case TextFormat::BLACK: $newString .= Terminal::$COLOR_BLACK; break; case TextFormat::DARK_BLUE: $newString .= Terminal::$COLOR_DARK_BLUE; break; case TextFormat::DARK_GREEN: $newString .= Terminal::$COLOR_DARK_GREEN; break; case TextFormat::DARK_AQUA: $newString .= Terminal::$COLOR_DARK_AQUA; break; case TextFormat::DARK_RED: $newString .= Terminal::$COLOR_DARK_RED; break; case TextFormat::DARK_PURPLE: $newString .= Terminal::$COLOR_PURPLE; break; case TextFormat::GOLD: $newString .= Terminal::$COLOR_GOLD; break; case TextFormat::GRAY: $newString .= Terminal::$COLOR_GRAY; break; case TextFormat::DARK_GRAY: $newString .= Terminal::$COLOR_DARK_GRAY; break; case TextFormat::BLUE: $newString .= Terminal::$COLOR_BLUE; break; case TextFormat::GREEN: $newString .= Terminal::$COLOR_GREEN; break; case TextFormat::AQUA: $newString .= Terminal::$COLOR_AQUA; break; case TextFormat::RED: $newString .= Terminal::$COLOR_RED; break; case TextFormat::LIGHT_PURPLE: $newString .= Terminal::$COLOR_LIGHT_PURPLE; break; case TextFormat::YELLOW: $newString .= Terminal::$COLOR_YELLOW; break; case TextFormat::WHITE: $newString .= Terminal::$COLOR_WHITE; break; default: $newString .= $token; break; } } return $newString; } /** * Emits a string containing Minecraft colour codes to the console formatted with native colours. * * @param string $line */ public static function write(string $line) : void{ echo self::toANSI($line); } /** * Emits a string containing Minecraft colour codes to the console formatted with native colours, followed by a * newline character. * * @param string $line */ public static function writeLine(string $line) : void{ echo self::toANSI($line) . PHP_EOL; } }