public function testAnsiColorsAndEmojis() { $bar = new ProgressBar($output = $this->getOutputStream(), 15); ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) { static $i = 0; $mem = 100000 * $i; $colors = $i++ ? '41;37' : '44;37'; return "[" . $colors . "m " . Helper::formatMemory($mem) . " [0m"; }); $bar->setFormat(" [44;37m %title:-37s% [0m\n %current%/%max% %bar% %percent:3s%%\n 🏁 %remaining:-10s% %memory:37s%"); $bar->setBarCharacter($done = "[32m●[0m"); $bar->setEmptyBarCharacter($empty = "[31m●[0m"); $bar->setProgressCharacter($progress = "[32m➤ [0m"); $bar->setMessage('Starting the demo... fingers crossed', 'title'); $bar->start(); $bar->setMessage('Looks good to me...', 'title'); $bar->advance(4); $bar->setMessage('Thanks, bye', 'title'); $bar->finish(); rewind($output->getStream()); $this->assertEquals($this->generateOutput(" [44;37m Starting the demo... fingers crossed [0m\n" . " 0/15 " . $progress . str_repeat($empty, 26) . " 0%\n" . " 🏁 1 sec [44;37m 0 B [0m") . $this->generateOutput(" [44;37m Looks good to me... [0m\n" . " 4/15 " . str_repeat($done, 7) . $progress . str_repeat($empty, 19) . " 26%\n" . " 🏁 1 sec [41;37m 97 KiB [0m") . $this->generateOutput(" [44;37m Thanks, bye [0m\n" . " 15/15 " . str_repeat($done, 28) . " 100%\n" . " 🏁 1 sec [41;37m 195 KiB [0m"), stream_get_contents($output->getStream())); }
private static function initPlaceholderFormatters() { return array('bar' => function (ProgressBar $bar, OutputInterface $output) { $completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getProgress() % $bar->getBarWidth()); $display = str_repeat($bar->getBarCharacter(), $completeBars); if ($completeBars < $bar->getBarWidth()) { $emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter()); $display .= $bar->getProgressCharacter() . str_repeat($bar->getEmptyBarCharacter(), $emptyBars); } return $display; }, 'elapsed' => function (ProgressBar $bar) { return Helper::formatTime(time() - $bar->getStartTime()); }, 'remaining' => function (ProgressBar $bar) { if (!$bar->getMaxSteps()) { throw new \LogicException('Unable to display the remaining time if the maximum number of steps is not set.'); } if (!$bar->getProgress()) { $remaining = 0; } else { $remaining = round((time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress())); } return Helper::formatTime($remaining); }, 'estimated' => function (ProgressBar $bar) { if (!$bar->getMaxSteps()) { throw new \LogicException('Unable to display the estimated time if the maximum number of steps is not set.'); } if (!$bar->getProgress()) { $estimated = 0; } else { $estimated = round((time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps()); } return Helper::formatTime($estimated); }, 'memory' => function (ProgressBar $bar) { return Helper::formatMemory(memory_get_usage(true)); }, 'current' => function (ProgressBar $bar) { return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', STR_PAD_LEFT); }, 'max' => function (ProgressBar $bar) { return $bar->getMaxSteps(); }, 'percent' => function (ProgressBar $bar) { return floor($bar->getProgressPercent() * 100); }); }
/** * Gets cell width. * * @param array $row * @param int $column * * @return int */ private function getCellWidth(array $row, $column) { return isset($row[$column]) ? Helper::strlenWithoutDecoration($this->output->getFormatter(), $row[$column]) : 0; }