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:
Dylan K. Taylor 2018-12-29 11:23:32 +00:00
parent 86c4e936cb
commit c2c210e25a
3 changed files with 38 additions and 29 deletions

View File

@ -177,7 +177,7 @@ namespace pocketmine {
define('pocketmine\RESOURCE_PATH', \pocketmine\PATH . 'src' . DIRECTORY_SEPARATOR . 'pocketmine' . DIRECTORY_SEPARATOR . '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\PLUGIN_PATH', isset($opts["plugins"]) ? $opts["plugins"] . DIRECTORY_SEPARATOR : realpath(getcwd()) . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR);
@ -197,6 +197,14 @@ namespace pocketmine {
//Logger has a dependency on timezone
$tzError = 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->registerStatic();

View File

@ -302,20 +302,18 @@ class MainLogger extends \AttachableThreadedLogger{
$message = sprintf($this->format, $time->format("H:i:s"), $color, $threadName, $prefix, $message);
$this->synchronized(function() use ($message, $level, $time) : void{
$cleanMessage = TextFormat::clean($message);
if(!Terminal::isInit()){
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
echo Terminal::toANSI($message) . PHP_EOL;
}else{
echo $cleanMessage . PHP_EOL;
}
$this->synchronized(function() use ($message, $level, $time) : void{
echo Terminal::toANSI($message) . PHP_EOL;
foreach($this->attachments as $attachment){
$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;
});
}

View File

@ -57,31 +57,29 @@ abstract class Terminal{
public static $COLOR_YELLOW = "";
public static $COLOR_WHITE = "";
/** @var bool|null */
private static $formattingCodes = null;
public static function hasFormattingCodes(){
public static function hasFormattingCodes() : bool{
if(self::$formattingCodes === null){
$opts = getopt("", ["enable-ansi", "disable-ansi"]);
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();
throw new \InvalidStateException("Formatting codes have not been initialized");
}
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(){
self::$FORMAT_BOLD = "\x1b[1m";
self::$FORMAT_OBFUSCATED = "";
@ -148,8 +146,9 @@ abstract class Terminal{
}
}
public static function init(){
if(!self::hasFormattingCodes()){
public static function init(?bool $enableFormatting = null) : void{
self::$formattingCodes = $enableFormatting ?? self::detectFormattingCodesSupport();
if(!self::$formattingCodes){
return;
}
@ -169,6 +168,10 @@ abstract class Terminal{
//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.