From 4edfabc5208c3938f5102b6b7f7defbd37259009 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 6 Nov 2015 19:48:00 +0100 Subject: [PATCH 01/14] [Console] ProgressBar - adjust to the window width (static) --- .../Component/Console/Helper/ProgressBar.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 4fcf17cf8da64..40f492b6a5a74 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -408,8 +408,15 @@ public function display() $this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) { if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) { $text = call_user_func($formatter, $this, $this->output); + if ($this->adjustBarWidthToWindowWidth($text)) { + $text = call_user_func($formatter, $this, $this->output); + } } elseif (isset($this->messages[$matches[1]])) { $text = $this->messages[$matches[1]]; + if ($this->adjustBarWidthToWindowWidth($text)) { + $text = $this->messages[$matches[1]]; + } + } else { return $matches[0]; } @@ -600,4 +607,22 @@ private static function initFormats() 'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%', ); } + + /** + * @param string $line + * @return bool + */ + private function adjustBarWidthToWindowWidth($line) + { + $lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line); + $windowWidth = exec('tput cols'); + + if ($lineLength > $windowWidth) { + $barWidthDelta = $lineLength - $windowWidth; + $newBarWidth = $this->barWidth - $barWidthDelta - 20; + $this->setBarWidth($newBarWidth); + return true; + } + return false; + } } From 3a55e6b9e0d4cfa8d4fad34d1210078984d9930a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 14 Nov 2015 19:08:36 +0100 Subject: [PATCH 02/14] ProgressBar: use Application method getTerminalDimensions() to get terminals width --- .../Component/Console/Helper/ProgressBar.php | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 40f492b6a5a74..62de4c6ebcc33 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console\Helper; +use Symfony\Component\Console\Application; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Exception\LogicException; @@ -615,8 +616,7 @@ private static function initFormats() private function adjustBarWidthToWindowWidth($line) { $lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line); - $windowWidth = exec('tput cols'); - + $windowWidth = $this->getTerminalWidth(); if ($lineLength > $windowWidth) { $barWidthDelta = $lineLength - $windowWidth; $newBarWidth = $this->barWidth - $barWidthDelta - 20; @@ -625,4 +625,22 @@ private function adjustBarWidthToWindowWidth($line) } return false; } + + /** + * @return int + */ + private function getTerminalWidth() + { + $dimensions = $this->getTerminalDimensions(); + return $dimensions[0]; + } + + /** + * @return int[] + */ + private function getTerminalDimensions() + { + $application = new Application(); + return $application->getTerminalDimensions(); + } } From 77b92da62b5967b66b42f82b4c3c71be08def928 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 14 Nov 2015 21:00:53 +0100 Subject: [PATCH 03/14] TerminalDimensionsProvider added --- .../Terminal/TerminalDimensionsProvider.php | 133 ++++++++++++++++++ .../TerminalDimensionsProviderInterface.php | 36 +++++ .../TerminalDimensionsProviderTest.php | 64 +++++++++ 3 files changed, 233 insertions(+) create mode 100644 src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php create mode 100644 src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php create mode 100644 src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php new file mode 100644 index 0000000000000..9672945cba07f --- /dev/null +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php @@ -0,0 +1,133 @@ + + * + * 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 implements TerminalDimensionsProviderInterface +{ + /** + * @var int[] + */ + private $terminalDimensions; + + /** + * {@inheritdoc} + */ + 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 [(int) $matches[1], (int) $matches[2]]; + } + // extract [w, h] from "wxh" + if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) { + return [(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 [(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 [(int) $matches[2], (int) $matches[1]]; + } + } + + return [null, null]; + } + + /** + * {@inheritdoc} + */ + public function getTerminalWidth() + { + return $this->getTerminalDimensions()[0]; + } + + /** + * {@inheritdoc} + */ + public function getTerminalHeight() + { + return $this->getTerminalDimensions()[1]; + } + + /** + * 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 = [ + 1 => ['pipe', 'w'], + 2 => ['pipe', 'w'] + ]; + $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, ['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 = [ + 1 => ['pipe', 'w'], + 2 => ['pipe', 'w'] + ]; + + $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, ['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/Terminal/TerminalDimensionsProviderInterface.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php new file mode 100644 index 0000000000000..6c2fd687d8b5b --- /dev/null +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Terminal; + +interface TerminalDimensionsProviderInterface +{ + /** + * Tries to figure out the terminal dimensions based on the current environment. + * + * @return int[] Array containing width and height + */ + public function getTerminalDimensions(); + + /** + * Tries to figure out the terminal width in which this application runs. + * + * @return int|null + */ + public function getTerminalWidth(); + + /** + * Tries to figure out the terminal height in which this application runs. + * + * @return int|null + */ + public function getTerminalHeight(); +} 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..6d19b03ef7f67 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php @@ -0,0 +1,64 @@ + + * + * 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->assertInternalType('int', $dimensions[0]); + $this->assertInternalType('int', $dimensions[1]); + + if ($this->isUnixEnvironment()) { + $this->assertSame((int) exec('tput cols'), $dimensions[0]); + } + } + + public function testGetTerminalWidth() + { + $width = $this->terminalDimensionsProvider->getTerminalWidth(); + $this->assertInternalType('int', $width); + if ($this->isUnixEnvironment()) { + $this->assertSame((int) exec('tput cols'), $width); + + } + } + + public function testGetTerminalHeight() + { + $this->assertInternalType('int', $this->terminalDimensionsProvider->getTerminalHeight()); + } + + /** + * @return bool + */ + private function isUnixEnvironment() + { + return '/' === DIRECTORY_SEPARATOR; + } +} From 6a77dbee6476fb28b94f5705d33e46e75c549906 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 14 Nov 2015 22:48:55 +0100 Subject: [PATCH 04/14] Application: make use of TerminalDimensionsProvider --- src/Symfony/Component/Console/Application.php | 104 +++--------------- 1 file changed, 16 insertions(+), 88 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 166b7c458e948..a3e12ef74c5f1 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -35,6 +35,8 @@ 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\Console\Terminal\TerminalDimensionsProviderInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -64,19 +66,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 TerminalDimensionsProviderInterface */ - 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 TerminalDimensionsProviderInterface $terminalDimensionsProvider + */ + public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', TerminalDimensionsProviderInterface $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 +662,7 @@ public function renderException(\Exception $e, OutputInterface $output) */ protected function getTerminalWidth() { - $dimensions = $this->getTerminalDimensions(); - - return $dimensions[0]; + return $this->terminalDimensionsProvider->getTerminalWidth(); } /** @@ -668,9 +672,7 @@ protected function getTerminalWidth() */ protected function getTerminalHeight() { - $dimensions = $this->getTerminalDimensions(); - - return $dimensions[1]; + return $this->terminalDimensionsProvider->getTerminalWidth(); } /** @@ -680,33 +682,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 +697,7 @@ public function getTerminalDimensions() */ public function setTerminalDimensions($width, $height) { - $this->terminalDimensions = array($width, $height); + $this->terminalDimensionsProvider->setTerminalDimensions($width, $height); return $this; } @@ -880,54 +856,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. * From 066f39176e04e51f84f31f2451293c8fad7a5e86 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 14 Nov 2015 22:51:22 +0100 Subject: [PATCH 05/14] ProgressBar: make use of TerminalDimensionsProvider --- .../Component/Console/Helper/ProgressBar.php | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 62de4c6ebcc33..2ce42dbfeb3a7 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -11,10 +11,11 @@ namespace Symfony\Component\Console\Helper; -use Symfony\Component\Console\Application; use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Exception\LogicException; +use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; +use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface; /** * The ProgressBar provides helpers to display progress output. @@ -51,12 +52,16 @@ class ProgressBar private static $formats; /** - * Constructor. - * - * @param OutputInterface $output An OutputInterface instance - * @param int $max Maximum steps (0 if unknown) + * @var TerminalDimensionsProviderInterface + */ + private $terminalDimensionsProvider; + + /** + * @param OutputInterface $output An OutputInterface instance + * @param int $max Maximum steps (0 if unknown) + * @param TerminalDimensionsProviderInterface $terminalDimensionsProvider */ - public function __construct(OutputInterface $output, $max = 0) + public function __construct(OutputInterface $output, $max = 0, TerminalDimensionsProviderInterface $terminalDimensionsProvider = null) { if ($output instanceof ConsoleOutputInterface) { $output = $output->getErrorOutput(); @@ -64,6 +69,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. @@ -616,31 +622,15 @@ private static function initFormats() private function adjustBarWidthToWindowWidth($line) { $lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line); - $windowWidth = $this->getTerminalWidth(); + $windowWidth = $this->terminalDimensionsProvider->getTerminalWidth(); if ($lineLength > $windowWidth) { $barWidthDelta = $lineLength - $windowWidth; $newBarWidth = $this->barWidth - $barWidthDelta - 20; $this->setBarWidth($newBarWidth); + return true; } - return false; - } - - /** - * @return int - */ - private function getTerminalWidth() - { - $dimensions = $this->getTerminalDimensions(); - return $dimensions[0]; - } - /** - * @return int[] - */ - private function getTerminalDimensions() - { - $application = new Application(); - return $application->getTerminalDimensions(); + return false; } } From efb2b7db0245a2eca5baf655b26f6ebae6667831 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 14 Nov 2015 22:54:06 +0100 Subject: [PATCH 06/14] TerminalDimensionsProvider: use old array style, add test helper method --- .../Terminal/TerminalDimensionsProvider.php | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php index 9672945cba07f..dbaaab103a75b 100644 --- a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php @@ -16,7 +16,7 @@ class TerminalDimensionsProvider implements TerminalDimensionsProviderInterface /** * @var int[] */ - private $terminalDimensions; + private $terminalDimensions = array(); /** * {@inheritdoc} @@ -30,26 +30,26 @@ public function getTerminalDimensions() if ($this->isWindowsEnvironment()) { // extract [w, H] from "wxh (WxH)" if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) { - return [(int) $matches[1], (int) $matches[2]]; + return array((int) $matches[1], (int) $matches[2]); } // extract [w, h] from "wxh" if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) { - return [(int) $matches[1], (int) $matches[2]]; + 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 [(int) $matches[2], (int) $matches[1]]; + 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 [(int) $matches[2], (int) $matches[1]]; + return array((int) $matches[2], (int) $matches[1]); } } - return [null, null]; + return array(null, null); } /** @@ -68,6 +68,19 @@ public function getTerminalHeight() return $this->getTerminalDimensions()[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. * @@ -79,11 +92,11 @@ private function getConsoleMode() return; } - $descriptorspec = [ - 1 => ['pipe', 'w'], - 2 => ['pipe', 'w'] - ]; - $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); + $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]); @@ -107,12 +120,12 @@ private function getSttyColumns() return; } - $descriptorspec = [ - 1 => ['pipe', 'w'], - 2 => ['pipe', 'w'] - ]; + $descriptorspec = array( + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w') + ); - $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, ['suppress_errors' => true]); + $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]); From 0dea4e8dba8af645dfbe3d81a957ce14cc4ace22 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 14 Nov 2015 23:14:41 +0100 Subject: [PATCH 07/14] coding standard fixes --- src/Symfony/Component/Console/Application.php | 6 +++--- src/Symfony/Component/Console/Helper/ProgressBar.php | 6 +++--- .../Console/Terminal/TerminalDimensionsProvider.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index a3e12ef74c5f1..c2cf53f0b48ec 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -74,9 +74,9 @@ class Application private $terminalDimensionsProvider; /** - * @param string $name The name of the application - * @param string $version The version of the application - * @param TerminalDimensionsProviderInterface $terminalDimensionsProvider + * @param string $name The name of the application + * @param string $version The version of the application + * @param TerminalDimensionsProviderInterface $terminalDimensionsProvider */ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', TerminalDimensionsProviderInterface $terminalDimensionsProvider = null) { diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 2ce42dbfeb3a7..efced2aa2bf5d 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -57,9 +57,9 @@ class ProgressBar private $terminalDimensionsProvider; /** - * @param OutputInterface $output An OutputInterface instance - * @param int $max Maximum steps (0 if unknown) - * @param TerminalDimensionsProviderInterface $terminalDimensionsProvider + * @param OutputInterface $output An OutputInterface instance + * @param int $max Maximum steps (0 if unknown) + * @param TerminalDimensionsProviderInterface $terminalDimensionsProvider */ public function __construct(OutputInterface $output, $max = 0, TerminalDimensionsProviderInterface $terminalDimensionsProvider = null) { diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php index dbaaab103a75b..48df07a9b72e3 100644 --- a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php @@ -94,7 +94,7 @@ private function getConsoleMode() $descriptorspec = array( 1 => array('pipe', 'w'), - 2 => array('pipe', 'w') + 2 => array('pipe', 'w'), ); $process = proc_open('mode CON', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); if (is_resource($process)) { @@ -122,7 +122,7 @@ private function getSttyColumns() $descriptorspec = array( 1 => array('pipe', 'w'), - 2 => array('pipe', 'w') + 2 => array('pipe', 'w'), ); $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); From 846f1be74976acbea2e96c45ff7cb4969e0335c4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 14 Nov 2015 23:30:23 +0100 Subject: [PATCH 08/14] TerminalDimensionsProvider test fixes --- .../Tests/Terminal/TerminalDimensionsProviderTest.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php b/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php index 6d19b03ef7f67..127f235659048 100644 --- a/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php +++ b/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php @@ -31,8 +31,6 @@ public function testGetTerminalDimensions() { $dimensions = $this->terminalDimensionsProvider->getTerminalDimensions(); $this->assertCount(2, $dimensions); - $this->assertInternalType('int', $dimensions[0]); - $this->assertInternalType('int', $dimensions[1]); if ($this->isUnixEnvironment()) { $this->assertSame((int) exec('tput cols'), $dimensions[0]); @@ -42,18 +40,11 @@ public function testGetTerminalDimensions() public function testGetTerminalWidth() { $width = $this->terminalDimensionsProvider->getTerminalWidth(); - $this->assertInternalType('int', $width); if ($this->isUnixEnvironment()) { $this->assertSame((int) exec('tput cols'), $width); - } } - public function testGetTerminalHeight() - { - $this->assertInternalType('int', $this->terminalDimensionsProvider->getTerminalHeight()); - } - /** * @return bool */ From cc14c5688b7cdc638a85076c3c7e57a1edd72472 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 15 Nov 2015 09:20:27 +0100 Subject: [PATCH 09/14] cs --- src/Symfony/Component/Console/Application.php | 4 +- .../Component/Console/Helper/ProgressBar.php | 5 +- .../Terminal/TerminalDimensionsProvider.php | 260 +++++++++--------- 3 files changed, 135 insertions(+), 134 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index c2cf53f0b48ec..16306623b7eaa 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -74,8 +74,8 @@ class Application private $terminalDimensionsProvider; /** - * @param string $name The name of the application - * @param string $version The version of the application + * @param string $name The name of the application + * @param string $version The version of the application * @param TerminalDimensionsProviderInterface $terminalDimensionsProvider */ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', TerminalDimensionsProviderInterface $terminalDimensionsProvider = null) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index efced2aa2bf5d..8d325a013b879 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -57,8 +57,8 @@ class ProgressBar private $terminalDimensionsProvider; /** - * @param OutputInterface $output An OutputInterface instance - * @param int $max Maximum steps (0 if unknown) + * @param OutputInterface $output An OutputInterface instance + * @param int $max Maximum steps (0 if unknown) * @param TerminalDimensionsProviderInterface $terminalDimensionsProvider */ public function __construct(OutputInterface $output, $max = 0, TerminalDimensionsProviderInterface $terminalDimensionsProvider = null) @@ -617,6 +617,7 @@ private static function initFormats() /** * @param string $line + * * @return bool */ private function adjustBarWidthToWindowWidth($line) diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php index 48df07a9b72e3..4c803432ef229 100644 --- a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php @@ -13,134 +13,134 @@ class TerminalDimensionsProvider implements TerminalDimensionsProviderInterface { - /** - * @var int[] - */ - private $terminalDimensions = array(); - - /** - * {@inheritdoc} - */ - 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); - } - - /** - * {@inheritdoc} - */ - public function getTerminalWidth() - { - return $this->getTerminalDimensions()[0]; - } - - /** - * {@inheritdoc} - */ - public function getTerminalHeight() - { - return $this->getTerminalDimensions()[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; - } + /** + * @var int[] + */ + private $terminalDimensions = array(); + + /** + * {@inheritdoc} + */ + 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); + } + + /** + * {@inheritdoc} + */ + public function getTerminalWidth() + { + return $this->getTerminalDimensions()[0]; + } + + /** + * {@inheritdoc} + */ + public function getTerminalHeight() + { + return $this->getTerminalDimensions()[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; + } } From ac3ad469afbd73cac77fdf8aeb43a61be06b8d48 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 15 Nov 2015 09:56:40 +0100 Subject: [PATCH 10/14] ProgressBar: make sure width is positive number --- src/Symfony/Component/Console/Helper/ProgressBar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 8d325a013b879..a37598eae532e 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -217,7 +217,7 @@ public function getProgressPercent() */ public function setBarWidth($size) { - $this->barWidth = (int) $size; + $this->barWidth = max(1, (int) $size); } /** From c89d24fd858020bab85d3b0209e3fdf3e790e111 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 15 Nov 2015 17:14:43 +0100 Subject: [PATCH 11/14] ProgressBar: decouple building line to standalone method, better adjust method naming --- .../Component/Console/Helper/ProgressBar.php | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index a37598eae532e..3c549099d7563 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -412,28 +412,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); - if ($this->adjustBarWidthToWindowWidth($text)) { - $text = call_user_func($formatter, $this, $this->output); - } - } elseif (isset($this->messages[$matches[1]])) { - $text = $this->messages[$matches[1]]; - if ($this->adjustBarWidthToWindowWidth($text)) { - $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); } /** @@ -615,23 +596,44 @@ private static function initFormats() ); } + /** + * @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 adjustBarWidthToWindowWidth($line) + private function adjustLineWidthToTerminalWidth($line) { $lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line); - $windowWidth = $this->terminalDimensionsProvider->getTerminalWidth(); - if ($lineLength > $windowWidth) { - $barWidthDelta = $lineLength - $windowWidth; - $newBarWidth = $this->barWidth - $barWidthDelta - 20; + $terminalWidth = $this->terminalDimensionsProvider->getTerminalWidth(); + if ($lineLength > $terminalWidth) { + $newBarWidth = $this->barWidth - $lineLength + $terminalWidth; $this->setBarWidth($newBarWidth); - return true; + return $this->buildLine(); } - return false; + return $line; } } From cf0e411bc9a5b5d12bd2a7ec42e9f9afd0d3da25 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 15 Nov 2015 19:43:53 +0100 Subject: [PATCH 12/14] ProgressBar: fix tests --- .../Terminal/TerminalDimensionsProvider.php | 7 +- .../TerminalDimensionsProviderInterface.php | 10 +++ .../Console/Tests/Helper/ProgressBarTest.php | 81 +++++++++++-------- .../TerminalDimensionsProviderTest.php | 21 +---- 4 files changed, 60 insertions(+), 59 deletions(-) diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php index 4c803432ef229..5508ef2c32bec 100644 --- a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php @@ -69,12 +69,7 @@ public function getTerminalHeight() } /** - * Sets terminal dimensions. - * - * Can be useful to force terminal dimensions for functional tests. - * - * @param int $width - * @param int $height + * {@inheritdoc} */ public function setTerminalDimensions($width, $height) { diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php index 6c2fd687d8b5b..6a5bf096daeb8 100644 --- a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php @@ -33,4 +33,14 @@ public function getTerminalWidth(); * @return int|null */ public function getTerminalHeight(); + + /** + * Sets terminal dimensions. + * + * Can be useful to force terminal dimensions for functional tests. + * + * @param int $width + * @param int $height + */ + public function setTerminalDimensions($width, $height); } diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 16a4bba54b43c..3faa886d20488 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -14,6 +14,8 @@ use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Output\StreamOutput; +use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; +use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface; /** * @group time-sensitive @@ -22,7 +24,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 +40,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 +54,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 +68,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 +84,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 +107,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 +116,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 +125,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 +135,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 +147,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 +166,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 +178,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 +190,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 +202,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 +220,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 +242,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 +257,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 +290,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 +311,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 +326,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 +342,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 +360,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 +388,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 +406,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 +421,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 +481,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 +504,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 +522,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 +542,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 +585,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 +594,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 +609,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 +643,15 @@ protected function generateOutput($expected) return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expected; } + + /** + * @return TerminalDimensionsProviderInterface + */ + 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 index 127f235659048..0dbd656bfa2e6 100644 --- a/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php +++ b/src/Symfony/Component/Console/Tests/Terminal/TerminalDimensionsProviderTest.php @@ -32,24 +32,7 @@ public function testGetTerminalDimensions() $dimensions = $this->terminalDimensionsProvider->getTerminalDimensions(); $this->assertCount(2, $dimensions); - if ($this->isUnixEnvironment()) { - $this->assertSame((int) exec('tput cols'), $dimensions[0]); - } - } - - public function testGetTerminalWidth() - { - $width = $this->terminalDimensionsProvider->getTerminalWidth(); - if ($this->isUnixEnvironment()) { - $this->assertSame((int) exec('tput cols'), $width); - } - } - - /** - * @return bool - */ - private function isUnixEnvironment() - { - return '/' === DIRECTORY_SEPARATOR; + $this->terminalDimensionsProvider->setTerminalDimensions(100, 50); + $this->assertSame(array(100, 50), $this->terminalDimensionsProvider->getTerminalDimensions()); } } From eb8a58d43e3a4963085f84abe723597315476983 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 5 Mar 2016 14:53:37 +0000 Subject: [PATCH 13/14] [Console] drop TerminalDimensionsProviderInterface --- src/Symfony/Component/Console/Application.php | 11 ++--- .../Component/Console/Helper/ProgressBar.php | 11 ++--- .../Terminal/TerminalDimensionsProvider.php | 21 +++++++-- .../TerminalDimensionsProviderInterface.php | 46 ------------------- .../Console/Tests/Helper/ProgressBarTest.php | 3 +- 5 files changed, 27 insertions(+), 65 deletions(-) delete mode 100644 src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 16306623b7eaa..ceae5408e8697 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -36,7 +36,6 @@ use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; -use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -69,16 +68,16 @@ class Application private $defaultCommand; /** - * @var TerminalDimensionsProviderInterface + * @var TerminalDimensionsProvider */ private $terminalDimensionsProvider; /** - * @param string $name The name of the application - * @param string $version The version of the application - * @param TerminalDimensionsProviderInterface $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', TerminalDimensionsProviderInterface $terminalDimensionsProvider = null) + public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', TerminalDimensionsProvider $terminalDimensionsProvider = null) { $this->name = $name; $this->version = $version; diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 3c549099d7563..560d39a254565 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -15,7 +15,6 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; -use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface; /** * The ProgressBar provides helpers to display progress output. @@ -52,16 +51,16 @@ class ProgressBar private static $formats; /** - * @var TerminalDimensionsProviderInterface + * @var TerminalDimensionsProvider */ private $terminalDimensionsProvider; /** - * @param OutputInterface $output An OutputInterface instance - * @param int $max Maximum steps (0 if unknown) - * @param TerminalDimensionsProviderInterface $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, TerminalDimensionsProviderInterface $terminalDimensionsProvider = null) + public function __construct(OutputInterface $output, $max = 0, TerminalDimensionsProvider $terminalDimensionsProvider = null) { if ($output instanceof ConsoleOutputInterface) { $output = $output->getErrorOutput(); diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php index 5508ef2c32bec..06d478a2aed0a 100644 --- a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Console\Terminal; -class TerminalDimensionsProvider implements TerminalDimensionsProviderInterface +class TerminalDimensionsProvider { /** * @var int[] @@ -19,7 +19,9 @@ class TerminalDimensionsProvider implements TerminalDimensionsProviderInterface private $terminalDimensions = array(); /** - * {@inheritdoc} + * Tries to figure out the terminal dimensions based on the current environment. + * + * @return int[] Array containing width and height */ public function getTerminalDimensions() { @@ -53,7 +55,9 @@ public function getTerminalDimensions() } /** - * {@inheritdoc} + * Tries to figure out the terminal width in which this application runs. + * + * @return int|null */ public function getTerminalWidth() { @@ -61,7 +65,9 @@ public function getTerminalWidth() } /** - * {@inheritdoc} + * Tries to figure out the terminal height in which this application runs. + * + * @return int|null */ public function getTerminalHeight() { @@ -69,7 +75,12 @@ public function getTerminalHeight() } /** - * {@inheritdoc} + * Sets terminal dimensions. + * + * Can be useful to force terminal dimensions for functional tests. + * + * @param int $width + * @param int $height */ public function setTerminalDimensions($width, $height) { diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php deleted file mode 100644 index 6a5bf096daeb8..0000000000000 --- a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProviderInterface.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Console\Terminal; - -interface TerminalDimensionsProviderInterface -{ - /** - * Tries to figure out the terminal dimensions based on the current environment. - * - * @return int[] Array containing width and height - */ - public function getTerminalDimensions(); - - /** - * Tries to figure out the terminal width in which this application runs. - * - * @return int|null - */ - public function getTerminalWidth(); - - /** - * Tries to figure out the terminal height in which this application runs. - * - * @return int|null - */ - public function getTerminalHeight(); - - /** - * Sets terminal dimensions. - * - * Can be useful to force terminal dimensions for functional tests. - * - * @param int $width - * @param int $height - */ - public function setTerminalDimensions($width, $height); -} diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 3faa886d20488..5514e44de5488 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -15,7 +15,6 @@ use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; -use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface; /** * @group time-sensitive @@ -645,7 +644,7 @@ protected function generateOutput($expected) } /** - * @return TerminalDimensionsProviderInterface + * @return TerminalDimensionsProvider */ private function createTerminalDimensionsProvider() { From 885e4eacad7ee2d43842470492587fa8b9bb065f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 5 Mar 2016 15:01:42 +0000 Subject: [PATCH 14/14] [Console] TerminalDimensionsProvider - make PHP 5.3 compatible --- .../Console/Terminal/TerminalDimensionsProvider.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php index 06d478a2aed0a..83ce9bdf45dba 100644 --- a/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php +++ b/src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php @@ -61,7 +61,9 @@ public function getTerminalDimensions() */ public function getTerminalWidth() { - return $this->getTerminalDimensions()[0]; + $terminalDimensions = $this->getTerminalDimensions(); + + return $terminalDimensions[0]; } /** @@ -71,7 +73,9 @@ public function getTerminalWidth() */ public function getTerminalHeight() { - return $this->getTerminalDimensions()[1]; + $terminalDimensions = $this->getTerminalDimensions(); + + return $terminalDimensions[1]; } /**