Fix #16831: Fix console Table Widget does not render correctly in combination with ANSI formatting

This commit is contained in:
Ivan Sidorov
2020-10-23 22:21:03 +03:00
committed by GitHub
parent ed1c087784
commit cd36f505b1
5 changed files with 290 additions and 21 deletions

View File

@ -331,7 +331,7 @@ class BaseConsole
*/
public static function stripAnsiFormat($string)
{
return preg_replace('/\033\[[\d;?]*\w/', '', $string);
return preg_replace(self::ansiCodesPattern(), '', $string);
}
/**
@ -355,6 +355,67 @@ class BaseConsole
return mb_strwidth(static::stripAnsiFormat($string), Yii::$app->charset);
}
/**
* Returns the portion with ANSI color codes of string specified by the start and length parameters.
* If string has color codes, then will be return "TEXT_COLOR + TEXT_STRING + DEFAULT_COLOR",
* else will be simple "TEXT_STRING".
* @param string $string
* @param int $start
* @param int $length
* @return string
*/
public static function ansiColorizedSubstr($string, $start, $length)
{
if ($start < 0 || $length <= 0) {
return '';
}
$textItems = preg_split(self::ansiCodesPattern(), $string);
preg_match_all(self::ansiCodesPattern(), $string, $colors);
$colors = count($colors) ? $colors[0] : [];
array_unshift($colors, '');
$result = '';
$curPos = 0;
$inRange = false;
foreach ($textItems as $k => $textItem) {
$color = $colors[$k];
if ($curPos <= $start && $start < $curPos + Console::ansiStrwidth($textItem)) {
$text = mb_substr($textItem, $start - $curPos, null, Yii::$app->charset);
$inRange = true;
} else {
$text = $textItem;
}
if ($inRange) {
$result .= $color . $text;
$diff = $length - Console::ansiStrwidth($result);
if ($diff <= 0) {
if ($diff < 0) {
$result = mb_substr($result, 0, $diff, Yii::$app->charset);
}
$defaultColor = static::renderColoredString('%n');
if ($color && $color != $defaultColor) {
$result .= $defaultColor;
}
break;
}
}
$curPos += mb_strlen($textItem, Yii::$app->charset);
}
return $result;
}
private static function ansiCodesPattern()
{
return /** @lang PhpRegExp */ '/\033\[[\d;?]*\w/';
}
/**
* Converts an ANSI formatted string to HTML.
*