-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
4e76aa3
2a78a09
1aa7b8c
7a30e50
a9d47eb
244d3b8
38f7a6f
8c0022b
0d1a58c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,8 +44,8 @@ class ProgressBar | |
private $formatLineCount; | ||
private $messages; | ||
|
||
static private $formatters; | ||
static private $formats; | ||
private static $formatters; | ||
private static $formats; | ||
|
||
/** | ||
* Constructor. | ||
|
@@ -86,6 +86,18 @@ public static function setPlaceholderFormatterDefinition($name, $callable) | |
self::$formatters[$name] = $callable; | ||
} | ||
|
||
/** | ||
* 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shoudln't this method initialize the formatters if not done yet ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, fixed now |
||
} | ||
|
||
/** | ||
* Sets a format for a given name. | ||
* | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this initialize the formats if not done yet ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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() | ||
{ | ||
|
@@ -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() | ||
{ | ||
|
@@ -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() | ||
{ | ||
|
@@ -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() | ||
{ | ||
|
@@ -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() | ||
{ | ||
|
@@ -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) | ||
{ | ||
|
@@ -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() | ||
{ | ||
|
@@ -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) | ||
{ | ||
|
@@ -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 { | ||
|
@@ -433,7 +457,7 @@ private function determineBestFormat() | |
} | ||
} | ||
|
||
static private function initPlaceholderFormatters() | ||
private static function initPlaceholderFormatters() | ||
{ | ||
return array( | ||
'bar' => function (ProgressBar $bar) { | ||
|
@@ -490,7 +514,7 @@ static private function initPlaceholderFormatters() | |
); | ||
} | ||
|
||
static private function initFormats() | ||
private static function initFormats() | ||
{ | ||
return array( | ||
'quiet' => ' %percent%%', | ||
|
There was a problem hiding this comment.
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: