2017-03-03 28 views
1

在Symfony的documentation about ProgressBar中有一個gif,顯示了一個漂亮的ProgressBarSymfony ProgressBar:如何重新創建文章開頭顯示的欄

在文章本身is stated

甲格式可以跨越多於一個的線;當您希望 在進度欄旁邊顯示更多上下文信息時(參見本文開頭的示例,請參閱 ))時,這非常有用。

但是:

  1. 我怎樣才能使多行的格式跨度?使用\n似乎無法正常工作。
  2. gif中顯示的ProgressBar是否有工作示例?

回答

1

問題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" 
    } 
} 
+0

謝謝!我沒有測試代碼,但我會繼續信任! :) – Aerendir