Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Console output 2

Status
Not open for further replies.

Segfault7375

Programmer
Jul 13, 2001
23
US
Is there a way to update the text on a given line in the console in Perl? Basically so that I could have a percentage complete indicator, but the text staying on one line. If you have ever seen fsck run on Linux as it runs and the percentage complete is counted, that is exactly what I am looking for.

Thanks!
Segfault
 
This is brutally low tech..... but...... I sometimes just print dots for a progress indicator for a long process. You must first set $| = 1;




keep the rudder amid ship and beware the odd typo
 
This can be done by using the backspace character "\b". Also make sure to turn on auto-flush ($|=1).

$| = 1;
for ($i=0; $i<=100; $i++) {
$string = $i . '%';
print &quot;$string&quot;;
$len = length($string);
print &quot;\b&quot; x $len;
sleep(1);
}
print &quot;\n&quot;;

 
This can also be done with escape codes &quot;\e[(char)&quot;. For instance, for a spinny thingy:
Code:
#!/usr/bin/perl
$|=1;
print &quot;\n&quot;;
my @thingy = qw~ \ | / - ~;
while(1) {
    foreach (@thingy) {
        print &quot;\e[A\e[D$_\n&quot;;
    }
}
This moves the cursor up one line and left one character before printing each new character.

brendanc@icehouse.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top