Introduce and use TextFormat::addBase() (#5268)

This function adds "base" format to a string. The given formats are inserted directly after any RESET code in the sequence.

An example of where this is needed is in the logger.

Without this change, the following code:
$logger->notice("I'm a " . TextFormat::RED . "special" . TextFormat::RESET . " cookie");

causes the "cookie" part of the message to show as grey, instead of the expected aqua for NOTICE level messages.

There are also many workarounds for this problem throughout the server, mostly in command outputs, being forced to use WHITE instead of RESET to avoid breaking the logger output.
This commit is contained in:
Dylan T 2022-09-28 16:13:11 +01:00 committed by GitHub
parent 86a2f8e360
commit b87e4d8bd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 47 additions and 21 deletions

View File

@ -1712,7 +1712,7 @@ class Server{
$session = $player->getNetworkSession();
$position = $player->getPosition();
$this->logger->info($this->language->translate(KnownTranslationFactory::pocketmine_player_logIn(
TextFormat::AQUA . $player->getName() . TextFormat::WHITE,
TextFormat::AQUA . $player->getName() . TextFormat::RESET,
$session->getIp(),
(string) $session->getPort(),
(string) $player->getId(),

View File

@ -63,7 +63,7 @@ class GarbageCollectorCommand extends VanillaCommand{
$cyclesCollected = $sender->getServer()->getMemoryManager()->triggerGarbageCollector();
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_gc_header()->format(TextFormat::GREEN . "---- " . TextFormat::WHITE, TextFormat::GREEN . " ----" . TextFormat::WHITE));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_gc_header()->format(TextFormat::GREEN . "---- " . TextFormat::RESET, TextFormat::GREEN . " ----" . TextFormat::RESET));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_gc_chunks(TextFormat::RED . number_format($chunksCollected))->prefix(TextFormat::GOLD));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_gc_entities(TextFormat::RED . number_format($entitiesCollected))->prefix(TextFormat::GOLD));

View File

@ -95,7 +95,7 @@ class HelpCommand extends VanillaCommand{
foreach($commands[$pageNumber - 1] as $command){
$description = $command->getDescription();
$descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description;
$sender->sendMessage(TextFormat::DARK_GREEN . "/" . $command->getName() . ": " . TextFormat::WHITE . $descriptionString);
$sender->sendMessage(TextFormat::DARK_GREEN . "/" . $command->getName() . ": " . TextFormat::RESET . $descriptionString);
}
}
@ -107,18 +107,18 @@ class HelpCommand extends VanillaCommand{
$description = $cmd->getDescription();
$descriptionString = $description instanceof Translatable ? $lang->translate($description) : $description;
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_header($cmd->getName())
->format(TextFormat::YELLOW . "--------- " . TextFormat::WHITE, TextFormat::YELLOW . " ---------"));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_description(TextFormat::WHITE . $descriptionString)
->format(TextFormat::YELLOW . "--------- " . TextFormat::RESET, TextFormat::YELLOW . " ---------"));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_description(TextFormat::RESET . $descriptionString)
->prefix(TextFormat::GOLD));
$usage = $cmd->getUsage();
$usageString = $usage instanceof Translatable ? $lang->translate($usage) : $usage;
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_usage(TextFormat::WHITE . implode("\n" . TextFormat::WHITE, explode("\n", $usageString)))
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_usage(TextFormat::RESET . implode("\n" . TextFormat::RESET, explode("\n", $usageString)))
->prefix(TextFormat::GOLD));
$aliases = $cmd->getAliases();
sort($aliases, SORT_NATURAL);
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_aliases(TextFormat::WHITE . implode(", ", $aliases))
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_help_specificCommand_aliases(TextFormat::RESET . implode(", ", $aliases))
->prefix(TextFormat::GOLD));
return true;

View File

@ -52,7 +52,7 @@ class MeCommand extends VanillaCommand{
throw new InvalidCommandSyntaxException();
}
$sender->getServer()->broadcastMessage(KnownTranslationFactory::chat_type_emote($sender instanceof Player ? $sender->getDisplayName() : $sender->getName(), TextFormat::WHITE . implode(" ", $args)));
$sender->getServer()->broadcastMessage(KnownTranslationFactory::chat_type_emote($sender instanceof Player ? $sender->getDisplayName() : $sender->getName(), TextFormat::RESET . implode(" ", $args)));
return true;
}

View File

@ -56,7 +56,7 @@ class PluginsCommand extends VanillaCommand{
}, $sender->getServer()->getPluginManager()->getPlugins());
sort($list, SORT_STRING);
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_plugins_success((string) count($list), implode(TextFormat::WHITE . ", ", $list)));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_plugins_success((string) count($list), implode(TextFormat::RESET . ", ", $list)));
return true;
}
}

View File

@ -52,7 +52,7 @@ class StatusCommand extends VanillaCommand{
$mUsage = Process::getAdvancedMemoryUsage();
$server = $sender->getServer();
$sender->sendMessage(TextFormat::GREEN . "---- " . TextFormat::WHITE . "Server status" . TextFormat::GREEN . " ----");
$sender->sendMessage(TextFormat::GREEN . "---- " . TextFormat::RESET . "Server status" . TextFormat::GREEN . " ----");
$time = (int) (microtime(true) - $server->getStartTime());

View File

@ -57,17 +57,18 @@ class VersionCommand extends VanillaCommand{
if(count($args) === 0){
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_serverSoftwareName(
VersionInfo::NAME
TextFormat::GREEN . VersionInfo::NAME . TextFormat::RESET
));
$versionColor = VersionInfo::IS_DEVELOPMENT_BUILD ? TextFormat::YELLOW : TextFormat::GREEN;
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_serverSoftwareVersion(
VersionInfo::VERSION()->getFullVersion(),
VersionInfo::GIT_HASH()
$versionColor . VersionInfo::VERSION()->getFullVersion() . TextFormat::RESET,
TextFormat::GREEN . VersionInfo::GIT_HASH() . TextFormat::RESET
));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_minecraftVersion(
ProtocolInfo::MINECRAFT_VERSION_NETWORK,
(string) ProtocolInfo::CURRENT_PROTOCOL
TextFormat::GREEN . ProtocolInfo::MINECRAFT_VERSION_NETWORK . TextFormat::RESET,
TextFormat::GREEN . ProtocolInfo::CURRENT_PROTOCOL . TextFormat::RESET
));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_phpVersion(PHP_VERSION));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_phpVersion(TextFormat::GREEN . PHP_VERSION . TextFormat::RESET));
$jitMode = Utils::getOpcacheJitMode();
if($jitMode !== null){
@ -79,8 +80,8 @@ class VersionCommand extends VanillaCommand{
}else{
$jitStatus = KnownTranslationFactory::pocketmine_command_version_phpJitNotSupported();
}
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_phpJitStatus($jitStatus));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_operatingSystem(Utils::getOS()));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_phpJitStatus($jitStatus->format(TextFormat::GREEN, TextFormat::RESET)));
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_operatingSystem(TextFormat::GREEN . Utils::getOS() . TextFormat::RESET));
}else{
$pluginName = implode(" ", $args);
$exactPlugin = $sender->getServer()->getPluginManager()->getPlugin($pluginName);
@ -110,7 +111,7 @@ class VersionCommand extends VanillaCommand{
private function describeToSender(Plugin $plugin, CommandSender $sender) : void{
$desc = $plugin->getDescription();
$sender->sendMessage(TextFormat::DARK_GREEN . $desc->getName() . TextFormat::WHITE . " version " . TextFormat::DARK_GREEN . $desc->getVersion());
$sender->sendMessage(TextFormat::DARK_GREEN . $desc->getName() . TextFormat::RESET . " version " . TextFormat::DARK_GREEN . $desc->getVersion());
if($desc->getDescription() !== ""){
$sender->sendMessage($desc->getDescription());

View File

@ -66,7 +66,7 @@ class ConsoleCommandSender implements CommandSender{
}
foreach(explode("\n", trim($message)) as $line){
Terminal::writeLine(TextFormat::GREEN . "Command output | " . TextFormat::WHITE . $line);
Terminal::writeLine(TextFormat::GREEN . "Command output | " . TextFormat::addBase(TextFormat::WHITE, $line));
}
}

View File

@ -191,7 +191,7 @@ class MainLogger extends \AttachableThreadedLogger implements \BufferedLogger{
$threadName = (new \ReflectionClass($thread))->getShortName() . " thread";
}
$message = sprintf($this->format, $time->format("H:i:s.v"), $color, $threadName, $prefix, TextFormat::clean($message, false));
$message = sprintf($this->format, $time->format("H:i:s.v"), $color, $threadName, $prefix, TextFormat::addBase($color, TextFormat::clean($message, false)));
if(!Terminal::isInit()){
Terminal::init($this->useFormattingCodes); //lazy-init colour codes because we don't know if they've been registered on this thread

View File

@ -158,6 +158,31 @@ abstract class TextFormat{
return self::preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-gk-or])/u', TextFormat::ESCAPE . '$1', $string);
}
/**
* Adds base formatting to the string. The given format codes will be inserted directly after any RESET (§r) codes.
*
* This is useful for log messages, where a RESET code should return to the log message's original colour (e.g.
* blue for NOTICE), rather than whatever the terminal's base text colour is (usually some off-white colour).
*
* Example behaviour:
* - Base format "§c" (red) + "Hello" (no format) = "§r§cHello"
* - Base format "§c" + "Hello §rWorld" = "§r§cHello §r§cWorld"
*
* Note: Adding base formatting to the output string a second time will result in a combination of formats from both
* calls. This is not by design, but simply a consequence of the way the function is implemented.
*/
public static function addBase(string $baseFormat, string $string) : string{
$baseFormatParts = self::tokenize($baseFormat);
foreach($baseFormatParts as $part){
if(!isset(self::FORMATS[$part]) && !isset(self::COLORS[$part])){
throw new \InvalidArgumentException("Unexpected base format token \"$part\", expected only color and format tokens");
}
}
$baseFormat = self::RESET . $baseFormat;
return $baseFormat . str_replace(TextFormat::RESET, $baseFormat, $string);
}
/**
* Returns an HTML-formatted string with colors/markup
*/