Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit033ec1b

Browse files
feature#35611 [Console] Moved estimated & remaining calculation logic to separate get method (peterjaap)
This PR was squashed before being merged into the 5.1-dev branch.Discussion----------[Console] Moved estimated & remaining calculation logic to separate get method| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets | -| License | MITThis way, we can use `getEstimated()`and `getRemaining()` to get easy access to the estimated / remaining number of seconds to be used in our placeholder definition set with `setPlaceholderFormatterDefinition` without having to redefine the calculation ourself.Example before;```phpProgressBar::setPlaceholderFormatterDefinition( 'eta', function (ProgressBar $progressBar) { $estimated = round((time() - $progressBar->getStartTime()) / ($progressBar->getProgress() ?: 0.1) * $progressBar->getMaxSteps()); return date('H:i:s', strtotime('+' . $estimated . ' seconds')); });```Example after;```phpProgressBar::setPlaceholderFormatterDefinition( 'eta', function (ProgressBar $progressBar) { return date('H:i:s', strtotime('+' . $this->getEstimated() . ' seconds')); });```Commits-------19958fb [Console] Moved estimated & remaining calculation logic to separate get method
2 parents7e4abf5 +19958fb commit033ec1b

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

‎src/Symfony/Component/Console/Helper/ProgressBar.php‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,29 @@ public function getProgressPercent(): float
191191
return$this->percent;
192192
}
193193

194-
publicfunctiongetBarOffset():int
194+
publicfunctiongetBarOffset():float
195195
{
196196
returnfloor($this->max ?$this->percent *$this->barWidth : (null ===$this->redrawFreq ?min(5,$this->barWidth /15) *$this->writeCount :$this->step) %$this->barWidth);
197197
}
198198

199+
publicfunctiongetEstimated():float
200+
{
201+
if (!$this->step) {
202+
return0;
203+
}
204+
205+
returnround((time() -$this->startTime) /$this->step *$this->max);
206+
}
207+
208+
publicfunctiongetRemaining():float
209+
{
210+
if (!$this->step) {
211+
return0;
212+
}
213+
214+
returnround((time() -$this->startTime) /$this->step * ($this->max -$this->step));
215+
}
216+
199217
publicfunctionsetBarWidth(int$size)
200218
{
201219
$this->barWidth =max(1,$size);
@@ -500,26 +518,14 @@ private static function initPlaceholderFormatters(): array
500518
thrownewLogicException('Unable to display the remaining time if the maximum number of steps is not set.');
501519
}
502520

503-
if (!$bar->getProgress()) {
504-
$remaining =0;
505-
}else {
506-
$remaining =round((time() -$bar->getStartTime()) /$bar->getProgress() * ($bar->getMaxSteps() -$bar->getProgress()));
507-
}
508-
509-
return Helper::formatTime($remaining);
521+
return Helper::formatTime($bar->getRemaining());
510522
},
511523
'estimated' =>function (self$bar) {
512524
if (!$bar->getMaxSteps()) {
513525
thrownewLogicException('Unable to display the estimated time if the maximum number of steps is not set.');
514526
}
515527

516-
if (!$bar->getProgress()) {
517-
$estimated =0;
518-
}else {
519-
$estimated =round((time() -$bar->getStartTime()) /$bar->getProgress() *$bar->getMaxSteps());
520-
}
521-
522-
return Helper::formatTime($estimated);
528+
return Helper::formatTime($bar->getEstimated());
523529
},
524530
'memory' =>function (self$bar) {
525531
return Helper::formatMemory(memory_get_usage(true));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp