fixed logic

This commit is contained in:
Fabien Potencier 2016-06-09 17:57:23 +02:00
parent a589635643
commit bf7a5c58a9
5 changed files with 12 additions and 24 deletions

View File

@ -36,7 +36,6 @@ use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Terminal;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**

View File

@ -413,9 +413,7 @@ class ProgressBar
$this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
}
$line = $this->buildLine();
$line = $this->adjustLineWidthToTerminalWidth($line);
$this->overwrite($line);
$this->overwrite($this->buildLine());
}
/**
@ -601,7 +599,8 @@ class ProgressBar
*/
private function buildLine()
{
return preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) {
$regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
$callback = function ($matches) {
if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
$text = call_user_func($formatter, $this, $this->output);
} elseif (isset($this->messages[$matches[1]])) {
@ -615,25 +614,17 @@ class ProgressBar
}
return $text;
}, $this->format);
}
};
$line = preg_replace_callback($regex, $callback, $this->format);
/**
* @param string $line
*
* @return bool
*/
private function adjustLineWidthToTerminalWidth($line)
{
$lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line);
$terminalWidth = $this->terminal->getWidth();
if ($lineLength > $terminalWidth) {
$newBarWidth = $this->barWidth - $lineLength + $terminalWidth;
$this->setBarWidth($newBarWidth);
return $this->buildLine();
if ($lineLength <= $terminalWidth) {
return $line;
}
return $line;
$this->setBarWidth($this->barWidth - $lineLength + $terminalWidth);
return preg_replace_callback($regex, $callback, $this->format);
}
}

View File

@ -11,7 +11,6 @@
namespace Symfony\Component\Console\Style;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Helper;

View File

@ -33,7 +33,7 @@ class Terminal
/**
* Sets the terminal width.
*
* @param int
* @param int $width
*/
public function setWidth($width)
{
@ -57,7 +57,7 @@ class Terminal
/**
* Sets the terminal height.
*
* @param int
* @param int $height
*/
public function setHeight($height)
{

View File

@ -14,7 +14,6 @@ namespace Symfony\Component\Console\Tests\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Terminal;
/**
* @group time-sensitive