[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\Event\ConsoleTerminateEvent;
|
||||||
use Symfony\Component\Console\Exception\CommandNotFoundException;
|
use Symfony\Component\Console\Exception\CommandNotFoundException;
|
||||||
use Symfony\Component\Console\Exception\LogicException;
|
use Symfony\Component\Console\Exception\LogicException;
|
||||||
|
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -65,20 +66,24 @@ class Application
|
|||||||
private $definition;
|
private $definition;
|
||||||
private $helperSet;
|
private $helperSet;
|
||||||
private $dispatcher;
|
private $dispatcher;
|
||||||
private $terminalDimensions;
|
|
||||||
private $defaultCommand;
|
private $defaultCommand;
|
||||||
private $singleCommand;
|
private $singleCommand;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* @var TerminalDimensionsProvider
|
||||||
*
|
*/
|
||||||
|
private $terminalDimensionsProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
* @param string $name The name of the application
|
* @param string $name The name of the application
|
||||||
* @param string $version The version 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->name = $name;
|
||||||
$this->version = $version;
|
$this->version = $version;
|
||||||
|
$this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider();
|
||||||
$this->defaultCommand = 'list';
|
$this->defaultCommand = 'list';
|
||||||
$this->helperSet = $this->getDefaultHelperSet();
|
$this->helperSet = $this->getDefaultHelperSet();
|
||||||
$this->definition = $this->getDefaultInputDefinition();
|
$this->definition = $this->getDefaultInputDefinition();
|
||||||
@ -692,9 +697,7 @@ class Application
|
|||||||
*/
|
*/
|
||||||
protected function getTerminalWidth()
|
protected function getTerminalWidth()
|
||||||
{
|
{
|
||||||
$dimensions = $this->getTerminalDimensions();
|
return $this->terminalDimensionsProvider->getTerminalWidth();
|
||||||
|
|
||||||
return $dimensions[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -704,9 +707,7 @@ class Application
|
|||||||
*/
|
*/
|
||||||
protected function getTerminalHeight()
|
protected function getTerminalHeight()
|
||||||
{
|
{
|
||||||
$dimensions = $this->getTerminalDimensions();
|
return $this->terminalDimensionsProvider->getTerminalWidth();
|
||||||
|
|
||||||
return $dimensions[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -716,33 +717,7 @@ class Application
|
|||||||
*/
|
*/
|
||||||
public function getTerminalDimensions()
|
public function getTerminalDimensions()
|
||||||
{
|
{
|
||||||
if ($this->terminalDimensions) {
|
return $this->terminalDimensionsProvider->getTerminalDimensions();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -757,7 +732,7 @@ class Application
|
|||||||
*/
|
*/
|
||||||
public function setTerminalDimensions($width, $height)
|
public function setTerminalDimensions($width, $height)
|
||||||
{
|
{
|
||||||
$this->terminalDimensions = array($width, $height);
|
$this->terminalDimensionsProvider->setTerminalDimensions($width, $height);
|
||||||
|
|
||||||
return $this;
|
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.
|
* 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\ConsoleOutputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Exception\LogicException;
|
use Symfony\Component\Console\Exception\LogicException;
|
||||||
|
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ProgressBar provides helpers to display progress output.
|
* The ProgressBar provides helpers to display progress output.
|
||||||
@ -49,12 +50,16 @@ class ProgressBar
|
|||||||
private static $formats;
|
private static $formats;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* @var TerminalDimensionsProvider
|
||||||
*
|
*/
|
||||||
|
private $terminalDimensionsProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
* @param OutputInterface $output An OutputInterface instance
|
* @param OutputInterface $output An OutputInterface instance
|
||||||
* @param int $max Maximum steps (0 if unknown)
|
* @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) {
|
if ($output instanceof ConsoleOutputInterface) {
|
||||||
$output = $output->getErrorOutput();
|
$output = $output->getErrorOutput();
|
||||||
@ -62,6 +67,7 @@ class ProgressBar
|
|||||||
|
|
||||||
$this->output = $output;
|
$this->output = $output;
|
||||||
$this->setMaxSteps($max);
|
$this->setMaxSteps($max);
|
||||||
|
$this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider();
|
||||||
|
|
||||||
if (!$this->output->isDecorated()) {
|
if (!$this->output->isDecorated()) {
|
||||||
// disable overwrite when output does not support ANSI codes.
|
// disable overwrite when output does not support ANSI codes.
|
||||||
@ -217,7 +223,7 @@ class ProgressBar
|
|||||||
*/
|
*/
|
||||||
public function setBarWidth($size)
|
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->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) {
|
$line = $this->buildLine();
|
||||||
if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
|
$line = $this->adjustLineWidthToTerminalWidth($line);
|
||||||
$text = call_user_func($formatter, $this, $this->output);
|
$this->overwrite($line);
|
||||||
} 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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -592,4 +586,45 @@ class ProgressBar
|
|||||||
'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
|
'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\ProgressBar;
|
||||||
use Symfony\Component\Console\Helper\Helper;
|
use Symfony\Component\Console\Helper\Helper;
|
||||||
use Symfony\Component\Console\Output\StreamOutput;
|
use Symfony\Component\Console\Output\StreamOutput;
|
||||||
|
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group time-sensitive
|
* @group time-sensitive
|
||||||
@ -22,7 +23,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
public function testMultipleStart()
|
public function testMultipleStart()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
$bar->start();
|
$bar->start();
|
||||||
@ -38,7 +39,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testAdvance()
|
public function testAdvance()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
|
|
||||||
@ -52,7 +53,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testAdvanceWithStep()
|
public function testAdvanceWithStep()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->advance(5);
|
$bar->advance(5);
|
||||||
|
|
||||||
@ -66,7 +67,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testAdvanceMultipleTimes()
|
public function testAdvanceMultipleTimes()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->advance(3);
|
$bar->advance(3);
|
||||||
$bar->advance(2);
|
$bar->advance(2);
|
||||||
@ -82,7 +83,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testAdvanceOverMax()
|
public function testAdvanceOverMax()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider());
|
||||||
$bar->setProgress(9);
|
$bar->setProgress(9);
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
@ -105,7 +106,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
;
|
;
|
||||||
|
|
||||||
// max in construct, no format
|
// max in construct, no format
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->advance(10);
|
$bar->advance(10);
|
||||||
$bar->finish();
|
$bar->finish();
|
||||||
@ -114,7 +115,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expected, stream_get_contents($output->getStream()));
|
$this->assertEquals($expected, stream_get_contents($output->getStream()));
|
||||||
|
|
||||||
// max in start, no format
|
// max in start, no format
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start(10);
|
$bar->start(10);
|
||||||
$bar->advance(10);
|
$bar->advance(10);
|
||||||
$bar->finish();
|
$bar->finish();
|
||||||
@ -123,7 +124,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expected, stream_get_contents($output->getStream()));
|
$this->assertEquals($expected, stream_get_contents($output->getStream()));
|
||||||
|
|
||||||
// max in construct, explicit format before
|
// 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->setFormat('normal');
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->advance(10);
|
$bar->advance(10);
|
||||||
@ -133,7 +134,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expected, stream_get_contents($output->getStream()));
|
$this->assertEquals($expected, stream_get_contents($output->getStream()));
|
||||||
|
|
||||||
// max in start, explicit format before
|
// 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->setFormat('normal');
|
||||||
$bar->start(10);
|
$bar->start(10);
|
||||||
$bar->advance(10);
|
$bar->advance(10);
|
||||||
@ -145,7 +146,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testCustomizations()
|
public function testCustomizations()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider());
|
||||||
$bar->setBarWidth(10);
|
$bar->setBarWidth(10);
|
||||||
$bar->setBarCharacter('_');
|
$bar->setBarCharacter('_');
|
||||||
$bar->setEmptyBarCharacter(' ');
|
$bar->setEmptyBarCharacter(' ');
|
||||||
@ -164,7 +165,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testDisplayWithoutStart()
|
public function testDisplayWithoutStart()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
|
||||||
$bar->display();
|
$bar->display();
|
||||||
|
|
||||||
rewind($output->getStream());
|
rewind($output->getStream());
|
||||||
@ -176,7 +177,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testDisplayWithQuietVerbosity()
|
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();
|
$bar->display();
|
||||||
|
|
||||||
rewind($output->getStream());
|
rewind($output->getStream());
|
||||||
@ -188,7 +189,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testFinishWithoutStart()
|
public function testFinishWithoutStart()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
|
||||||
$bar->finish();
|
$bar->finish();
|
||||||
|
|
||||||
rewind($output->getStream());
|
rewind($output->getStream());
|
||||||
@ -200,7 +201,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testPercent()
|
public function testPercent()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->display();
|
$bar->display();
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
@ -218,7 +219,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testOverwriteWithShorterLine()
|
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->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->display();
|
$bar->display();
|
||||||
@ -240,7 +241,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testStartWithMax()
|
public function testStartWithMax()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->setFormat('%current%/%max% [%bar%]');
|
$bar->setFormat('%current%/%max% [%bar%]');
|
||||||
$bar->start(50);
|
$bar->start(50);
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
@ -255,7 +256,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testSetCurrentProgress()
|
public function testSetCurrentProgress()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->display();
|
$bar->display();
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
@ -288,7 +289,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testRegressProgress()
|
public function testRegressProgress()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->setProgress(15);
|
$bar->setProgress(15);
|
||||||
$bar->setProgress(10);
|
$bar->setProgress(10);
|
||||||
@ -329,7 +330,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testMultiByteSupport()
|
public function testMultiByteSupport()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->setBarCharacter('■');
|
$bar->setBarCharacter('■');
|
||||||
$bar->advance(3);
|
$bar->advance(3);
|
||||||
@ -344,7 +345,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testClear()
|
public function testClear()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->setProgress(25);
|
$bar->setProgress(25);
|
||||||
$bar->clear();
|
$bar->clear();
|
||||||
@ -360,7 +361,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testPercentNotHundredBeforeComplete()
|
public function testPercentNotHundredBeforeComplete()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 200);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 200, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->display();
|
$bar->display();
|
||||||
$bar->advance(199);
|
$bar->advance(199);
|
||||||
@ -378,7 +379,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testNonDecoratedOutput()
|
public function testNonDecoratedOutput()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(false), 200);
|
$bar = new ProgressBar($output = $this->getOutputStream(false), 200, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
|
|
||||||
for ($i = 0; $i < 200; ++$i) {
|
for ($i = 0; $i < 200; ++$i) {
|
||||||
@ -406,7 +407,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testNonDecoratedOutputWithClear()
|
public function testNonDecoratedOutputWithClear()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(false), 50);
|
$bar = new ProgressBar($output = $this->getOutputStream(false), 50, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->setProgress(25);
|
$bar->setProgress(25);
|
||||||
$bar->clear();
|
$bar->clear();
|
||||||
@ -424,7 +425,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testNonDecoratedOutputWithoutMax()
|
public function testNonDecoratedOutputWithoutMax()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(false));
|
$bar = new ProgressBar($output = $this->getOutputStream(false), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
|
|
||||||
@ -439,10 +440,10 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testParallelBars()
|
public function testParallelBars()
|
||||||
{
|
{
|
||||||
$output = $this->getOutputStream();
|
$output = $this->getOutputStream();
|
||||||
$bar1 = new ProgressBar($output, 2);
|
$bar1 = new ProgressBar($output, 2, $this->createTerminalDimensionsProvider());
|
||||||
$bar2 = new ProgressBar($output, 3);
|
$bar2 = new ProgressBar($output, 3, $this->createTerminalDimensionsProvider());
|
||||||
$bar2->setProgressCharacter('#');
|
$bar2->setProgressCharacter('#');
|
||||||
$bar3 = new ProgressBar($output);
|
$bar3 = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider());
|
||||||
|
|
||||||
$bar1->start();
|
$bar1->start();
|
||||||
$output->write("\n");
|
$output->write("\n");
|
||||||
@ -499,7 +500,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$output = $this->getOutputStream();
|
$output = $this->getOutputStream();
|
||||||
|
|
||||||
$bar = new ProgressBar($output);
|
$bar = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->start();
|
$bar->start();
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
@ -522,7 +523,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
|
ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
|
||||||
return $bar->getMaxSteps() - $bar->getProgress();
|
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->setFormat(' %remaining_steps% [%bar%]');
|
||||||
|
|
||||||
$bar->start();
|
$bar->start();
|
||||||
@ -540,7 +541,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testMultilineFormat()
|
public function testMultilineFormat()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream(), 3);
|
$bar = new ProgressBar($output = $this->getOutputStream(), 3, $this->createTerminalDimensionsProvider());
|
||||||
$bar->setFormat("%bar%\nfoobar");
|
$bar->setFormat("%bar%\nfoobar");
|
||||||
|
|
||||||
$bar->start();
|
$bar->start();
|
||||||
@ -560,7 +561,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testAnsiColorsAndEmojis()
|
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) {
|
ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) {
|
||||||
static $i = 0;
|
static $i = 0;
|
||||||
$mem = 100000 * $i;
|
$mem = 100000 * $i;
|
||||||
@ -603,7 +604,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testSetFormat()
|
public function testSetFormat()
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->setFormat('normal');
|
$bar->setFormat('normal');
|
||||||
$bar->start();
|
$bar->start();
|
||||||
rewind($output->getStream());
|
rewind($output->getStream());
|
||||||
@ -612,7 +613,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
stream_get_contents($output->getStream())
|
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->setFormat('normal');
|
||||||
$bar->start();
|
$bar->start();
|
||||||
rewind($output->getStream());
|
rewind($output->getStream());
|
||||||
@ -627,7 +628,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testFormatsWithoutMax($format)
|
public function testFormatsWithoutMax($format)
|
||||||
{
|
{
|
||||||
$bar = new ProgressBar($output = $this->getOutputStream());
|
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
|
||||||
$bar->setFormat($format);
|
$bar->setFormat($format);
|
||||||
$bar->start();
|
$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 "\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