Skip to content

Commit

Permalink
[Console] Add Placeholders to ProgressBar for exactly times
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeckers committed Jun 5, 2023
1 parent 9909410 commit 82a1c62
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,42 @@ public static function formatTime(int|float $secs)
}
}

public static function formatTimeExactly(int|float $secs): string
{
$secs = (int) floor($secs);

if (0 === $secs) {
return '< 1 sec';
}

static $timeFormats = [
[1, '1 sec', 'secs'],
[60, '1 min', 'mins'],
[3600, '1 hr', 'hrs'],
[86400, '1 day', 'days'],
];

$times = [];
foreach ($timeFormats as $index => $format) {
$seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs;

if (0 === $seconds) {
continue;
}

$unitCount = ($seconds / $format[0]);
$times[] = 1 === $unitCount ? $format[1] : $unitCount.' '.$format[2];

if ($secs === $seconds) {
break;
}

$secs -= $seconds;
}

return implode(', ', array_reverse($times));
}

/**
* @return string
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,20 +535,35 @@ private static function initPlaceholderFormatters(): array
return $display;
},
'elapsed' => fn (self $bar) => Helper::formatTime(time() - $bar->getStartTime()),
'elapsed_time_exactly' => fn (self $bar) => Helper::formatTimeExactly(time() - $bar->getStartTime()),
'remaining' => function (self $bar) {
if (!$bar->getMaxSteps()) {
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
}

return Helper::formatTime($bar->getRemaining());
},
'remaining_time_exactly' => function (self $bar) {
if (!$bar->getMaxSteps()) {
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
}

return Helper::formatTimeExactly($bar->getRemaining());
},
'estimated' => function (self $bar) {
if (!$bar->getMaxSteps()) {
throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
}

return Helper::formatTime($bar->getEstimated());
},
'estimated_time_exactly' => function (self $bar) {
if (!$bar->getMaxSteps()) {
throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
}

return Helper::formatTimeExactly($bar->getEstimated());
},
'memory' => fn (self $bar) => Helper::formatMemory(memory_get_usage(true)),
'current' => fn (self $bar) => str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', \STR_PAD_LEFT),
'max' => fn (self $bar) => $bar->getMaxSteps(),
Expand Down
39 changes: 39 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ public static function formatTimeProvider()
];
}

public static function formatTimeExactlyProvider()
{
return [
[0, '< 1 sec'],
[0.95, '< 1 sec'],
[1, '1 sec'],
[2, '2 secs'],
[59, '59 secs'],
[59.21, '59 secs'],
[60, '1 min'],
[61, '1 min, 1 sec'],
[119, '1 min, 59 secs'],
[120, '2 mins'],
[121, '2 mins, 1 sec'],
[3599, '59 mins, 59 secs'],
[3600, '1 hr'],
[7199, '1 hr, 59 mins, 59 secs'],
[7200, '2 hrs'],
[7201, '2 hrs, 1 sec'],
[86399, '23 hrs, 59 mins, 59 secs'],
[86400, '1 day'],
[86401, '1 day, 1 sec'],
[172799, '1 day, 23 hrs, 59 mins, 59 secs'],
[172800, '2 days'],
[172801, '2 days, 1 sec'],
];
}

public static function decoratedTextProvider()
{
return [
Expand All @@ -64,6 +92,17 @@ public function testFormatTime($secs, $expectedFormat)
$this->assertEquals($expectedFormat, Helper::formatTime($secs));
}

/**
* @dataProvider formatTimeExactlyProvider
*
* @param int $secs
* @param string $expectedFormat
*/
public function testFormatTimeExactly($secs, $expectedFormat)
{
$this->assertEquals($expectedFormat, Helper::formatTimeExactly($secs));
}

/**
* @dataProvider decoratedTextProvider
*/
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,27 @@ public function testSetFormat()
);
}

public function testSetFormatWithTimes()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 15, 0);
$bar->setFormat('%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%/%remaining:-6s%');
$bar->start();
rewind($output->getStream());
$this->assertEquals(
' 0/15 [>---------------------------] 0% < 1 sec/< 1 sec/< 1 sec',
stream_get_contents($output->getStream())
);

$bar = new ProgressBar($output = $this->getOutputStream(), 15, 0);
$bar->setFormat('%current%/%max% [%bar%] %percent:3s%% %elapsed_time_exactly%/%estimated_time_exactly%/%remaining_time_exactly%');
$bar->start();
rewind($output->getStream());
$this->assertEquals(
' 0/15 [>---------------------------] 0% < 1 sec/< 1 sec/< 1 sec',
stream_get_contents($output->getStream())
);
}

public function testUnicode()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 10, 0);
Expand Down

0 comments on commit 82a1c62

Please sign in to comment.