【问题标题】:ProgressBar output appears to pad console outputProgressBar 输出显示为填充控制台输出
【发布时间】:2015-01-27 11:29:56
【问题描述】:

我正在尝试将 Symfony 的控制台输出组件包含到现有的非 Symfony 应用程序中,总体而言进展顺利。

我似乎仍然无法理解的一件事是,如果我使用 ProgressBar 类,它似乎会按照进度条的宽度填充我发送的所有输出。

这是代码的粗略概述(删除了各种不必要的杂物):

public function run() {
    $this->logger = new \Symfony\Component\Console\Output\StreamOutput(fopen('php://stdout', 'w'));
    $this->logger->setVerbosity(\Symfony\Component\Console\Output\OutputInterface::VERBOSITY_DEBUG);
    $this->progress = new \Symfony\Component\Console\Helper\ProgressBar($this->logger);

    $this->progress->start();
    $this->doStepOne();
    $this->progress->finish();
}

public function doStepOne() {
    $i = 1;
    while($i < 10000000000) {
        $this->info(sprintf('Sweet, done a thing on page #%d', $i));
    }
}

public function info($message) {
    $this->log(sprintf("<info>%s</info>", $message));
}

public function log($message) {
    // This follows advice from http://symfony.com/doc/current/components/console/helpers/progressbar.html indicating that if you output content, you should ->clear() first and ->display() after.
    // If I don't do this, then the progress bar is output on every line, immediately followed by $message
    $this->progress->clear();
    $this->logger->writeln($message);
    $this->progress->advance();
    $this->progress->display();
}

所以这一切看起来都很简单。同样,我没有在 Symfony 控制台输出中使用它,所以也许这就是原因,但这看起来很奇怪,所以我想我会看看是否有其他人遇到过它。我用我的 Google-fu 找不到任何东西,但也许它很弱?

示例(忽略 ^C 字符,即 Ctrl-C 终止进程): 此外,为了消除任何混淆 - 第一个可见行有我添加的 no 缩进,第二行有 一个空格字符 我已经添加的缩进插入自己,抱歉有任何混淆。

【问题讨论】:

    标签: symfony symfony-console


    【解决方案1】:

    我也观察到这一点,使用 Symfony 2.7,使用 ConsoleOutput 流。我“修复”它的方式是手动将 ANSI delete-to-start 序列插入到输出中。修改您的代码,即:

    if ($this->logger->isDecorated()) {
        $this->logger->write("\x0D");
    }
    

    不漂亮。

    从 2.8 开始,这种行为仍然存在。我不确定这是否是错误。不过好像是这样的。

    【讨论】:

    • 感谢@bishop,这对我有用,但似乎有点错误,或者无论如何都没有得到很好的解释,因为它似乎很常见。将您的回复标记为答案。
    猜你喜欢
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 2015-05-26
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2011-05-21
    相关资源
    最近更新 更多