[Console] ProgressBar - adjust to the window width (static)
This commit is contained in:
parent
7ccfdb4016
commit
b030c24263
@ -36,6 +36,7 @@ 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\TerminalDimensionsProvider;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
@ -65,20 +66,24 @@ class Application
|
||||
private $definition;
|
||||
private $helperSet;
|
||||
private $dispatcher;
|
||||
private $terminalDimensions;
|
||||
private $defaultCommand;
|
||||
private $singleCommand;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @var TerminalDimensionsProvider
|
||||
*/
|
||||
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')
|
||||
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();
|
||||
@ -692,9 +697,7 @@ class Application
|
||||
*/
|
||||
protected function getTerminalWidth()
|
||||
{
|
||||
$dimensions = $this->getTerminalDimensions();
|
||||
|
||||
return $dimensions[0];
|
||||
return $this->terminalDimensionsProvider->getTerminalWidth();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -704,9 +707,7 @@ class Application
|
||||
*/
|
||||
protected function getTerminalHeight()
|
||||
{
|
||||
$dimensions = $this->getTerminalDimensions();
|
||||
|
||||
return $dimensions[1];
|
||||
return $this->terminalDimensionsProvider->getTerminalWidth();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -716,33 +717,7 @@ class Application
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -757,7 +732,7 @@ class Application
|
||||
*/
|
||||
public function setTerminalDimensions($width, $height)
|
||||
{
|
||||
$this->terminalDimensions = array($width, $height);
|
||||
$this->terminalDimensionsProvider->setTerminalDimensions($width, $height);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -927,54 +902,6 @@ class Application
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 <width>x<height> 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.
|
||||
*
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Component\Console\Helper;
|
||||
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.
|
||||
@ -49,12 +50,16 @@ class ProgressBar
|
||||
private static $formats;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @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();
|
||||
@ -62,6 +67,7 @@ class ProgressBar
|
||||
|
||||
$this->output = $output;
|
||||
$this->setMaxSteps($max);
|
||||
$this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider();
|
||||
|
||||
if (!$this->output->isDecorated()) {
|
||||
// disable overwrite when output does not support ANSI codes.
|
||||
@ -217,7 +223,7 @@ class ProgressBar
|
||||
*/
|
||||
public function setBarWidth($size)
|
||||
{
|
||||
$this->barWidth = (int) $size;
|
||||
$this->barWidth = max(1, (int) $size);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -412,21 +418,9 @@ class ProgressBar
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -592,4 +586,45 @@ class ProgressBar
|
||||
'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;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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()
|
||||
{
|
||||
return $this->getTerminalDimensions()[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to figure out the terminal height in which this application runs.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
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 <width>x<height> 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;
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ 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\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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
;
|
||||
|
||||
// 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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
$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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
$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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
$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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
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);
|
||||
@ -329,7 +330,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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);
|
||||
@ -344,7 +345,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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();
|
||||
@ -360,7 +361,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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);
|
||||
@ -378,7 +379,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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) {
|
||||
@ -406,7 +407,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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();
|
||||
@ -424,7 +425,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testNonDecoratedOutputWithoutMax()
|
||||
{
|
||||
$bar = new ProgressBar($output = $this->getOutputStream(false));
|
||||
$bar = new ProgressBar($output = $this->getOutputStream(false), 0, $this->createTerminalDimensionsProvider());
|
||||
$bar->start();
|
||||
$bar->advance();
|
||||
|
||||
@ -439,10 +440,10 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
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");
|
||||
@ -499,7 +500,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$output = $this->getOutputStream();
|
||||
|
||||
$bar = new ProgressBar($output);
|
||||
$bar = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider());
|
||||
$bar->start();
|
||||
$bar->advance();
|
||||
$bar->advance();
|
||||
@ -522,7 +523,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
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();
|
||||
@ -540,7 +541,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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();
|
||||
@ -560,7 +561,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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;
|
||||
@ -603,7 +604,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
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());
|
||||
@ -612,7 +613,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
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());
|
||||
@ -627,7 +628,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testFormatsWithoutMax($format)
|
||||
{
|
||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
||||
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||
$bar->setFormat($format);
|
||||
$bar->start();
|
||||
|
||||
@ -661,4 +662,15 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TerminalDimensionsProvider
|
||||
*/
|
||||
private function createTerminalDimensionsProvider()
|
||||
{
|
||||
$terminalDimensionsProvider = new TerminalDimensionsProvider();
|
||||
$terminalDimensionsProvider->setTerminalDimensions(800, 5);
|
||||
|
||||
return $terminalDimensionsProvider;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user