Skip to content

Commit c0ca8f9

Browse files
committed
[Console] Add Placeholders to ProgressBar for exactly times
1 parent 3147082 commit c0ca8f9

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,38 @@ public static function substr(?string $string, int $from, int $length = null): s
9696
*/
9797
public static function formatTime(int|float $secs)
9898
{
99+
$secs = (int) floor($secs);
100+
101+
if (0 === $secs) {
102+
return '< 1 sec';
103+
}
104+
99105
static $timeFormats = [
100-
[0, '< 1 sec'],
101-
[1, '1 sec'],
102-
[2, 'secs', 1],
103-
[60, '1 min'],
104-
[120, 'mins', 60],
105-
[3600, '1 hr'],
106-
[7200, 'hrs', 3600],
107-
[86400, '1 day'],
108-
[172800, 'days', 86400],
106+
[1, '1 sec', 'secs'],
107+
[60, '1 min', 'mins'],
108+
[3600, '1 hr', 'hrs'],
109+
[86400, '1 day', 'days'],
109110
];
110111

112+
$times = [];
111113
foreach ($timeFormats as $index => $format) {
112-
if ($secs >= $format[0]) {
113-
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
114-
|| $index == \count($timeFormats) - 1
115-
) {
116-
if (2 == \count($format)) {
117-
return $format[1];
118-
}
119-
120-
return floor($secs / $format[2]).' '.$format[1];
121-
}
114+
$seconds = isset($timeFormats[$index + 1]) ? $secs % $timeFormats[$index + 1][0] : $secs;
115+
116+
if (0 === $seconds) {
117+
continue;
122118
}
119+
120+
$unitCount = ($seconds / $format[0]);
121+
$times[] = 1 === $unitCount ? $format[1] : $unitCount.' '.$format[2];
122+
123+
if ($secs === $seconds) {
124+
break;
125+
}
126+
127+
$secs -= $seconds;
123128
}
129+
130+
return implode(', ', array_reverse($times));
124131
}
125132

126133
/**

src/Symfony/Component/Console/Tests/Helper/HelperTest.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,27 @@ public static function formatTimeProvider()
2121
{
2222
return [
2323
[0, '< 1 sec'],
24+
[0.95, '< 1 sec'],
2425
[1, '1 sec'],
2526
[2, '2 secs'],
2627
[59, '59 secs'],
28+
[59.21, '59 secs'],
2729
[60, '1 min'],
28-
[61, '1 min'],
29-
[119, '1 min'],
30+
[61, '1 min, 1 sec'],
31+
[119, '1 min, 59 secs'],
3032
[120, '2 mins'],
31-
[121, '2 mins'],
32-
[3599, '59 mins'],
33+
[121, '2 mins, 1 sec'],
34+
[3599, '59 mins, 59 secs'],
3335
[3600, '1 hr'],
34-
[7199, '1 hr'],
36+
[7199, '1 hr, 59 mins, 59 secs'],
3537
[7200, '2 hrs'],
36-
[7201, '2 hrs'],
37-
[86399, '23 hrs'],
38+
[7201, '2 hrs, 1 sec'],
39+
[86399, '23 hrs, 59 mins, 59 secs'],
3840
[86400, '1 day'],
39-
[86401, '1 day'],
40-
[172799, '1 day'],
41+
[86401, '1 day, 1 sec'],
42+
[172799, '1 day, 23 hrs, 59 mins, 59 secs'],
4143
[172800, '2 days'],
42-
[172801, '2 days'],
44+
[172801, '2 days, 1 sec'],
4345
];
4446
}
4547

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,18 @@ public function testSetFormat()
10051005
);
10061006
}
10071007

1008+
public function testSetFormatWithTimes()
1009+
{
1010+
$bar = new ProgressBar($output = $this->getOutputStream(), 15, 0);
1011+
$bar->setFormat('%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%/%remaining:-6s%');
1012+
$bar->start();
1013+
rewind($output->getStream());
1014+
$this->assertEquals(
1015+
' 0/15 [>---------------------------] 0% < 1 sec/< 1 sec/< 1 sec',
1016+
stream_get_contents($output->getStream())
1017+
);
1018+
}
1019+
10081020
public function testUnicode()
10091021
{
10101022
$bar = new ProgressBar($output = $this->getOutputStream(), 10, 0);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy