PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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
63 private static ?bool $formattingCodes = null;
64
65 public static function hasFormattingCodes() : bool{
66 if(self::$formattingCodes === null){
67 throw new \LogicException("Formatting codes have not been initialized");
68 }
69 return self::$formattingCodes;
70 }
71
72 private static function detectFormattingCodesSupport() : bool{
73 $stdout = fopen("php://stdout", "w");
74 if($stdout === false) throw new AssumptionFailedError("Opening php://stdout should never fail");
75 $result = (
76 stream_isatty($stdout) && //STDOUT isn't being piped
77 (
78 getenv('TERM') !== false || //Console says it supports colours
79 (function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support($stdout)) //we're on windows and have vt100 support
80 )
81 );
82 fclose($stdout);
83 return $result;
84 }
85
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";
92
93 self::$FORMAT_RESET = "\x1b[m";
94
95 $color = fn(int $code) => "\x1b[38;5;{$code}m";
96
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);
114 }
115
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");
119
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"; //`tput `;
125
126 self::$FORMAT_RESET = $tput("sgr0");
127
128 $colors = (int) $tput("colors");
129 if($colors > 8){
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);
147 }else{
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);
156 }
157 }
158
159 public static function init(?bool $enableFormatting = null) : void{
160 self::$formattingCodes = $enableFormatting ?? self::detectFormattingCodesSupport();
161 if(!self::$formattingCodes){
162 return;
163 }
164
165 switch(Utils::getOS()){
166 case Utils::OS_LINUX:
167 case Utils::OS_MACOS:
168 case Utils::OS_BSD:
169 if(getenv('TERM') !== false){
170 self::getEscapeCodes();
171 return;
172 }
173
174 case Utils::OS_WINDOWS:
175 case Utils::OS_ANDROID:
176 self::getFallbackEscapeCodes();
177 return;
178 }
179
180 //TODO: iOS
181 }
182
183 public static function isInit() : bool{
184 return self::$formattingCodes !== null;
185 }
186
191 public static function toANSI(string $string) : string{
192 $newString = "";
193 foreach(TextFormat::tokenize($string) as $token){
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,
218 default => $token,
219 };
220 }
221
222 return $newString;
223 }
224
228 public static function write(string $line) : void{
229 echo self::toANSI($line);
230 }
231
236 public static function writeLine(string $line) : void{
237 echo self::toANSI($line) . self::$FORMAT_RESET . PHP_EOL;
238 }
239}
static toANSI(string $string)
Definition Terminal.php:191
static write(string $line)
Definition Terminal.php:228
static writeLine(string $line)
Definition Terminal.php:236
static tokenize(string $string)
static getOS(bool $recalculate=false)
Definition Utils.php:275