Skip to content

Commit 091a779

Browse files
committed
[Console] made formats even more flexible
1 parent 660697a commit 091a779

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

Helper/ProgressBar.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public function __construct(OutputInterface $output, $max = 0)
6767
if (!self::$formats) {
6868
self::$formats = self::initFormats();
6969
}
70+
71+
$this->setFormat($this->determineBestFormat());
7072
}
7173

7274
/**
@@ -274,7 +276,15 @@ public function getProgressCharacter()
274276
*/
275277
public function setFormat($format)
276278
{
277-
$this->format = isset(self::$formats[$format]) ? self::$formats[$format] : $format;
279+
// try to use the _nomax variant if available
280+
if (!$this->max && isset(self::$formats[$format.'_nomax'])) {
281+
$this->format = self::$formats[$format.'_nomax'];
282+
} elseif (isset(self::$formats[$format])) {
283+
$this->format = self::$formats[$format];
284+
} else {
285+
$this->format = $format;
286+
}
287+
278288
$this->formatLineCount = substr_count($this->format, "\n");
279289
}
280290

@@ -299,10 +309,6 @@ public function start()
299309
$this->lastMessagesLength = 0;
300310
$this->barCharOriginal = '';
301311

302-
if (null === $this->format) {
303-
$this->setFormat($this->determineBestFormat());
304-
}
305-
306312
if (!$this->max) {
307313
$this->barCharOriginal = $this->barChar;
308314
$this->barChar = $this->emptyBarChar;
@@ -457,12 +463,13 @@ private function overwrite($message)
457463
private function determineBestFormat()
458464
{
459465
switch ($this->output->getVerbosity()) {
460-
case OutputInterface::VERBOSITY_QUIET:
461-
return $this->max > 0 ? 'quiet' : 'quiet_nomax';
466+
// OutputInterface::VERBOSITY_QUIET: display is disabled anyway
462467
case OutputInterface::VERBOSITY_VERBOSE:
468+
return $this->max > 0 ? 'verbose' : 'verbose_nomax';
463469
case OutputInterface::VERBOSITY_VERY_VERBOSE:
470+
return $this->max > 0 ? 'very_verbose' : 'very_verbose_nomax';
464471
case OutputInterface::VERBOSITY_DEBUG:
465-
return $this->max > 0 ? 'verbose' : 'verbose_nomax';
472+
return $this->max > 0 ? 'debug' : 'debug_nomax';
466473
default:
467474
return $this->max > 0 ? 'normal' : 'normal_nomax';
468475
}
@@ -528,12 +535,17 @@ private static function initPlaceholderFormatters()
528535
private static function initFormats()
529536
{
530537
return array(
531-
'quiet' => ' %percent%%',
532-
'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
533-
'verbose' => ' %current%/%max% [%bar%] %percent:3s%% Elapsed: %elapsed:6s%',
534-
'quiet_nomax' => ' %current%',
535-
'normal_nomax' => ' %current% [%bar%]',
536-
'verbose_nomax' => ' %current% [%bar%] Elapsed: %elapsed:6s%',
538+
'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
539+
'normal_nomax' => ' %current% [%bar%]',
540+
541+
'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
542+
'verbose_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%',
543+
544+
'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
545+
'very_verbose_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%',
546+
547+
'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
548+
'debug_nomax' => ' %current% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
537549
);
538550
}
539551
}

Tests/Helper/ProgressBarTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ public function testAnsiColorsAndEmojis()
363363
$bar->finish();
364364

365365
rewind($output->getStream());
366-
367366
$this->assertEquals(
368367
$this->generateOutput(
369368
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
@@ -384,6 +383,27 @@ public function testAnsiColorsAndEmojis()
384383
);
385384
}
386385

386+
public function testSetFormat()
387+
{
388+
$bar = new ProgressBar($output = $this->getOutputStream());
389+
$bar->setFormat('normal');
390+
$bar->start();
391+
rewind($output->getStream());
392+
$this->assertEquals(
393+
$this->generateOutput(' 0 [>---------------------------]'),
394+
stream_get_contents($output->getStream())
395+
);
396+
397+
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
398+
$bar->setFormat('normal');
399+
$bar->start();
400+
rewind($output->getStream());
401+
$this->assertEquals(
402+
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
403+
stream_get_contents($output->getStream())
404+
);
405+
}
406+
387407
protected function getOutputStream($decorated = true)
388408
{
389409
return new StreamOutput(fopen('php://memory', 'r+', false), StreamOutput::VERBOSITY_NORMAL, $decorated);

0 commit comments

Comments
 (0)