如何更新Perl命令行应用程序上的进度显示?

时间:2021-11-28 12:15:11

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.

谁知道需要什么魔法?便携性对我来说并不重要,脚本非常易于使用。

6 个解决方案

#1


6  

In addition to the other answers, \r will go back to the beginning of the current line

除了其他答案,\ r \ n将返回当前行的开头

#2


8  

You could use curses and 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";

#3


6  

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。查看一些名称中包含“进度”的模块。

#4


6  

You might be interested in Smart Comments. This would be probably easier than coding Your own progress bars.

您可能对Smart Comments感兴趣。这可能比编码您自己的进度条更容易。

#5


2  

You should be able to print a backspace character '\b' to move the cursor back so you can overwrite what you printed previously.

您应该能够打印退格符'\ b'以将光标向后移动,这样您就可以覆盖之前打印的内容。

#6


0  

I don't know if this works in Perl, but in C/C++ you can use

我不知道这是否适用于Perl,但在C / C ++中你可以使用

\b
for a backspace. Using several of those, you can move the cursor to overwrite old values.

#1


6  

In addition to the other answers, \r will go back to the beginning of the current line

除了其他答案,\ r \ n将返回当前行的开头

#2


8  

You could use curses and 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";

#3


6  

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。查看一些名称中包含“进度”的模块。

#4


6  

You might be interested in Smart Comments. This would be probably easier than coding Your own progress bars.

您可能对Smart Comments感兴趣。这可能比编码您自己的进度条更容易。

#5


2  

You should be able to print a backspace character '\b' to move the cursor back so you can overwrite what you printed previously.

您应该能够打印退格符'\ b'以将光标向后移动,这样您就可以覆盖之前打印的内容。

#6


0  

I don't know if this works in Perl, but in C/C++ you can use

我不知道这是否适用于Perl,但在C / C ++中你可以使用

\b
for a backspace. Using several of those, you can move the cursor to overwrite old values.