Symfony ProgressBar:如何重新创建文章开头显示的栏

时间:2017-03-03 17:20:07

标签: php symfony

在Symfony的documentation about ProgressBar中,有一个显示非常ProgressBar的gif。

在文章本身is stated中:

  

格式可以跨越多行;这在你想要的时候非常有用   在进度条旁显示更多上下文信息(请参阅   本文开头的例子。)

可是:

  1. 如何使格式跨越多行?使用\n似乎无效。
  2. 是否有gif中显示的ProgressBar的工作示例?

1 个答案:

答案 0 :(得分:2)

问题1

只要\n没有转义或使用单引号字符串,

使用\n就是正确的。例如:

/** @var ProgressBar $bar */
$bar->setFormat("Current: %current%\nMax: %max%");

问题2

引用示例的代码是单元测试的一部分。该代码位于870cc23a/Tests/Helper/ProgressBarTest.php#L654

我删除了单元测试代码并创建了一个工作示例(使用symfony/console ^3.3测试):

<强>代码

<?php

use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

require 'vendor/autoload.php';

$bar = new ProgressBar(new ConsoleOutput(), 15);

ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) {
    static $i = 0;
    $mem = 100000 * $i;
    $colors = $i++ ? '41;37' : '44;37';
    return "\033[" . $colors . 'm ' . Helper::formatMemory($mem) . " \033[0m";
});

$bar->setFormat(" \033[44;37m %title:-37s% \033[0m\n %current%/%max% %bar% %percent:3s%%\n   %remaining:-10s% %memory:37s%");
$bar->setBarCharacter($done = "\033[32m●\033[0m");
$bar->setEmptyBarCharacter($empty = "\033[31m●\033[0m");
$bar->setProgressCharacter($progress = "\033[32m➤ \033[0m");
$bar->setMessage('Starting the demo... fingers crossed', 'title');
$bar->start();

for ($i = 0; $i < 15; ++$i) {
    usleep(250000);
    $bar->advance();
}

$bar->finish();

echo "\n";

<强>作曲

{
    "require": {
        "symfony/console": "^3.3"
    }
}