Showing a spinner?

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 :slight_smile:
A spinner has \ | / - all in one spot so it looks like a spinning wheel.

Thanks!

–Aldric

Aldric G. 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 :slight_smile:
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

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

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

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

–Aldric

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

Mark T. 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

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

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?

On Nov 5, 10:14 am, Aldric G. [email protected] 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.

On Wed, Nov 5, 2008 at 1:22 AM, Rodrigo B.
[email protected] wrote:

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/index.html.en 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

That library looks interesting. Thanks for sharing, Michael!

Just want to mention that I tried the same code (posted by Rodrigo)
under
Windows Vista, and it worked fine without using STDOUT.flush.

Regards,
Yaser

Piling on…

Here’s my take…

require ‘thread’

class Spinner

C = [’|’, ‘/’, ‘-’, ‘\’]

def self.spin
@@thr = Thread.new do
$stdout.sync= true
Thread.current[:done]=false
until (Thread.current[:done])
4.times do |i|
print C[i]
sleep(0.1)
print “\b”
end
end
print "\b "
end
end

def self.quit
@@thr[:done]=true
end

end

if FILE == $0

Spinner.spin
sleep 3

Spinner.quit
sleep 3

end