Fixed the line length of the new Symfony Styles

This commit is contained in:
Javier Eguiluz 2015-03-31 15:14:18 +02:00 committed by Fabien Potencier
parent 75c8a2ba21
commit e9c7912f57

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\Console\Style; namespace Symfony\Component\Console\Style;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBar;
@ -34,6 +35,7 @@ class SymfonyStyle extends OutputStyle
private $input; private $input;
private $questionHelper; private $questionHelper;
private $progressBar; private $progressBar;
private $lineLength;
/** /**
* @param InputInterface $input * @param InputInterface $input
@ -42,6 +44,7 @@ class SymfonyStyle extends OutputStyle
public function __construct(InputInterface $input, OutputInterface $output) public function __construct(InputInterface $input, OutputInterface $output)
{ {
$this->input = $input; $this->input = $input;
$this->lineLength = min($this->getTerminalWidth(), self::MAX_LINE_LENGTH);
parent::__construct($output); parent::__construct($output);
} }
@ -68,7 +71,7 @@ class SymfonyStyle extends OutputStyle
// wrap and add newlines for each element // wrap and add newlines for each element
foreach ($messages as $key => $message) { foreach ($messages as $key => $message) {
$message = OutputFormatter::escape($message); $message = OutputFormatter::escape($message);
$lines = array_merge($lines, explode("\n", wordwrap($message, self::MAX_LINE_LENGTH - Helper::strlen($prefix)))); $lines = array_merge($lines, explode("\n", wordwrap($message, $this->lineLength - Helper::strlen($prefix))));
if (count($messages) > 1 && $key < count($message)) { if (count($messages) > 1 && $key < count($message)) {
$lines[] = ''; $lines[] = '';
@ -82,7 +85,7 @@ class SymfonyStyle extends OutputStyle
foreach ($lines as &$line) { foreach ($lines as &$line) {
$line = sprintf('%s%s', $prefix, $line); $line = sprintf('%s%s', $prefix, $line);
$line .= str_repeat(' ', self::MAX_LINE_LENGTH - Helper::strlen($line)); $line .= str_repeat(' ', $this->lineLength - Helper::strlen($line));
if ($style) { if ($style) {
$line = sprintf('<%s>%s</>', $style, $line); $line = sprintf('<%s>%s</>', $style, $line);
@ -307,4 +310,12 @@ class SymfonyStyle extends OutputStyle
return $this->progressBar; return $this->progressBar;
} }
private function getTerminalWidth()
{
$application = new Application();
list ($width, $height) = $application->getTerminalDimensions();
return $width;
}
} }