diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 166b7c458e948..ceae5408e8697 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -35,6 +35,7 @@ use Symfony\Component\Console\Event\ConsoleTerminateEvent; use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\Console\Exception\LogicException; +use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -64,19 +65,23 @@ class Application private $definition; private $helperSet; private $dispatcher; - private $terminalDimensions; private $defaultCommand; /** - * Constructor. - * - * @param string $name The name of the application - * @param string $version The version of the application + * @var TerminalDimensionsProvider */ - public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') + private $terminalDimensionsProvider; + + /** + * @param string $name The name of the application + * @param string $version The version of the application + * @param TerminalDimensionsProvider $terminalDimensionsProvider + */ + public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', TerminalDimensionsProvider $terminalDimensionsProvider = null) { $this->name = $name; $this->version = $version; + $this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider(); $this->defaultCommand = 'list'; $this->helperSet = $this->getDefaultHelperSet(); $this->definition = $this->getDefaultInputDefinition(); @@ -656,9 +661,7 @@ public function renderException(\Exception $e, OutputInterface $output) */ protected function getTerminalWidth() { - $dimensions = $this->getTerminalDimensions(); - - return $dimensions[0]; + return $this->terminalDimensionsProvider->getTerminalWidth(); } /** @@ -668,9 +671,7 @@ protected function getTerminalWidth() */ protected function getTerminalHeight() { - $dimensions = $this->getTerminalDimensions(); - - return $dimensions[1]; + return $this->terminalDimensionsProvider->getTerminalWidth(); } /** @@ -680,33 +681,7 @@ protected function getTerminalHeight() */ public function getTerminalDimensions() { - if ($this->terminalDimensions) { - return $this->terminalDimensions; - } - - if ('\\' === DIRECTORY_SEPARATOR) { - // extract [w, H] from "wxh (WxH)" - if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) { - return array((int) $matches[1], (int) $matches[2]); - } - // extract [w, h] from "wxh" - if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) { - return array((int) $matches[1], (int) $matches[2]); - } - } - - if ($sttyString = $this->getSttyColumns()) { - // extract [w, h] from "rows h; columns w;" - if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) { - return array((int) $matches[2], (int) $matches[1]); - } - // extract [w, h] from "; h rows; w columns" - if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) { - return array((int) $matches[2], (int) $matches[1]); - } - } - - return array(null, null); + return $this->terminalDimensionsProvider->getTerminalDimensions(); } /** @@ -721,7 +696,7 @@ public function getTerminalDimensions() */ public function setTerminalDimensions($width, $height) { - $this->terminalDimensions = array($width, $height); + $this->terminalDimensionsProvider->setTerminalDimensions($width, $height); return $this; } @@ -880,54 +855,6 @@ protected function getDefaultHelperSet() )); } - /** - * Runs and parses stty -a if it's available, suppressing any error output. - * - * @return string - */ - private function getSttyColumns() - { - if (!function_exists('proc_open')) { - return; - } - - $descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')); - $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); - if (is_resource($process)) { - $info = stream_get_contents($pipes[1]); - fclose($pipes[1]); - fclose($pipes[2]); - proc_close($process); - - return $info; - } - } - - /** - * Runs and parses mode CON if it's available, suppressing any error output. - * - * @return string x or null if it could not be parsed - */ - private function getConsoleMode() - { - if (!function_exists('proc_open')) { - return; - } - - $descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')); - $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); - if (is_resource($process)) { - $info = stream_get_contents($pipes[1]); - fclose($pipes[1]); - fclose($pipes[2]); - proc_close($process); - - if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) { - return $matches[2].'x'.$matches[1]; - } - } - } - /** * Returns abbreviated suggestions in string format. * diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 4fcf17cf8da64..560d39a254565 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -14,6 +14,7 @@ use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Exception\LogicException; +use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; /** * The ProgressBar provides helpers to display progress output. @@ -50,12 +51,16 @@ class ProgressBar private static $formats; /** - * Constructor. - * - * @param OutputInterface $output An OutputInterface instance - * @param int $max Maximum steps (0 if unknown) + * @var TerminalDimensionsProvider + */ + private $terminalDimensionsProvider; + + /** + * @param OutputInterface $output An OutputInterface instance + * @param int $max Maximum steps (0 if unknown) + * @param TerminalDimensionsProvider $terminalDimensionsProvider */ - public function __construct(OutputInterface $output, $max = 0) + public function __construct(OutputInterface $output, $max = 0, TerminalDimensionsProvider $terminalDimensionsProvider = null) { if ($output instanceof ConsoleOutputInterface) { $output = $output->getErrorOutput(); @@ -63,6 +68,7 @@ public function __construct(OutputInterface $output, $max = 0) $this->output = $output; $this->setMaxSteps($max); + $this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider(); if (!$this->output->isDecorated()) { // disable overwrite when output does not support ANSI codes. @@ -210,7 +216,7 @@ public function getProgressPercent() */ public function setBarWidth($size) { - $this->barWidth = (int) $size; + $this->barWidth = max(1, (int) $size); } /** @@ -405,21 +411,9 @@ public function display() $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat()); } - $this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) { - if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) { - $text = call_user_func($formatter, $this, $this->output); - } elseif (isset($this->messages[$matches[1]])) { - $text = $this->messages[$matches[1]]; - } else { - return $matches[0]; - } - - if (isset($matches[2])) { - $text = sprintf('%'.$matches[2], $text); - } - - return $text; - }, $this->format)); + $line = $this->buildLine(); + $line = $this->adjustLineWidthToTerminalWidth($line); + $this->overwrite($line); } /** @@ -600,4 +594,45 @@ private static function initFormats() 'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%', ); } + + /** + * @return string + */ + private function buildLine() + { + return preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) { + if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) { + $text = call_user_func($formatter, $this, $this->output); + } elseif (isset($this->messages[$matches[1]])) { + $text = $this->messages[$matches[1]]; + } else { + return $matches[0]; + } + + if (isset($matches[2])) { + $text = sprintf('%'.$matches[2], $text); + } + + return $text; + }, $this->format); + } + + /** + * @param string $line + * + * @return bool + */ + private function adjustLineWidthToTerminalWidth($line) + { + $lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line); + $terminalWidth = $this->terminalDimensionsProvider->getTerminalWidth(); + if ($lineLength > $terminalWidth) { + $newBarWidth = $this->barWidth - $lineLength + $terminalWidth; + $this->setBarWidth($newBarWidth); + + return $this->buildLine(); + } + + return $line; + } } diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php new file mode 100644 index 0000000000000..83ce9bdf45dba --- /dev/null +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php @@ -0,0 +1,156 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Terminal; + +class TerminalDimensionsProvider +{ + /** + * @var int[] + */ + private $terminalDimensions = array(); + + /** + * Tries to figure out the terminal dimensions based on the current environment. + * + * @return int[] Array containing width and height + */ + public function getTerminalDimensions() + { + if ($this->terminalDimensions) { + return $this->terminalDimensions; + } + + if ($this->isWindowsEnvironment()) { + // extract [w, H] from "wxh (WxH)" + if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) { + return array((int) $matches[1], (int) $matches[2]); + } + // extract [w, h] from "wxh" + if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) { + return array((int) $matches[1], (int) $matches[2]); + } + } + + if ($sttyString = $this->getSttyColumns()) { + // extract [w, h] from "rows h; columns w;" + if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) { + return array((int) $matches[2], (int) $matches[1]); + } + // extract [w, h] from "; h rows; w columns" + if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) { + return array((int) $matches[2], (int) $matches[1]); + } + } + + return array(null, null); + } + + /** + * Tries to figure out the terminal width in which this application runs. + * + * @return int|null + */ + public function getTerminalWidth() + { + $terminalDimensions = $this->getTerminalDimensions(); + + return $terminalDimensions[0]; + } + + /** + * Tries to figure out the terminal height in which this application runs. + * + * @return int|null + */ + public function getTerminalHeight() + { + $terminalDimensions = $this->getTerminalDimensions(); + + return $terminalDimensions[1]; + } + + /** + * Sets terminal dimensions. + * + * Can be useful to force terminal dimensions for functional tests. + * + * @param int $width + * @param int $height + */ + public function setTerminalDimensions($width, $height) + { + $this->terminalDimensions = array($width, $height); + } + + /** + * Runs and parses mode CON if it's available, suppressing any error output. + * + * @return string x or null if it could not be parsed + */ + private function getConsoleMode() + { + if (!function_exists('proc_open')) { + return; + } + + $descriptorspec = array( + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w'), + ); + $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); + if (is_resource($process)) { + $info = stream_get_contents($pipes[1]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($process); + + if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) { + return $matches[2].'x'.$matches[1]; + } + } + } + + /** + * Runs and parses stty -a if it's available, suppressing any error output. + * + * @return string + */ + private function getSttyColumns() + { + if (!function_exists('proc_open')) { + return; + } + + $descriptorspec = array( + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w'), + ); + + $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); + if (is_resource($process)) { + $info = stream_get_contents($pipes[1]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($process); + + return $info; + } + } + + /** + * @return bool + */ + private function isWindowsEnvironment() + { + return '\\' === DIRECTORY_SEPARATOR; + } +} diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 16a4bba54b43c..5514e44de5488 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -14,6 +14,7 @@ use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Output\StreamOutput; +use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; /** * @group time-sensitive @@ -22,7 +23,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase { public function testMultipleStart() { - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->advance(); $bar->start(); @@ -38,7 +39,7 @@ public function testMultipleStart() public function testAdvance() { - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->advance(); @@ -52,7 +53,7 @@ public function testAdvance() public function testAdvanceWithStep() { - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->advance(5); @@ -66,7 +67,7 @@ public function testAdvanceWithStep() public function testAdvanceMultipleTimes() { - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->advance(3); $bar->advance(2); @@ -82,7 +83,7 @@ public function testAdvanceMultipleTimes() public function testAdvanceOverMax() { - $bar = new ProgressBar($output = $this->getOutputStream(), 10); + $bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar->setProgress(9); $bar->advance(); $bar->advance(); @@ -105,7 +106,7 @@ public function testFormat() ; // max in construct, no format - $bar = new ProgressBar($output = $this->getOutputStream(), 10); + $bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->advance(10); $bar->finish(); @@ -114,7 +115,7 @@ public function testFormat() $this->assertEquals($expected, stream_get_contents($output->getStream())); // max in start, no format - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->start(10); $bar->advance(10); $bar->finish(); @@ -123,7 +124,7 @@ public function testFormat() $this->assertEquals($expected, stream_get_contents($output->getStream())); // max in construct, explicit format before - $bar = new ProgressBar($output = $this->getOutputStream(), 10); + $bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar->setFormat('normal'); $bar->start(); $bar->advance(10); @@ -133,7 +134,7 @@ public function testFormat() $this->assertEquals($expected, stream_get_contents($output->getStream())); // max in start, explicit format before - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->setFormat('normal'); $bar->start(10); $bar->advance(10); @@ -145,7 +146,7 @@ public function testFormat() public function testCustomizations() { - $bar = new ProgressBar($output = $this->getOutputStream(), 10); + $bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar->setBarWidth(10); $bar->setBarCharacter('_'); $bar->setEmptyBarCharacter(' '); @@ -164,7 +165,7 @@ public function testCustomizations() public function testDisplayWithoutStart() { - $bar = new ProgressBar($output = $this->getOutputStream(), 50); + $bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar->display(); rewind($output->getStream()); @@ -176,7 +177,7 @@ public function testDisplayWithoutStart() public function testDisplayWithQuietVerbosity() { - $bar = new ProgressBar($output = $this->getOutputStream(true, StreamOutput::VERBOSITY_QUIET), 50); + $bar = new ProgressBar($output = $this->getOutputStream(true, StreamOutput::VERBOSITY_QUIET), 50, $this->createTerminalDimensionsProvider()); $bar->display(); rewind($output->getStream()); @@ -188,7 +189,7 @@ public function testDisplayWithQuietVerbosity() public function testFinishWithoutStart() { - $bar = new ProgressBar($output = $this->getOutputStream(), 50); + $bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar->finish(); rewind($output->getStream()); @@ -200,7 +201,7 @@ public function testFinishWithoutStart() public function testPercent() { - $bar = new ProgressBar($output = $this->getOutputStream(), 50); + $bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->display(); $bar->advance(); @@ -218,7 +219,7 @@ public function testPercent() public function testOverwriteWithShorterLine() { - $bar = new ProgressBar($output = $this->getOutputStream(), 50); + $bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%'); $bar->start(); $bar->display(); @@ -240,7 +241,7 @@ public function testOverwriteWithShorterLine() public function testStartWithMax() { - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->setFormat('%current%/%max% [%bar%]'); $bar->start(50); $bar->advance(); @@ -255,7 +256,7 @@ public function testStartWithMax() public function testSetCurrentProgress() { - $bar = new ProgressBar($output = $this->getOutputStream(), 50); + $bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->display(); $bar->advance(); @@ -288,7 +289,7 @@ public function testSetCurrentBeforeStarting() */ public function testRegressProgress() { - $bar = new ProgressBar($output = $this->getOutputStream(), 50); + $bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->setProgress(15); $bar->setProgress(10); @@ -309,7 +310,7 @@ public function testRedrawFrequency() public function testMultiByteSupport() { - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->setBarCharacter('■'); $bar->advance(3); @@ -324,7 +325,7 @@ public function testMultiByteSupport() public function testClear() { - $bar = new ProgressBar($output = $this->getOutputStream(), 50); + $bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->setProgress(25); $bar->clear(); @@ -340,7 +341,7 @@ public function testClear() public function testPercentNotHundredBeforeComplete() { - $bar = new ProgressBar($output = $this->getOutputStream(), 200); + $bar = new ProgressBar($output = $this->getOutputStream(), 200, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->display(); $bar->advance(199); @@ -358,7 +359,7 @@ public function testPercentNotHundredBeforeComplete() public function testNonDecoratedOutput() { - $bar = new ProgressBar($output = $this->getOutputStream(false), 200); + $bar = new ProgressBar($output = $this->getOutputStream(false), 200, $this->createTerminalDimensionsProvider()); $bar->start(); for ($i = 0; $i < 200; ++$i) { @@ -386,7 +387,7 @@ public function testNonDecoratedOutput() public function testNonDecoratedOutputWithClear() { - $bar = new ProgressBar($output = $this->getOutputStream(false), 50); + $bar = new ProgressBar($output = $this->getOutputStream(false), 50, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->setProgress(25); $bar->clear(); @@ -404,7 +405,7 @@ public function testNonDecoratedOutputWithClear() public function testNonDecoratedOutputWithoutMax() { - $bar = new ProgressBar($output = $this->getOutputStream(false)); + $bar = new ProgressBar($output = $this->getOutputStream(false), 0, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->advance(); @@ -419,10 +420,10 @@ public function testNonDecoratedOutputWithoutMax() public function testParallelBars() { $output = $this->getOutputStream(); - $bar1 = new ProgressBar($output, 2); - $bar2 = new ProgressBar($output, 3); + $bar1 = new ProgressBar($output, 2, $this->createTerminalDimensionsProvider()); + $bar2 = new ProgressBar($output, 3, $this->createTerminalDimensionsProvider()); $bar2->setProgressCharacter('#'); - $bar3 = new ProgressBar($output); + $bar3 = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider()); $bar1->start(); $output->write("\n"); @@ -479,7 +480,7 @@ public function testWithoutMax() { $output = $this->getOutputStream(); - $bar = new ProgressBar($output); + $bar = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider()); $bar->start(); $bar->advance(); $bar->advance(); @@ -502,7 +503,7 @@ public function testAddingPlaceholderFormatter() ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) { return $bar->getMaxSteps() - $bar->getProgress(); }); - $bar = new ProgressBar($output = $this->getOutputStream(), 3); + $bar = new ProgressBar($output = $this->getOutputStream(), 3, $this->createTerminalDimensionsProvider()); $bar->setFormat(' %remaining_steps% [%bar%]'); $bar->start(); @@ -520,7 +521,7 @@ public function testAddingPlaceholderFormatter() public function testMultilineFormat() { - $bar = new ProgressBar($output = $this->getOutputStream(), 3); + $bar = new ProgressBar($output = $this->getOutputStream(), 3, $this->createTerminalDimensionsProvider()); $bar->setFormat("%bar%\nfoobar"); $bar->start(); @@ -540,7 +541,7 @@ public function testMultilineFormat() public function testAnsiColorsAndEmojis() { - $bar = new ProgressBar($output = $this->getOutputStream(), 15); + $bar = new ProgressBar($output = $this->getOutputStream(), 15, $this->createTerminalDimensionsProvider()); ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) { static $i = 0; $mem = 100000 * $i; @@ -583,7 +584,7 @@ public function testAnsiColorsAndEmojis() public function testSetFormat() { - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->setFormat('normal'); $bar->start(); rewind($output->getStream()); @@ -592,7 +593,7 @@ public function testSetFormat() stream_get_contents($output->getStream()) ); - $bar = new ProgressBar($output = $this->getOutputStream(), 10); + $bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar->setFormat('normal'); $bar->start(); rewind($output->getStream()); @@ -607,7 +608,7 @@ public function testSetFormat() */ public function testFormatsWithoutMax($format) { - $bar = new ProgressBar($output = $this->getOutputStream()); + $bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar->setFormat($format); $bar->start(); @@ -641,4 +642,15 @@ protected function generateOutput($expected) return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expected; } + + /** + * @return TerminalDimensionsProvider + */ + private function createTerminalDimensionsProvider() + { + $terminalDimensionsProvider = new TerminalDimensionsProvider(); + $terminalDimensionsProvider->setTerminalDimensions(800, 5); + + return $terminalDimensionsProvider; + } } diff --git a/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php b/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php new file mode 100644 index 0000000000000..0dbd656bfa2e6 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests\Terminal; + +use PHPUnit_Framework_TestCase; +use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; +use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface; + +class TerminalDimensionsProviderTest extends PHPUnit_Framework_TestCase +{ + /** + * @var TerminalDimensionsProviderInterface + */ + private $terminalDimensionsProvider; + + protected function setUp() + { + $this->terminalDimensionsProvider = new TerminalDimensionsProvider(); + } + + public function testGetTerminalDimensions() + { + $dimensions = $this->terminalDimensionsProvider->getTerminalDimensions(); + $this->assertCount(2, $dimensions); + + $this->terminalDimensionsProvider->setTerminalDimensions(100, 50); + $this->assertSame(array(100, 50), $this->terminalDimensionsProvider->getTerminalDimensions()); + } +}