简体   繁体   English

在终端更新多行 perl output

[英]Updating multiple lines of perl output on terminal

I am trying to update the progress of my Perl script on the terminal.我正在尝试在终端上更新我的 Perl 脚本的进度。 The output looks something like this output 看起来像这样

 Progress: ||||||||| [46%]

The progress keeps on getting updated until it reaches 100%.进度不断更新,直到达到 100%。 This is being done by printing "\r" after updating the progress.这是通过在更新进度后打印“\r”来完成的。 I wish to update multiple lines at the same time, how can it be done?我想同时更新多行,怎么做? The expectation is something like this期望是这样的

 Progress: ||||||||| [46%] Run-time: 100sec

After some progress(and or time) I wish to update it like this经过一些进展(和或时间)后,我希望像这样更新它

 Progress: |||||||||| [50%] Run-time: 150sec

I tried printing "\r" two times to go to the previous line.我尝试将“\r”两次打印到 go 到上一行。 But it didn't work.但它没有用。

I found similar questions ( here and here ), but they were answered for Python using modules.我发现了类似的问题( 此处此处),但使用模块为 Python 回答了这些问题。 Mine is a Perl script, and I am not preferring to use external modules.我的是 Perl 脚本,我不喜欢使用外部模块。

Term::ANSIScreen provides terminal control using ANSI escape sequences: Term::ANSIScreen使用 ANSI 转义序列提供终端控制:

use Term::ANSIScreen qw!savepos loadpos!;

print savepos();
for my $i (1..10) {
    print '|' x $i, "\n";
    print "Step: $i\n";
    sleep 1;
    print loadpos();
}

or或者

use Term::ANSIScreen qw!up!;

for my $i (1..10) {
    print '|' x $i, "\n";
    print "Step: $i\n";
    sleep 1;
    print up(2);
}

These constants can be used instead of the module:这些常量可以用来代替模块:

my $savepos = "\e[s";
my $loadpos = "\e[u";
my $up2 = "\e[2A";

ANSI escape codes ANSI 转义码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM