TextFormat: Added MINECOIN_GOLD (§g) color code support (#4670)

This commit is contained in:
xxAROX 2021-12-31 00:53:05 +01:00 committed by GitHub
parent 62afa2f28d
commit 193a1b3f4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -58,6 +58,7 @@ abstract class Terminal{
public static string $COLOR_LIGHT_PURPLE = ""; public static string $COLOR_LIGHT_PURPLE = "";
public static string $COLOR_YELLOW = ""; public static string $COLOR_YELLOW = "";
public static string $COLOR_WHITE = ""; public static string $COLOR_WHITE = "";
public static string $COLOR_MINECOIN_GOLD = "";
/** @var bool|null */ /** @var bool|null */
private static $formattingCodes = null; private static $formattingCodes = null;
@ -110,6 +111,7 @@ abstract class Terminal{
self::$COLOR_LIGHT_PURPLE = $color(207); self::$COLOR_LIGHT_PURPLE = $color(207);
self::$COLOR_YELLOW = $color(227); self::$COLOR_YELLOW = $color(227);
self::$COLOR_WHITE = $color(231); self::$COLOR_WHITE = $color(231);
self::$COLOR_MINECOIN_GOLD = $color(184);
} }
protected static function getEscapeCodes() : void{ protected static function getEscapeCodes() : void{
@ -142,11 +144,12 @@ abstract class Terminal{
self::$COLOR_LIGHT_PURPLE = $colors >= 256 ? $setaf(207) : $setaf(13); self::$COLOR_LIGHT_PURPLE = $colors >= 256 ? $setaf(207) : $setaf(13);
self::$COLOR_YELLOW = $colors >= 256 ? $setaf(227) : $setaf(11); self::$COLOR_YELLOW = $colors >= 256 ? $setaf(227) : $setaf(11);
self::$COLOR_WHITE = $colors >= 256 ? $setaf(231) : $setaf(15); self::$COLOR_WHITE = $colors >= 256 ? $setaf(231) : $setaf(15);
self::$COLOR_MINECOIN_GOLD = $colors >= 256 ? $setaf(184) : $setaf(11);
}else{ }else{
self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = $setaf(0); self::$COLOR_BLACK = self::$COLOR_DARK_GRAY = $setaf(0);
self::$COLOR_RED = self::$COLOR_DARK_RED = $setaf(1); self::$COLOR_RED = self::$COLOR_DARK_RED = $setaf(1);
self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = $setaf(2); self::$COLOR_GREEN = self::$COLOR_DARK_GREEN = $setaf(2);
self::$COLOR_YELLOW = self::$COLOR_GOLD = $setaf(3); self::$COLOR_YELLOW = self::$COLOR_GOLD = self::$COLOR_MINECOIN_GOLD = $setaf(3);
self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = $setaf(4); self::$COLOR_BLUE = self::$COLOR_DARK_BLUE = $setaf(4);
self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = $setaf(5); self::$COLOR_LIGHT_PURPLE = self::$COLOR_PURPLE = $setaf(5);
self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = $setaf(6); self::$COLOR_AQUA = self::$COLOR_DARK_AQUA = $setaf(6);
@ -210,6 +213,7 @@ abstract class Terminal{
TextFormat::LIGHT_PURPLE => Terminal::$COLOR_LIGHT_PURPLE, TextFormat::LIGHT_PURPLE => Terminal::$COLOR_LIGHT_PURPLE,
TextFormat::YELLOW => Terminal::$COLOR_YELLOW, TextFormat::YELLOW => Terminal::$COLOR_YELLOW,
TextFormat::WHITE => Terminal::$COLOR_WHITE, TextFormat::WHITE => Terminal::$COLOR_WHITE,
TextFormat::MINECOIN_GOLD => Terminal::$COLOR_MINECOIN_GOLD,
default => $token, default => $token,
}; };
} }

View File

@ -62,6 +62,7 @@ abstract class TextFormat{
public const LIGHT_PURPLE = TextFormat::ESCAPE . "d"; public const LIGHT_PURPLE = TextFormat::ESCAPE . "d";
public const YELLOW = TextFormat::ESCAPE . "e"; public const YELLOW = TextFormat::ESCAPE . "e";
public const WHITE = TextFormat::ESCAPE . "f"; public const WHITE = TextFormat::ESCAPE . "f";
public const MINECOIN_GOLD = TextFormat::ESCAPE . "g";
public const COLORS = [ public const COLORS = [
self::BLACK => self::BLACK, self::BLACK => self::BLACK,
@ -80,6 +81,7 @@ abstract class TextFormat{
self::LIGHT_PURPLE => self::LIGHT_PURPLE, self::LIGHT_PURPLE => self::LIGHT_PURPLE,
self::YELLOW => self::YELLOW, self::YELLOW => self::YELLOW,
self::WHITE => self::WHITE, self::WHITE => self::WHITE,
self::MINECOIN_GOLD => self::MINECOIN_GOLD,
]; ];
public const OBFUSCATED = TextFormat::ESCAPE . "k"; public const OBFUSCATED = TextFormat::ESCAPE . "k";
@ -128,7 +130,7 @@ abstract class TextFormat{
* @return string[] * @return string[]
*/ */
public static function tokenize(string $string) : array{ public static function tokenize(string $string) : array{
$result = preg_split("/(" . TextFormat::ESCAPE . "[0-9a-fk-or])/u", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $result = preg_split("/(" . TextFormat::ESCAPE . "[0-9a-gk-or])/u", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
if($result === false) throw self::makePcreError(); if($result === false) throw self::makePcreError();
return $result; return $result;
} }
@ -142,7 +144,7 @@ abstract class TextFormat{
$string = mb_scrub($string, 'UTF-8'); $string = mb_scrub($string, 'UTF-8');
$string = self::preg_replace("/[\x{E000}-\x{F8FF}]/u", "", $string); //remove unicode private-use-area characters (they might break the console) $string = self::preg_replace("/[\x{E000}-\x{F8FF}]/u", "", $string); //remove unicode private-use-area characters (they might break the console)
if($removeFormat){ if($removeFormat){
$string = str_replace(TextFormat::ESCAPE, "", self::preg_replace("/" . TextFormat::ESCAPE . "[0-9a-fk-or]/u", "", $string)); $string = str_replace(TextFormat::ESCAPE, "", self::preg_replace("/" . TextFormat::ESCAPE . "[0-9a-gk-or]/u", "", $string));
} }
return str_replace("\x1b", "", self::preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/u", "", $string)); return str_replace("\x1b", "", self::preg_replace("/\x1b[\\(\\][[0-9;\\[\\(]+[Bm]/u", "", $string));
} }
@ -153,7 +155,7 @@ abstract class TextFormat{
* @param string $placeholder default "&" * @param string $placeholder default "&"
*/ */
public static function colorize(string $string, string $placeholder = "&") : string{ public static function colorize(string $string, string $placeholder = "&") : string{
return self::preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-fk-or])/u', TextFormat::ESCAPE . '$1', $string); return self::preg_replace('/' . preg_quote($placeholder, "/") . '([0-9a-gk-or])/u', TextFormat::ESCAPE . '$1', $string);
} }
/** /**
@ -254,6 +256,10 @@ abstract class TextFormat{
$newString .= "<span style=color:#FFF>"; $newString .= "<span style=color:#FFF>";
++$tokens; ++$tokens;
break; break;
case TextFormat::MINECOIN_GOLD:
$newString .= "<span style=color:#dd0>";
++$tokens;
break;
default: default:
$newString .= $token; $newString .= $token;
break; break;