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

发布于 2024-07-08 19:36:26 字数 233 浏览 6 评论 0原文

我有一个小的 Perl 脚本(在 Windows 上),可以为我检查一些文件,以帮助我完成日常业务。 目前它打印出类似...

0%
25%
50%
75%
Complete

但我记得我过去使用过的脚本并没有逐行打印进度,而是更新了显示屏上的输出,大概是通过移动光标返回并套印那里的内容。

有谁知道需要什么魔法吗? 可移植性对我来说并不重要,脚本是一次性的。

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...

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

万劫不复 2024-07-15 19:36:26

您可以使用 curses 和制作一个漂亮的进度条。

编辑
或者做这样的事情:

print "#####                                 [ 10%]\r";
# Do something
print "##########                            [ 20%]\r";
# Do something else
print "###############                       [ 30%]\r";
# Do some more
# ...
# ...
# ...
print "##################################### [100%]\n";
print "Done.\n";

You could use curses and make a nice progress bar.

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";
只等公子 2024-07-15 19:36:26

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

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

不忘初心 2024-07-15 19:36:26

如果您需要在 Perl 中做某事,很可能有人已经完成并将其上传到 CPAN。 查看一些名称中带有“progress”的模块。

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.

小猫一只 2024-07-15 19:36:26

您可能对智能评论感兴趣。 这可能比编写自己的进度条更容易。

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

梦言归人 2024-07-15 19:36:26

您应该能够打印退格字符“\b”以将光标向后移动,以便您可以覆盖之前打印的内容。

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

铜锣湾横着走 2024-07-15 19:36:26

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

\b

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

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

\b

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文