mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-11 00:09:39 +00:00
Fixed --enable-ansi and --disable-ansi not being respected on threads
this causes some breakage to the behaviour of Terminal, and for that reason this is going on 4.0. Terminal::hasFormattingCodes() will no longer auto-detect whether colour codes are supported.
This commit is contained in:
parent
10ac322b8f
commit
498bffb34f
@ -183,7 +183,7 @@ namespace pocketmine {
|
|||||||
|
|
||||||
define('pocketmine\RESOURCE_PATH', \pocketmine\PATH . 'resources' . DIRECTORY_SEPARATOR);
|
define('pocketmine\RESOURCE_PATH', \pocketmine\PATH . 'resources' . DIRECTORY_SEPARATOR);
|
||||||
|
|
||||||
$opts = getopt("", ["data:", "plugins:", "no-wizard"]);
|
$opts = getopt("", ["data:", "plugins:", "no-wizard", "enable-ansi", "disable-ansi"]);
|
||||||
|
|
||||||
define('pocketmine\DATA', isset($opts["data"]) ? $opts["data"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR);
|
define('pocketmine\DATA', isset($opts["data"]) ? $opts["data"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR);
|
||||||
define('pocketmine\PLUGIN_PATH', isset($opts["plugins"]) ? $opts["plugins"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR);
|
define('pocketmine\PLUGIN_PATH', isset($opts["plugins"]) ? $opts["plugins"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR);
|
||||||
@ -195,6 +195,14 @@ namespace pocketmine {
|
|||||||
//Logger has a dependency on timezone
|
//Logger has a dependency on timezone
|
||||||
Timezone::init();
|
Timezone::init();
|
||||||
|
|
||||||
|
if(isset($opts["enable-ansi"])){
|
||||||
|
Terminal::init(true);
|
||||||
|
}elseif(isset($opts["disable-ansi"])){
|
||||||
|
Terminal::init(false);
|
||||||
|
}else{
|
||||||
|
Terminal::init();
|
||||||
|
}
|
||||||
|
|
||||||
$logger = new MainLogger(\pocketmine\DATA . "server.log");
|
$logger = new MainLogger(\pocketmine\DATA . "server.log");
|
||||||
$logger->registerStatic();
|
$logger->registerStatic();
|
||||||
\GlobalLogger::set($logger);
|
\GlobalLogger::set($logger);
|
||||||
|
@ -275,20 +275,18 @@ class MainLogger extends \AttachableThreadedLogger{
|
|||||||
|
|
||||||
$message = sprintf($this->format, $time->format("H:i:s"), $color, $threadName, $prefix, $message);
|
$message = sprintf($this->format, $time->format("H:i:s"), $color, $threadName, $prefix, $message);
|
||||||
|
|
||||||
$this->synchronized(function() use ($message, $level, $time) : void{
|
if(!Terminal::isInit()){
|
||||||
$cleanMessage = TextFormat::clean($message);
|
Terminal::init($this->mainThreadHasFormattingCodes); //lazy-init colour codes because we don't know if they've been registered on this thread
|
||||||
|
}
|
||||||
|
|
||||||
if($this->mainThreadHasFormattingCodes and Terminal::hasFormattingCodes()){ //hasFormattingCodes() lazy-inits colour codes because we don't know if they've been registered on this thread
|
$this->synchronized(function() use ($message, $level, $time) : void{
|
||||||
echo Terminal::toANSI($message) . PHP_EOL;
|
echo Terminal::toANSI($message) . PHP_EOL;
|
||||||
}else{
|
|
||||||
echo $cleanMessage . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($this->attachments as $attachment){
|
foreach($this->attachments as $attachment){
|
||||||
$attachment->call($level, $message);
|
$attachment->call($level, $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->logStream[] = $time->format("Y-m-d") . " " . $cleanMessage . PHP_EOL;
|
$this->logStream[] = $time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,31 +49,29 @@ abstract class Terminal{
|
|||||||
public static $COLOR_YELLOW = "";
|
public static $COLOR_YELLOW = "";
|
||||||
public static $COLOR_WHITE = "";
|
public static $COLOR_WHITE = "";
|
||||||
|
|
||||||
|
/** @var bool|null */
|
||||||
private static $formattingCodes = null;
|
private static $formattingCodes = null;
|
||||||
|
|
||||||
public static function hasFormattingCodes(){
|
public static function hasFormattingCodes() : bool{
|
||||||
if(self::$formattingCodes === null){
|
if(self::$formattingCodes === null){
|
||||||
$opts = getopt("", ["enable-ansi", "disable-ansi"]);
|
throw new \InvalidStateException("Formatting codes have not been initialized");
|
||||||
if(isset($opts["disable-ansi"])){
|
|
||||||
self::$formattingCodes = false;
|
|
||||||
}else{
|
|
||||||
$stdout = fopen("php://stdout", "w");
|
|
||||||
self::$formattingCodes = (isset($opts["enable-ansi"]) or ( //user explicitly told us to enable ANSI
|
|
||||||
stream_isatty($stdout) and //STDOUT isn't being piped
|
|
||||||
(
|
|
||||||
getenv('TERM') !== false or //Console says it supports colours
|
|
||||||
(function_exists('sapi_windows_vt100_support') and sapi_windows_vt100_support($stdout)) //we're on windows and have vt100 support
|
|
||||||
)
|
|
||||||
));
|
|
||||||
fclose($stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
self::init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$formattingCodes;
|
return self::$formattingCodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function detectFormattingCodesSupport() : bool{
|
||||||
|
$stdout = fopen("php://stdout", "w");
|
||||||
|
$result = (
|
||||||
|
stream_isatty($stdout) and //STDOUT isn't being piped
|
||||||
|
(
|
||||||
|
getenv('TERM') !== false or //Console says it supports colours
|
||||||
|
(function_exists('sapi_windows_vt100_support') and sapi_windows_vt100_support($stdout)) //we're on windows and have vt100 support
|
||||||
|
)
|
||||||
|
);
|
||||||
|
fclose($stdout);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
protected static function getFallbackEscapeCodes(){
|
protected static function getFallbackEscapeCodes(){
|
||||||
self::$FORMAT_BOLD = "\x1b[1m";
|
self::$FORMAT_BOLD = "\x1b[1m";
|
||||||
self::$FORMAT_OBFUSCATED = "";
|
self::$FORMAT_OBFUSCATED = "";
|
||||||
@ -140,8 +138,9 @@ abstract class Terminal{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function init(){
|
public static function init(?bool $enableFormatting = null) : void{
|
||||||
if(!self::hasFormattingCodes()){
|
self::$formattingCodes = $enableFormatting ?? self::detectFormattingCodesSupport();
|
||||||
|
if(!self::$formattingCodes){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,6 +160,10 @@ abstract class Terminal{
|
|||||||
//TODO: iOS
|
//TODO: iOS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function isInit() : bool{
|
||||||
|
return self::$formattingCodes !== null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string with colorized ANSI Escape codes for the current terminal
|
* 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.
|
* Note that this is platform-dependent and might produce different results depending on the terminal type and/or OS.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user