PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
Terminal.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\utils;
25
26use function fclose;
27use function fopen;
28use function function_exists;
29use function getenv;
30use function is_string;
31use function sapi_windows_vt100_support;
32use function shell_exec;
33use function stream_isatty;
34use const PHP_EOL;
35
36abstract class Terminal{
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 = "";
42
43 public static string $FORMAT_RESET = "";
44
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 = "";
62 public static string $COLOR_MATERIAL_QUARTZ = "";
63 public static string $COLOR_MATERIAL_IRON = "";
64 public static string $COLOR_MATERIAL_NETHERITE = "";
65 public static string $COLOR_MATERIAL_REDSTONE = "";
66 public static string $COLOR_MATERIAL_COPPER = "";
67 public static string $COLOR_MATERIAL_GOLD = "";
68 public static string $COLOR_MATERIAL_EMERALD = "";
69 public static string $COLOR_MATERIAL_DIAMOND = "";
70 public static string $COLOR_MATERIAL_LAPIS = "";
71 public static string $COLOR_MATERIAL_AMETHYST = "";
72
73 private static ?bool $formattingCodes = null;
74
75 public static function hasFormattingCodes() : bool{
76 if(self::$formattingCodes === null){
77 throw new \LogicException("Formatting codes have not been initialized");
78 }
79 return self::$formattingCodes;
80 }
81
82 private static function detectFormattingCodesSupport() : bool{
83 $stdout = fopen("php://stdout", "w");
84 if($stdout === false) throw new AssumptionFailedError("Opening php://stdout should never fail");
85 $result = (
86 stream_isatty($stdout) && //STDOUT isn't being piped
87 (
88 getenv('TERM') !== false || //Console says it supports colours
89 (function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support($stdout)) //we're on windows and have vt100 support
90 )
91 );
92 fclose($stdout);
93 return $result;
94 }
95
96 protected static function getFallbackEscapeCodes() : void{
97 self::$FORMAT_BOLD = "\x1b[1m";
98 self::$FORMAT_OBFUSCATED = "";
99 self::$FORMAT_ITALIC = "\x1b[3m";
100 self::$FORMAT_UNDERLINE = "\x1b[4m";
101 self::$FORMAT_STRIKETHROUGH = "\x1b[9m";
102
103 self::$FORMAT_RESET = "\x1b[m";
104
105 $color = fn(int $code) => "\x1b[38;5;{$code}m";
106
107 self::$COLOR_BLACK = $color(16);
108 self::$COLOR_DARK_BLUE = $color(19);
109 self::$COLOR_DARK_GREEN = $color(34);
110 self::$COLOR_DARK_AQUA = $color(37);
111 self::$COLOR_DARK_RED = $color(124);
112 self::$COLOR_PURPLE = $color(127);
113 self::$COLOR_GOLD = $color(214);
114 self::$COLOR_GRAY = $color(145);
115 self::$COLOR_DARK_GRAY = $color(59);
116 self::$COLOR_BLUE = $color(63);
117 self::$COLOR_GREEN = $color(83);
118 self::$COLOR_AQUA = $color(87);
119 self::$COLOR_RED = $color(203);
120 self::$COLOR_LIGHT_PURPLE = $color(207);
121 self::$COLOR_YELLOW = $color(227);
122 self::$COLOR_WHITE = $color(231);
123 self::$COLOR_MINECOIN_GOLD = $color(184);
124 self::$COLOR_MATERIAL_QUARTZ = $color(188);
125 self::$COLOR_MATERIAL_IRON = $color(251);
126 self::$COLOR_MATERIAL_NETHERITE = $color(237);
127 self::$COLOR_MATERIAL_REDSTONE = $color(88);
128 self::$COLOR_MATERIAL_COPPER = $color(131);
129 self::$COLOR_MATERIAL_GOLD = $color(178);
130 self::$COLOR_MATERIAL_EMERALD = $color(35);
131 self::$COLOR_MATERIAL_DIAMOND = $color(37);
132 self::$COLOR_MATERIAL_LAPIS = $color(24);
133 self::$COLOR_MATERIAL_AMETHYST = $color(98);
134 }
135
136 protected static function getEscapeCodes() : void{
137 $tput = fn(string $args) => is_string($result = shell_exec("tput $args")) ? $result : "";
138 $setaf = fn(int $code) => $tput("setaf $code");
139
140 self::$FORMAT_BOLD = $tput("bold");
141 self::$FORMAT_OBFUSCATED = $tput("smacs");
142 self::$FORMAT_ITALIC = $tput("sitm");
143 self::$FORMAT_UNDERLINE = $tput("smul");
144 self::$FORMAT_STRIKETHROUGH = "\x1b[9m"; //`tput `;
145
146 self::$FORMAT_RESET = $tput("sgr0");
147
148 $colors = (int) $tput("colors");
149 if($colors > 8){
150 self::$COLOR_BLACK = $colors >= 256 ? $setaf(16) : $setaf(0);
151 self::$COLOR_DARK_BLUE = $colors >= 256 ? $setaf(19) : $setaf(4);
152 self::$COLOR_DARK_GREEN = $colors >= 256 ? $setaf(34) : $setaf(2);
153 self::$COLOR_DARK_AQUA = $colors >= 256 ? $setaf(37) : $setaf(6);
154 self::$COLOR_DARK_RED = $colors >= 256 ? $setaf(124) : $setaf(1);
155 self::$COLOR_PURPLE = $colors >= 256 ? $setaf(127) : $setaf(5);
156 self::$COLOR_GOLD = $colors >= 256 ? $setaf(214) : $setaf(3);
157 self::$COLOR_GRAY = $colors >= 256 ? $setaf(145) : $setaf(7);
158 self::$COLOR_DARK_GRAY = $colors >= 256 ? $setaf(59) : $setaf(8);
159 self::$COLOR_BLUE = $colors >= 256 ? $setaf(63) : $setaf(12);
160 self::$COLOR_GREEN = $colors >= 256 ? $setaf(83) : $setaf(10);
161 self::$COLOR_AQUA = $colors >= 256 ? $setaf(87) : $setaf(14);
162 self::$COLOR_RED = $colors >= 256 ? $setaf(203) : $setaf(9);
163 self::$COLOR_LIGHT_PURPLE = $colors >= 256 ? $setaf(207) : $setaf(13);
164 self::$COLOR_YELLOW = $colors >= 256 ? $setaf(227) : $setaf(11);
165 self::$COLOR_WHITE = $colors >= 256 ? $setaf(231) : $setaf(15);
166 self::$COLOR_MINECOIN_GOLD = $colors >= 256 ? $setaf(184) : $setaf(11);
167 self::$COLOR_MATERIAL_QUARTZ = $colors >= 256 ? $setaf(188) : $setaf(7);
168 self::$COLOR_MATERIAL_IRON = $colors >= 256 ? $setaf(251) : $setaf(7);
169 self::$COLOR_MATERIAL_NETHERITE = $colors >= 256 ? $setaf(237) : $setaf(1);
170 self::$COLOR_MATERIAL_REDSTONE = $colors >= 256 ? $setaf(88) : $setaf(9);
171 self::$COLOR_MATERIAL_COPPER = $colors >= 256 ? $setaf(131) : $setaf(3);
172 self::$COLOR_MATERIAL_GOLD = $colors >= 256 ? $setaf(178) : $setaf(11);
173 self::$COLOR_MATERIAL_EMERALD = $colors >= 256 ? $setaf(35) : $setaf(2);
174 self::$COLOR_MATERIAL_DIAMOND = $colors >= 256 ? $setaf(37) : $setaf(14);
175 self::$COLOR_MATERIAL_LAPIS = $colors >= 256 ? $setaf(24) : $setaf(12);
176 self::$COLOR_MATERIAL_AMETHYST = $colors >= 256 ? $setaf(98) : $setaf(13);
177 }else{
178 self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = self::$COLOR_MATERIAL_NETHERITE = $setaf(0);
179 self::$COLOR_RED = self::$COLOR_DARK_RED = self::$COLOR_MATERIAL_REDSTONE = self::$COLOR_MATERIAL_COPPER = $setaf(1);
180 self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = self::$COLOR_MATERIAL_EMERALD = $setaf(2);
181 self::$COLOR_YELLOW = self::$COLOR_GOLD = self::$COLOR_MINECOIN_GOLD = self::$COLOR_MATERIAL_GOLD = $setaf(3);
182 self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = self::$COLOR_MATERIAL_LAPIS = $setaf(4);
183 self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = self::$COLOR_MATERIAL_AMETHYST = $setaf(5);
184 self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = self::$COLOR_MATERIAL_DIAMOND = $setaf(6);
185 self::$COLOR_GRAY = self::$COLOR_WHITE = self::$COLOR_MATERIAL_QUARTZ = self::$COLOR_MATERIAL_IRON = $setaf(7);
186 }
187 }
188
189 public static function init(?bool $enableFormatting = null) : void{
190 self::$formattingCodes = $enableFormatting ?? self::detectFormattingCodesSupport();
191 if(!self::$formattingCodes){
192 return;
193 }
194
195 switch(Utils::getOS()){
196 case Utils::OS_LINUX:
197 case Utils::OS_MACOS:
198 case Utils::OS_BSD:
199 if(getenv('TERM') !== false){
200 self::getEscapeCodes();
201 return;
202 }
203
204 case Utils::OS_WINDOWS:
205 case Utils::OS_ANDROID:
206 self::getFallbackEscapeCodes();
207 return;
208 }
209
210 //TODO: iOS
211 }
212
213 public static function isInit() : bool{
214 return self::$formattingCodes !== null;
215 }
216
221 public static function toANSI(string $string) : string{
222 $newString = "";
223 foreach(TextFormat::tokenize($string) as $token){
224 $newString .= match ($token) {
225 TextFormat::BOLD => Terminal::$FORMAT_BOLD,
226 TextFormat::OBFUSCATED => Terminal::$FORMAT_OBFUSCATED,
227 TextFormat::ITALIC => Terminal::$FORMAT_ITALIC,
228 TextFormat::RESET => Terminal::$FORMAT_RESET,
229 TextFormat::BLACK => Terminal::$COLOR_BLACK,
230 TextFormat::DARK_BLUE => Terminal::$COLOR_DARK_BLUE,
231 TextFormat::DARK_GREEN => Terminal::$COLOR_DARK_GREEN,
232 TextFormat::DARK_AQUA => Terminal::$COLOR_DARK_AQUA,
233 TextFormat::DARK_RED => Terminal::$COLOR_DARK_RED,
234 TextFormat::DARK_PURPLE => Terminal::$COLOR_PURPLE,
235 TextFormat::GOLD => Terminal::$COLOR_GOLD,
236 TextFormat::GRAY => Terminal::$COLOR_GRAY,
237 TextFormat::DARK_GRAY => Terminal::$COLOR_DARK_GRAY,
238 TextFormat::BLUE => Terminal::$COLOR_BLUE,
239 TextFormat::GREEN => Terminal::$COLOR_GREEN,
240 TextFormat::AQUA => Terminal::$COLOR_AQUA,
241 TextFormat::RED => Terminal::$COLOR_RED,
242 TextFormat::LIGHT_PURPLE => Terminal::$COLOR_LIGHT_PURPLE,
243 TextFormat::YELLOW => Terminal::$COLOR_YELLOW,
244 TextFormat::WHITE => Terminal::$COLOR_WHITE,
245 TextFormat::MINECOIN_GOLD => Terminal::$COLOR_MINECOIN_GOLD,
246 TextFormat::MATERIAL_QUARTZ => Terminal::$COLOR_MATERIAL_QUARTZ,
247 TextFormat::MATERIAL_IRON => Terminal::$COLOR_MATERIAL_IRON,
248 TextFormat::MATERIAL_NETHERITE => Terminal::$COLOR_MATERIAL_NETHERITE,
249 TextFormat::MATERIAL_REDSTONE => Terminal::$COLOR_MATERIAL_REDSTONE,
250 TextFormat::MATERIAL_COPPER => Terminal::$COLOR_MATERIAL_COPPER,
251 TextFormat::MATERIAL_GOLD => Terminal::$COLOR_MATERIAL_GOLD,
252 TextFormat::MATERIAL_EMERALD => Terminal::$COLOR_MATERIAL_EMERALD,
253 TextFormat::MATERIAL_DIAMOND => Terminal::$COLOR_MATERIAL_DIAMOND,
254 TextFormat::MATERIAL_LAPIS => Terminal::$COLOR_MATERIAL_LAPIS,
255 TextFormat::MATERIAL_AMETHYST => Terminal::$COLOR_MATERIAL_AMETHYST,
256 default => $token,
257 };
258 }
259
260 return $newString;
261 }
262
266 public static function write(string $line) : void{
267 echo self::toANSI($line);
268 }
269
274 public static function writeLine(string $line) : void{
275 echo self::toANSI($line) . self::$FORMAT_RESET . PHP_EOL;
276 }
277}
static toANSI(string $string)
Definition Terminal.php:221
static write(string $line)
Definition Terminal.php:266
static writeLine(string $line)
Definition Terminal.php:274
static tokenize(string $string)
static getOS(bool $recalculate=false)
Definition Utils.php:276