37 public static string $FORMAT_BOLD =
"";
38 public static string $FORMAT_OBFUSCATED =
"";
39 public static string $FORMAT_ITALIC =
"";
40 public static string $FORMAT_UNDERLINE =
"";
41 public static string $FORMAT_STRIKETHROUGH =
"";
43 public static string $FORMAT_RESET =
"";
45 public static string $COLOR_BLACK =
"";
46 public static string $COLOR_DARK_BLUE =
"";
47 public static string $COLOR_DARK_GREEN =
"";
48 public static string $COLOR_DARK_AQUA =
"";
49 public static string $COLOR_DARK_RED =
"";
50 public static string $COLOR_PURPLE =
"";
51 public static string $COLOR_GOLD =
"";
52 public static string $COLOR_GRAY =
"";
53 public static string $COLOR_DARK_GRAY =
"";
54 public static string $COLOR_BLUE =
"";
55 public static string $COLOR_GREEN =
"";
56 public static string $COLOR_AQUA =
"";
57 public static string $COLOR_RED =
"";
58 public static string $COLOR_LIGHT_PURPLE =
"";
59 public static string $COLOR_YELLOW =
"";
60 public static string $COLOR_WHITE =
"";
61 public static string $COLOR_MINECOIN_GOLD =
"";
63 private static ?
bool $formattingCodes =
null;
65 public static function hasFormattingCodes() :
bool{
66 if(self::$formattingCodes ===
null){
67 throw new \LogicException(
"Formatting codes have not been initialized");
69 return self::$formattingCodes;
72 private static function detectFormattingCodesSupport() :
bool{
73 $stdout = fopen(
"php://stdout",
"w");
76 stream_isatty($stdout) &&
78 getenv(
'TERM') !==
false ||
79 (function_exists(
'sapi_windows_vt100_support') && sapi_windows_vt100_support($stdout))
86 protected static function getFallbackEscapeCodes() :
void{
87 self::$FORMAT_BOLD =
"\x1b[1m";
88 self::$FORMAT_OBFUSCATED =
"";
89 self::$FORMAT_ITALIC =
"\x1b[3m";
90 self::$FORMAT_UNDERLINE =
"\x1b[4m";
91 self::$FORMAT_STRIKETHROUGH =
"\x1b[9m";
93 self::$FORMAT_RESET =
"\x1b[m";
95 $color = fn(
int $code) =>
"\x1b[38;5;{$code}m";
97 self::$COLOR_BLACK = $color(16);
98 self::$COLOR_DARK_BLUE = $color(19);
99 self::$COLOR_DARK_GREEN = $color(34);
100 self::$COLOR_DARK_AQUA = $color(37);
101 self::$COLOR_DARK_RED = $color(124);
102 self::$COLOR_PURPLE = $color(127);
103 self::$COLOR_GOLD = $color(214);
104 self::$COLOR_GRAY = $color(145);
105 self::$COLOR_DARK_GRAY = $color(59);
106 self::$COLOR_BLUE = $color(63);
107 self::$COLOR_GREEN = $color(83);
108 self::$COLOR_AQUA = $color(87);
109 self::$COLOR_RED = $color(203);
110 self::$COLOR_LIGHT_PURPLE = $color(207);
111 self::$COLOR_YELLOW = $color(227);
112 self::$COLOR_WHITE = $color(231);
113 self::$COLOR_MINECOIN_GOLD = $color(184);
116 protected static function getEscapeCodes() :
void{
117 $tput = fn(
string $args) => is_string($result = shell_exec(
"tput $args")) ? $result :
"";
118 $setaf = fn(
int $code) => $tput(
"setaf $code");
120 self::$FORMAT_BOLD = $tput(
"bold");
121 self::$FORMAT_OBFUSCATED = $tput(
"smacs");
122 self::$FORMAT_ITALIC = $tput(
"sitm");
123 self::$FORMAT_UNDERLINE = $tput(
"smul");
124 self::$FORMAT_STRIKETHROUGH =
"\x1b[9m";
126 self::$FORMAT_RESET = $tput(
"sgr0");
128 $colors = (int) $tput(
"colors");
130 self::$COLOR_BLACK = $colors >= 256 ? $setaf(16) : $setaf(0);
131 self::$COLOR_DARK_BLUE = $colors >= 256 ? $setaf(19) : $setaf(4);
132 self::$COLOR_DARK_GREEN = $colors >= 256 ? $setaf(34) : $setaf(2);
133 self::$COLOR_DARK_AQUA = $colors >= 256 ? $setaf(37) : $setaf(6);
134 self::$COLOR_DARK_RED = $colors >= 256 ? $setaf(124) : $setaf(1);
135 self::$COLOR_PURPLE = $colors >= 256 ? $setaf(127) : $setaf(5);
136 self::$COLOR_GOLD = $colors >= 256 ? $setaf(214) : $setaf(3);
137 self::$COLOR_GRAY = $colors >= 256 ? $setaf(145) : $setaf(7);
138 self::$COLOR_DARK_GRAY = $colors >= 256 ? $setaf(59) : $setaf(8);
139 self::$COLOR_BLUE = $colors >= 256 ? $setaf(63) : $setaf(12);
140 self::$COLOR_GREEN = $colors >= 256 ? $setaf(83) : $setaf(10);
141 self::$COLOR_AQUA = $colors >= 256 ? $setaf(87) : $setaf(14);
142 self::$COLOR_RED = $colors >= 256 ? $setaf(203) : $setaf(9);
143 self::$COLOR_LIGHT_PURPLE = $colors >= 256 ? $setaf(207) : $setaf(13);
144 self::$COLOR_YELLOW = $colors >= 256 ? $setaf(227) : $setaf(11);
145 self::$COLOR_WHITE = $colors >= 256 ? $setaf(231) : $setaf(15);
146 self::$COLOR_MINECOIN_GOLD = $colors >= 256 ? $setaf(184) : $setaf(11);
148 self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = $setaf(0);
149 self::$COLOR_RED = self::$COLOR_DARK_RED = $setaf(1);
150 self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = $setaf(2);
151 self::$COLOR_YELLOW = self::$COLOR_GOLD = self::$COLOR_MINECOIN_GOLD = $setaf(3);
152 self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = $setaf(4);
153 self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = $setaf(5);
154 self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = $setaf(6);
155 self::$COLOR_GRAY = self::$COLOR_WHITE = $setaf(7);
159 public static function init(?
bool $enableFormatting =
null) :
void{
160 self::$formattingCodes = $enableFormatting ?? self::detectFormattingCodesSupport();
161 if(!self::$formattingCodes){
166 case Utils::OS_LINUX:
167 case Utils::OS_MACOS:
169 if(getenv(
'TERM') !==
false){
170 self::getEscapeCodes();
174 case Utils::OS_WINDOWS:
175 case Utils::OS_ANDROID:
176 self::getFallbackEscapeCodes();
183 public static function isInit() :
bool{
184 return self::$formattingCodes !==
null;
191 public static function toANSI(
string $string) : string{
194 $newString .= match($token){
195 TextFormat::BOLD => Terminal::$FORMAT_BOLD,
196 TextFormat::OBFUSCATED => Terminal::$FORMAT_OBFUSCATED,
197 TextFormat::ITALIC => Terminal::$FORMAT_ITALIC,
198 TextFormat::UNDERLINE => Terminal::$FORMAT_UNDERLINE,
199 TextFormat::STRIKETHROUGH => Terminal::$FORMAT_STRIKETHROUGH,
200 TextFormat::RESET => Terminal::$FORMAT_RESET,
201 TextFormat::BLACK => Terminal::$COLOR_BLACK,
202 TextFormat::DARK_BLUE => Terminal::$COLOR_DARK_BLUE,
203 TextFormat::DARK_GREEN => Terminal::$COLOR_DARK_GREEN,
204 TextFormat::DARK_AQUA => Terminal::$COLOR_DARK_AQUA,
205 TextFormat::DARK_RED => Terminal::$COLOR_DARK_RED,
206 TextFormat::DARK_PURPLE => Terminal::$COLOR_PURPLE,
207 TextFormat::GOLD => Terminal::$COLOR_GOLD,
208 TextFormat::GRAY => Terminal::$COLOR_GRAY,
209 TextFormat::DARK_GRAY => Terminal::$COLOR_DARK_GRAY,
210 TextFormat::BLUE => Terminal::$COLOR_BLUE,
211 TextFormat::GREEN => Terminal::$COLOR_GREEN,
212 TextFormat::AQUA => Terminal::$COLOR_AQUA,
213 TextFormat::RED => Terminal::$COLOR_RED,
214 TextFormat::LIGHT_PURPLE => Terminal::$COLOR_LIGHT_PURPLE,
215 TextFormat::YELLOW => Terminal::$COLOR_YELLOW,
216 TextFormat::WHITE => Terminal::$COLOR_WHITE,
217 TextFormat::MINECOIN_GOLD => Terminal::$COLOR_MINECOIN_GOLD,
228 public static function write(
string $line) : void{
229 echo self::toANSI($line);
237 echo self::toANSI($line) . self::$FORMAT_RESET . PHP_EOL;