[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Trying to get dynamic output in console

Misiek Sz

7/9/2007 5:28:00 PM

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.

--
Posted via http://www.ruby-....

5 Answers

Dick Monahan

7/9/2007 5:39:00 PM

0


"Michal Sza" <nicpon@nicpon.net> wrote in message
news:033840e271b07d6344e95506465cb536@ruby-forum.com...
> 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.

for i in 1..10 do

print "Progress #{i}% is done.\r"
sleep 1

end



Morton Goldberg

7/9/2007 6:21:00 PM

0

On Jul 9, 2007, at 1:28 PM, Michal Sza wrote:

> 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



Misiek Sz

7/9/2007 6:31:00 PM

0

This is exactly what I was looking for.
Thanks.

--
Posted via http://www.ruby-....

Nobuyoshi Nakada

7/10/2007 4:45:00 AM

0

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

--
Nobu Nakada

Morton Goldberg

7/10/2007 5:24:00 AM

0

On Jul 10, 2007, at 12:45 AM, Nobuyoshi Nakada wrote:

> 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