[Console] Do not duplicate Helper::strlen() code
This commit is contained in:
parent
62cb443770
commit
01c2c099a4
@ -15,6 +15,7 @@ use Symfony\Component\Console\Descriptor\TextDescriptor;
|
|||||||
use Symfony\Component\Console\Descriptor\XmlDescriptor;
|
use Symfony\Component\Console\Descriptor\XmlDescriptor;
|
||||||
use Symfony\Component\Console\Formatter\OutputFormatter;
|
use Symfony\Component\Console\Formatter\OutputFormatter;
|
||||||
use Symfony\Component\Console\Helper\DebugFormatterHelper;
|
use Symfony\Component\Console\Helper\DebugFormatterHelper;
|
||||||
|
use Symfony\Component\Console\Helper\Helper;
|
||||||
use Symfony\Component\Console\Helper\ProcessHelper;
|
use Symfony\Component\Console\Helper\ProcessHelper;
|
||||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
@ -653,7 +654,7 @@ class Application
|
|||||||
do {
|
do {
|
||||||
$title = sprintf(' [%s] ', get_class($e));
|
$title = sprintf(' [%s] ', get_class($e));
|
||||||
|
|
||||||
$len = $this->stringWidth($title);
|
$len = Helper::strlen($title);
|
||||||
|
|
||||||
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
|
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
|
||||||
// HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
|
// HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
|
||||||
@ -664,7 +665,7 @@ class Application
|
|||||||
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
|
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
|
||||||
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
|
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
|
||||||
// pre-format lines to get the right string length
|
// pre-format lines to get the right string length
|
||||||
$lineLength = $this->stringWidth($line) + 4;
|
$lineLength = Helper::strlen($line) + 4;
|
||||||
$lines[] = array($line, $lineLength);
|
$lines[] = array($line, $lineLength);
|
||||||
|
|
||||||
$len = max($lineLength, $len);
|
$len = max($lineLength, $len);
|
||||||
@ -673,7 +674,7 @@ class Application
|
|||||||
|
|
||||||
$messages = array();
|
$messages = array();
|
||||||
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
|
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
|
||||||
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title))));
|
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
|
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
|
||||||
}
|
}
|
||||||
@ -1086,19 +1087,6 @@ class Application
|
|||||||
$this->defaultCommand = $commandName;
|
$this->defaultCommand = $commandName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function stringWidth($string)
|
|
||||||
{
|
|
||||||
if (!function_exists('mb_strwidth')) {
|
|
||||||
return strlen($string);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (false === $encoding = mb_detect_encoding($string, null, true)) {
|
|
||||||
return strlen($string);
|
|
||||||
}
|
|
||||||
|
|
||||||
return mb_strwidth($string, $encoding);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function splitStringByWidth($string, $width)
|
private function splitStringByWidth($string, $width)
|
||||||
{
|
{
|
||||||
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
|
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
|
||||||
|
Reference in New Issue
Block a user