From 284ec069ad15d1d9bb01f4de8ed9fc7103df53c5 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 25 Jun 2014 18:09:47 +0200 Subject: [PATCH] implemented ANSI strlen helper issue #746 --- framework/helpers/BaseConsole.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/framework/helpers/BaseConsole.php b/framework/helpers/BaseConsole.php index e10967a188..2ac9f67476 100644 --- a/framework/helpers/BaseConsole.php +++ b/framework/helpers/BaseConsole.php @@ -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 ");