[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Showing a spinner ?

Aldric Giacomoni

11/4/2008 4:08:00 PM

How do I show a spinner on the command line interface when a ruby script
is working? I sometimes write things that take a while, and I know I get
impatient when all I see a blinking cursor :)
A spinner has \ | / - all in one spot so it looks like a spinning wheel.

Thanks!

--Aldric
12 Answers

Rodrigo Bermejo

11/4/2008 4:22:00 PM

0

Aldric Giacomoni wrote:
> How do I show a spinner on the command line interface when a ruby script
> is working? I sometimes write things that take a while, and I know I get
> impatient when all I see a blinking cursor :)
> A spinner has \ | / - all in one spot so it looks like a spinning wheel.
>
> Thanks!
>
> --Aldric

Here is the idea ..you can make it better..

a=["-","\\","|","/"]
n=0
while 1 do
print a[n % 4]
print "\b"
n+=1
sleep 0.1
puts "..#{n}." if (n % 50==0)
end


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

Yaser Sulaiman

11/4/2008 6:34:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

I think a STDOUT.flush is needed after the second print "\b" to make it work
properly.

On Tue, Nov 4, 2008 at 7:22 PM, Rodrigo Bermejo
<rodrigo.bermejo@ps.ge.com>wrote:

> Aldric Giacomoni wrote:
> > How do I show a spinner on the command line interface when a ruby script
> > is working? I sometimes write things that take a while, and I know I get
> > impatient when all I see a blinking cursor :)
> > A spinner has \ | / - all in one spot so it looks like a spinning wheel.
> >
> > Thanks!
> >
> > --Aldric
>
> Here is the idea ..you can make it better..
>
> a=["-","\\","|","/"]
> n=0
> while 1 do
> print a[n % 4]
> print "\b"
> n+=1
> sleep 0.1
> puts "..#{n}." if (n % 50==0)
> end
>
>
> --
> Posted via http://www.ruby-....
>
>

Aldric Giacomoni

11/4/2008 6:36:00 PM

0

Great, thanks! What other special characters are there? Where can I find
a list? (I know - I could probably just google that, so if you don't
have it handy I'll go and do my own research when I get a chance).

--Aldric

Rodrigo Bermejo wrote:
> Aldric Giacomoni wrote:
>> How do I show a spinner on the command line interface when a ruby script
>> is working? I sometimes write things that take a while, and I know I get
>> impatient when all I see a blinking cursor :)
>> A spinner has \ | / - all in one spot so it looks like a spinning wheel.
>>
>> Thanks!
>>
>> --Aldric
>
> Here is the idea ..you can make it better..
>
> a=["-","\\","|","/"]
> n=0
> while 1 do
> print a[n % 4]
> print "\b"
> n+=1
> sleep 0.1
> puts "..#{n}." if (n % 50==0)
> end
>
>

Aldric Giacomoni

11/4/2008 7:03:00 PM

0

The STDOUT.flush makes it seem frozen in time - not my idea of a
spinner! ;-) Still, thanks for showing me this, I've now got quite a bit
more that I can learn about :)

--Aldric

Yaser Sulaiman wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> I think a STDOUT.flush is needed after the second print "\b" to make it work
> properly.
>
> On Tue, Nov 4, 2008 at 7:22 PM, Rodrigo Bermejo
> <rodrigo.bermejo@ps.ge.com>wrote:
>

Yaser Sulaiman

11/4/2008 8:12:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Well, when I remove STDOUT.flush, the spinner doesn't show at all, and I
only get:
..50.
..100.
..150.
..200.
[etc.]
with a 5-second "pause" at the beginning of each line.

I wonder if this behavior is somehow related to the OS (Ubuntu in my case).

Regards,
Yaser Sulaiman

On Tue, Nov 4, 2008 at 10:03 PM, Aldric Giacomoni <aldric@trevoke.net>wrote:

> The STDOUT.flush makes it seem frozen in time - not my idea of a
> spinner! ;-) Still, thanks for showing me this, I've now got quite a bit
> more that I can learn about :)
>
> --Aldric
>
> Yaser Sulaiman wrote:
> > [Note: parts of this message were removed to make it a legal post.]
> >
> > I think a STDOUT.flush is needed after the second print "\b" to make it
> work
> > properly.
> >
> > On Tue, Nov 4, 2008 at 7:22 PM, Rodrigo Bermejo
> > <rodrigo.bermejo@ps.ge.com>wrote:
> >
>
>

Mark Thomas

11/4/2008 8:35:00 PM

0


> a=["-","\\","|","/"]
> n=0
> while 1 do
>     print a[n % 4]
>     print "\b"
>     n+=1
>     sleep 0.1
>     puts "..#{n}." if (n % 50==0)
> end


In ruby 1.9:

n=0
a=["-","\\","|","/"].cycle do |a|
print a
print "\b"
n+=1
sleep 0.1
break if (n % 50).zero?
end

Rodrigo Bermejo

11/5/2008 2:19:00 PM

0

Mark Thomas wrote:

>
> In ruby 1.9:
>
> n=0
> a=["-","\\","|","/"].cycle do |a|
> print a
> print "\b"
> n+=1
> sleep 0.1
> break if (n % 50).zero?
> end

woooow ! - I'm becoming obsolete =c
--
Posted via http://www.ruby-....

Aldric Giacomoni

11/5/2008 3:14:00 PM

0

I don't see anything different about this code, except 'cycle' which is
neat.
Why assign the array to 'a' if we're just gonna iterate through it
anyway, though?

Rodrigo Bermejo wrote:
> Mark Thomas wrote:
>
>> In ruby 1.9:
>>
>> n=0
>> a=["-","\\","|","/"].cycle do |a|
>> print a
>> print "\b"
>> n+=1
>> sleep 0.1
>> break if (n % 50).zero?
>> end
>
> woooow ! - I'm becoming obsolete =c

Mark Thomas

11/6/2008 10:40:00 PM

0

On Nov 5, 10:14 am, Aldric Giacomoni <ald...@trevoke.net> wrote:
> I don't see anything different about this code, except 'cycle' which is
> neat.
> Why assign the array to 'a' if we're just gonna iterate through it
> anyway, though?

You're right--that was a copy and paste error. The assignment
shouldn't be there.

Another neat thing about cycle: you can do this

a = ["green","yellow","red"].cycle

Now a is an iterator with special behavior:

a.next #=> "green"
a.next #=> "yellow"
a.next #=> "red"
a.next #=> "green"


-- Mark.

Michael Fellinger

11/7/2008 12:07:00 AM

0

On Wed, Nov 5, 2008 at 1:22 AM, Rodrigo Bermejo
<rodrigo.bermejo@ps.ge.com> wrote:
> Aldric Giacomoni wrote:
>> How do I show a spinner on the command line interface when a ruby script
>> is working? I sometimes write things that take a while, and I know I get
>> impatient when all I see a blinking cursor :)
>> A spinner has \ | / - all in one spot so it looks like a spinning wheel.
>>
>> Thanks!
>>
>> --Aldric
>
> Here is the idea ..you can make it better..
>
> a=["-","\\","|","/"]
> n=0
> while 1 do
> print a[n % 4]
> print "\b"
> n+=1
> sleep 0.1
> puts "..#{n}." if (n % 50==0)
> end

Just had the same thing lying around to keep ssh alive, not much in
terms of progress though
See http://0xcc.net/ruby-progressbar/ind... for a full lib that
handles this problem

sigma ~prog/ruby % cat spinner.rb
a = %w[ | / - \\ ]

$stdout.sync = true

loop do
print a.unshift(a.pop).last
sleep 0.1
print "\b"
end