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 Oct 9, 2023
1 parent 3147082 commit c0ca8f9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
45 changes: 26 additions & 19 deletions src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,38 @@ public static function substr(?string $string, int $from, int $length = null): s
*/
public static function formatTime(int|float $secs)
{
$secs = (int) floor($secs);

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

static $timeFormats = [
[0, '< 1 sec'],
[1, '1 sec'],
[2, 'secs', 1],
[60, '1 min'],
[120, 'mins', 60],
[3600, '1 hr'],
[7200, 'hrs', 3600],
[86400, '1 day'],
[172800, 'days', 86400],
[1, '1 sec', 'secs'],
[60, '1 min', 'mins'],
[3600, '1 hr', 'hrs'],
[86400, '1 day', 'days'],
];

$times = [];
foreach ($timeFormats as $index => $format) {
if ($secs >= $format[0]) {
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
|| $index == \count($timeFormats) - 1
) {
if (2 == \count($format)) {
return $format[1];
}

return floor($secs / $format[2]).' '.$format[1];
}
$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));
}

/**
Expand Down
22 changes: 12 additions & 10 deletions src/Symfony/Component/Console/Tests/Helper/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,27 @@ public static function formatTimeProvider()
{
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'],
[119, '1 min'],
[61, '1 min, 1 sec'],
[119, '1 min, 59 secs'],
[120, '2 mins'],
[121, '2 mins'],
[3599, '59 mins'],
[121, '2 mins, 1 sec'],
[3599, '59 mins, 59 secs'],
[3600, '1 hr'],
[7199, '1 hr'],
[7199, '1 hr, 59 mins, 59 secs'],
[7200, '2 hrs'],
[7201, '2 hrs'],
[86399, '23 hrs'],
[7201, '2 hrs, 1 sec'],
[86399, '23 hrs, 59 mins, 59 secs'],
[86400, '1 day'],
[86401, '1 day'],
[172799, '1 day'],
[86401, '1 day, 1 sec'],
[172799, '1 day, 23 hrs, 59 mins, 59 secs'],
[172800, '2 days'],
[172801, '2 days'],
[172801, '2 days, 1 sec'],
];
}

Expand Down
12 changes: 12 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,18 @@ 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())
);
}

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

0 comments on commit c0ca8f9

Please sign in to comment.