(quickie) print in place?

D

Damian

was wondering how exactly do you print(...) in place? Like the percent
counter for installers like linux's rpm. In other words so the output
keeps going to the same spot in stead of advancing the cursor.

If this is something only on Linux/Unix thats fine, as thats the only
place I really need to do this. Though portability never hurts.

Many thanks.
 
G

gnari

Damian said:
was wondering how exactly do you print(...) in place? Like the percent
counter for installers like linux's rpm. In other words so the output
keeps going to the same spot in stead of advancing the cursor.

If this is something only on Linux/Unix thats fine, as thats the only
place I really need to do this. Though portability never hurts.

"\r"

gnari
 
B

Ben Morrow

Damian said:
was wondering how exactly do you print(...) in place? Like the percent
counter for installers like linux's rpm. In other words so the output
keeps going to the same spot in stead of advancing the cursor.

If this is something only on Linux/Unix thats fine, as thats the only
place I really need to do this. Though portability never hurts.

Term::ANSIScreen.

Ben
 
D

Damian

gnari said:
"\r"

gnari

This this a good way of doing it? This is what I just tried:

#!/usr/local/bin/perl -w

use strict;

$| = 1; # Needs to be non zero so you cna see print's in a loop.

for my $i (0..10) {
my $c = $i * 10;
print "Percent complete: $c %\r";

sleep(1); # Sleep for 1 second.
}
print "Percent complete: Done!\n"


I notice though that that if you hit a key while it's looping (logged
into ssh), it shifts the whole thing over, then the next itereation
comes along and rewites that portion, ending up with the right hand bit
of text from the shifted part rtemaining there. Is the only way to make
sure that everything up to the end of thel ine is cleared, short of
manually padding it with spaces, which would require I find out the
column width and then find out how much space is left. Theres gotta be a
beter way to do this.
 
D

Damian

Damian said:
gnari wrote:
I notice though that that if you hit a key while it's looping (logged
into ssh), it shifts the whole thing over, then the next itereation
comes along and rewites that portion, ending up with the right hand
bit of text from the shifted part rtemaining there. Is the only way
to make sure that everything up to the end of thel ine is cleared,
short of manually padding it with spaces, which would require I find
out the column width and then find out how much space is left. Theres
gotta be a beter way to do this.

Or would it be bettter to supress input / local echo? Is this even
possible and a good way to go about this?
 
B

Ben Morrow

Damian said:
Or would it be bettter to supress input / local echo? Is this even
possible and a good way to go about this?

Depending on what you're doing, yes, it may well be.

See Term::ReadKey and (as I said before) Term::ANSIScreen.

Ben
 
J

James Willmore

On Mon, 09 Feb 2004 18:30:05 -0800, Damian wrote:

This this a good way of doing it? This is what I just tried:

#!/usr/local/bin/perl -w

use strict;

$| = 1; # Needs to be non zero so you cna see print's in a loop.

for my $i (0..10) {
my $c = $i * 10;
print "Percent complete: $c %\r";

sleep(1); # Sleep for 1 second.
}
print "Percent complete: Done!\n"


I notice though that that if you hit a key while it's looping (logged
into ssh), it shifts the whole thing over, then the next itereation
comes along and rewites that portion, ending up with the right hand bit
of text from the shifted part rtemaining there. Is the only way to make
sure that everything up to the end of thel ine is cleared, short of
manually padding it with spaces, which would require I find out the
column width and then find out how much space is left. Theres gotta be a
beter way to do this.

If you want to have a progress bar or something similar, try
Term::progressBar.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
This fortune is inoperative. Please try another.
 
B

Brad Baxter

was wondering how exactly do you print(...) in place? Like the percent
counter for installers like linux's rpm. In other words so the output
keeps going to the same spot in stead of advancing the cursor.

If this is something only on Linux/Unix thats fine, as thats the only
place I really need to do this. Though portability never hurts.

You've gotten answers that are surely more correct and more portable, but
I typically use this technique for "quickie" progress reporting:

perl -e '$|++;for(1..10){printf "%6d%s", $_, "\b"x6; sleep 1}'

Yes, it suffers from the same problems you mentioned re \r.

Regards,

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top