public static function renderColoredString($string, $colored = true)
{
// TODO rework/refactor according to https://github.com/yiisoft/yii2/issues/746
static $conversions = ['%y' => [self::FG_YELLOW], '%g' => [self::FG_GREEN], '%b' => [self::FG_BLUE], '%r' => [self::FG_RED], '%p' => [self::FG_PURPLE], '%m' => [self::FG_PURPLE], '%c' => [self::FG_CYAN], '%w' => [self::FG_GREY], '%k' => [self::FG_BLACK], '%n' => [0], '%Y' => [self::FG_YELLOW, self::BOLD], '%G' => [self::FG_GREEN, self::BOLD], '%B' => [self::FG_BLUE, self::BOLD], '%R' => [self::FG_RED, self::BOLD], '%P' => [self::FG_PURPLE, self::BOLD], '%M' => [self::FG_PURPLE, self::BOLD], '%C' => [self::FG_CYAN, self::BOLD], '%W' => [self::FG_GREY, self::BOLD], '%K' => [self::FG_BLACK, self::BOLD], '%N' => [0, self::BOLD], '%3' => [self::BG_YELLOW], '%2' => [self::BG_GREEN], '%4' => [self::BG_BLUE], '%1' => [self::BG_RED], '%5' => [self::BG_PURPLE], '%6' => [self::BG_PURPLE], '%7' => [self::BG_CYAN], '%0' => [self::BG_GREY], '%F' => [self::BLINK], '%U' => [self::UNDERLINE], '%8' => [self::NEGATIVE], '%9' => [self::BOLD], '%_' => [self::BOLD]];
if ($colored) {
$string = str_replace('%%', '% ', $string);
foreach ($conversions as $key => $value) {
$string = str_replace($key, static::ansiFormatCode($value), $string);
}
$string = str_replace('% ', '%', $string);
} else {
$string = preg_replace('/%((%)|.)/', '$2', $string);
}
return $string;
}