implemented ANSI strlen helper

issue #746
This commit is contained in:
Carsten Brandt
2014-06-25 18:09:47 +02:00
parent 708e6646b4
commit 284ec069ad

View File

@ -329,6 +329,15 @@ class BaseConsole
return preg_replace('/\033\[[\d;?]*\w/', '', $string);
}
/**
* Returns the length of the string without ANSI color codes.
* @param string $string the string to measure
* @return int the length of the string not counting ANSI format characters
*/
public static function ansiStrlen($string) {
return mb_strlen(static::stripAnsiFormat($string));
}
/**
* Converts an ANSI formatted string to HTML
*
@ -876,7 +885,7 @@ class BaseConsole
} else {
self::$_progressPrefix = $prefix;
}
$width -= mb_strlen($prefix);
$width -= static::ansiStrlen($prefix);
$percent = ($total == 0) ? 1 : $done / $total;
$info = sprintf("%d%% (%d/%d)", $percent * 100, $done, $total);
@ -888,7 +897,7 @@ class BaseConsole
$info .= sprintf(' ETA: %d sec.', $rate * ($total - $done));
}
$width -= 3 + mb_strlen($info);
$width -= 3 + static::ansiStrlen($info);
// skipping progress bar on very small display or if forced to skip
if ($width < 5) {
static::stdout("\r$prefix$info ");