8000 [Console] A better progress bar by fabpot · Pull Request #10356 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] A better progress bar #10356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 3, 2014
Prev Previous commit
Next Next commit
[Console] fixed PHP comptability
  • Loading branch information
fabpot committed Mar 1, 2014
commit 38f7a6f81795f2ef5d40501366aa8a559acc2c0c
52 changes: 38 additions & 14 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class ProgressBar
private $formatLineCount;
private $messages;

static private $formatters;
static private $formats;
private static $formatters;
private static $formats;

/**
* Constructor.
Expand Down Expand Up @@ -86,6 +86,18 @@ public static function setPlaceholderFormatterDefinition($name, $callable)
self::$formatters[$name] = $callable;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method initPlaceholderFormatters should be called here if the class has not been instantiated yet.

Use case:

ProgressBar::setPlaceholderFormatter('%cpu%', function (ProgressBar $bar) {});
$bar = new ProgressBar($output, 100);

}

/**
* Gets the placeholder formatter for a given name.
*
* @param string $name The placeholder name (including the delimiter char like %)
*
* @return callable|null A PHP callable
*/
public static function getPlaceholderFormatterDefinition($name)
{
return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoudln't this method initialize the formatters if not done yet ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixed now

}

/**
* Sets a format for a given name.
*
Expand All @@ -103,6 +115,18 @@ public static function setFormatDefinition($name, $format)
self::$formats[$name] = $format;
}

/**
* Gets the format for a given name.
*
* @param string $name The format name
*
* @return string|null A format string
*/
public static function getFormatDefinition($name)
{
return isset(self::$formats[$name]) ? self::$formats[$name] : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this initialize the formats if not done yet ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixed now

}

public function setMessage($message, $name = 'message')
{
$this->messages[$name] = $message;
Expand All @@ -116,7 +140,7 @@ public function getMessage($name = 'message')
/**
* Gets the progress bar start time.
*
* @return int The progress bar start time
* @return integer The progress bar start time
*/
public function getStartTime()
{
Expand All @@ -126,7 +150,7 @@ public function getStartTime()
/**
* Gets the progress bar maximal steps.
*
* @return int The progress bar max steps
* @return integer The progress bar max steps
*/
public function getMaxSteps()
{
Expand All @@ -136,7 +160,7 @@ public function getMaxSteps()
/**
* Gets the progress bar step.
*
* @return int The progress bar step
* @return integer The progress bar step
*/
public function getStep()
{
Expand All @@ -146,7 +170,7 @@ public function getStep()
/**
* Gets the progress bar step width.
*
* @return int The progress bar step width
* @return integer The progress bar step width
*/
public function getStepWidth()
{
Expand All @@ -156,7 +180,7 @@ public function getStepWidth()
/**
* Gets the current progress bar percent.
*
* @return int The current progress bar percent
* @return integer The current progress bar percent
*/
public function getProgressPercent()
{
Expand All @@ -166,7 +190,7 @@ public function getProgressPercent()
/**
* Sets the progress bar width.
*
* @param int $size The progress bar size
* @param integer $size The progress bar size
*/
public function setBarWidth($size)
{
Expand All @@ -176,7 +200,7 @@ public function setBarWidth($size)
/**
* Gets the progress bar width.
*
* @return int The progress bar size
* @return integer The progress bar size
*/
public function getBarWidth()
{
Expand Down Expand Up @@ -257,7 +281,7 @@ public function setFormat($format)
/**
* Sets the redraw frequency.
*
* @param int $freq The frequency in steps
* @param integer $freq The frequency in steps
*/
public function setRedrawFrequency($freq)
{
Expand Down Expand Up @@ -365,8 +389,8 @@ public function display()

$self = $this;
$this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self) {
if (isset(self::$formatters[$matches[1]])) {
$text = call_user_func(self::$formatters[$matches[1]], $self);
if ($formatter = $self::getPlaceholderFormatterDefinition($matches[1])) {
$text = call_user_func($formatter, $self);
} elseif (isset($this->messages[$matches[1]])) {
$text = $this->messages[$matches[1]];
} else {
Expand Down Expand Up @@ -433,7 +457,7 @@ private function determineBestFormat()
}
}

static private function initPlaceholderFormatters()
private static function initPlaceholderFormatters()
{
return array(
'bar' => function (ProgressBar $bar) {
Expand Down Expand Up @@ -490,7 +514,7 @@ static private function initPlaceholderFormatters()
);
}

static private function initFormats()
private static function initFormats()
{
return array(
'quiet' => ' %percent%%',
Expand Down
0