Trying to get dynamic output in console

M

Michal Sza

Hello,
I have a little script that processes excel file and I would like the
script to output some sort of status either line or percent number as it
runs.
How can I output something to console always in the same spot, meaning
that I don't want start new line every time the script advances.
Something like this:
Progress: 51% - I want this number to change and stay in the same spot.
 
M

Morton Goldberg

Hello,
I have a little script that processes excel file and I would like the
script to output some sort of status either line or percent number
as it
runs.
How can I output something to console always in the same spot, meaning
that I don't want start new line every time the script advances.
Something like this:
Progress: 51% - I want this number to change and stay in the same
spot.

Perhaps the following example will help you. Please note the \r at
the beginning of the format strings.

<code>
0.step(100, 10) do |i|
printf((i < 100 ? "\rProgress: %02d\%" : "\rProgress: %3d\%"), i)
$stdout.flush
sleep(1)
end
puts
</code>

Regards, Morton
 
N

Nobuyoshi Nakada

Hi,

At Tue, 10 Jul 2007 03:21:07 +0900,
Morton Goldberg wrote in [ruby-talk:258465]:
0.step(100, 10) do |i|
printf("\rProgress: %3.2d%%", i)
$stdout.flush
sleep(1)
end
puts

The backslash before % is meaningless and just ignored. You
need two %'s to print one %.
 
M

Morton Goldberg

Hi,

At Tue, 10 Jul 2007 03:21:07 +0900,
Morton Goldberg wrote in [ruby-talk:258465]:
0.step(100, 10) do |i|
printf("\rProgress: %3.2d%%", i)
$stdout.flush
sleep(1)
end
puts

The backslash before % is meaningless and just ignored. You
need two %'s to print one %.

You are of course right: the backslash before the percent should be
another percent. But in this case doubling the percent also turns out
to be unnecessary. The following works (in Ruby 1.8.2) although one
would think that it wouldn't:

<code>
0.step(100, 10) do |i|
printf((i < 100 ? "\rProgress: %02d%" : "\rProgress: %3d%"), i)
$stdout.flush
sleep(1)
end
puts
</code>

It seems to work because the single percent occurs at the end of the
format string. Is this a minor bug in printf? Is it behavior
inherited from C?

Regards, Morton
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top