windows 如何更新 Perl 命令行应用程序上的进度显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/277384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I update a progress display on a Perl command-line application?
提问by izb
I have a little Perl script (On Windows) that checks some files for me as an aid to my day-to-day business. At the moment it prints out something like...
我有一个小的 Perl 脚本(在 Windows 上),它为我检查一些文件以帮助我的日常业务。目前它打印出类似......
0%
25%
50%
75%
Complete
But I can remember scripts I've used in the past that didn't print progress on a line-by-line basis, but which updated the output on the display, presumably by moving the cursor back and over-printing what was there.
但我记得我过去使用的脚本没有逐行打印进度,而是更新了显示器上的输出,大概是通过将光标移回并叠印那里的内容。
Anyone know what magic is required? Portability isn't important to me, the script is quite disposable.
有人知道需要什么魔法吗?便携性对我来说并不重要,脚本是一次性的。
回答by daniels
You could use cursesand make a nice progress bar.
您可以使用curses并制作一个不错的进度条。
EDIT: Or do something like this:
编辑:或者做这样的事情:
print "##### [ 10%]\r";
# Do something
print "########## [ 20%]\r";
# Do something else
print "############### [ 30%]\r";
# Do some more
# ...
# ...
# ...
print "##################################### [100%]\n";
print "Done.\n";
回答by brian d foy
If you ever need to do something in Perl, it's very likely that someone has done it and uploaded it to CPAN. Look at some of the modules with "progress" in their name.
如果您需要在 Perl 中做某事,很可能有人已经完成并将其上传到 CPAN。查看一些名称中带有“progress”的模块。
回答by zoul
You might be interested in Smart Comments. This would be probably easier than coding Your own progress bars.
您可能对智能评论感兴趣。这可能比编写您自己的进度条更容易。
回答by 1800 INFORMATION
In addition to the other answers, \r will go back to the beginning of the current line
除了其他答案,\r 将回到当前行的开头
回答by Jarod Elliott
You should be able to print a backspace character '\b' to move the cursor back so you can overwrite what you printed previously.
您应该能够打印一个退格字符 '\b' 来将光标移回,这样您就可以覆盖之前打印的内容。
回答by schnaader
I don't know if this works in Perl, but in C/C++ you can use
我不知道这是否适用于 Perl,但在 C/C++ 中你可以使用
\b退格。使用其中的几个,您可以移动光标以覆盖旧值。