更新命令行输出,即进度 [英] Update Command-line Output, i.e. for Progress

查看:548
本文介绍了更新命令行输出,即进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在命令行上的简单PHP脚本中显示进度表。而不是看到

 进度:0%
进度:1%
等...

我只想更改数字,替换以前的数字, code>解决delta:100%(8522/8522),完成。。



在搜索时,我发现同一问题回答在Perl ,这是完美的,但我不能在PHP中找到它。是否可以?



更新:如果有人对C ++版本感兴趣,请在这里

解决方案

这可以使用 ANSI Escape Sequences -

在PHP中,您将使用当该页面上显示 ESC 时,\033





在你的情况下,你可以使用这样的:

  echoProgress: //最后填充5个字符
for($ i = 0; $ i <= 100; $ i ++){
echo\033 [5D; //向后移动5个字符
echo str_pad($ i,3,'',STR_PAD_LEFT)。 %; //输出总是5个字符长
sleep(1); //等一会儿,所以我们看到动画
}



我简化了一点,确保我总是有5个额外的字符,并总是显示相同数量的数据,总是向后移动相同数量的字符...



但是,当然,你应该能够做更复杂的,如果需要; - )



还有许多其他有趣的转义序列:例如,可以提高你的输出相当多; - )


I'd like to be able to show a progress meter in a simple PHP script on the command line. Instead of seeing

Progress: 0%
Progress: 1%
etc...

I'd like just the number to change, and replace the previous number, much like git clone does for example Resolving deltas: 100% (8522/8522), done..

While searching for this I found the same question answered in Perl, which is perfect, but I couldn't find it in PHP. Is it possible? If not, I'll resort to C.

Thanks

Update: If anyone's interested in the C++ version, it's here.

解决方案

This can be done using ANSI Escape Sequences -- see here for a list.

In PHP, you'll use "\033" when it's indicated ESC on that page.


In your case, you could use something like this :

echo "Progress :      ";  // 5 characters of padding at the end
for ($i=0 ; $i<=100 ; $i++) {
    echo "\033[5D";      // Move 5 characters backward
    echo str_pad($i, 3, ' ', STR_PAD_LEFT) . " %";    // Output is always 5 characters long
    sleep(1);           // wait for a while, so we see the animation
}


I simplified a bit, making sure I always have 5 extra characters, and always displaying the same amount of data, to always move backwards by the same number of chars...

But, of course, you should be able to do much more complicated, if needed ;-)

And there are many other interesting escape sequences : colors, for instance, can enhance your output quite a bit ;-)

这篇关于更新命令行输出,即进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆