Forum: Ruby Showing a spinner ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aldric G. (Guest)
on 2008-11-04 18:10
(Received via mailing list)
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
Rodrigo B. (Guest)
on 2008-11-04 18:23
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 :)
> 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
Yaser S. (Guest)
on 2008-11-04 20:37
(Received via mailing list)
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.
Aldric G. (Guest)
on 2008-11-04 20:40
(Received via mailing list)
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
Aldric G. (Guest)
on 2008-11-04 21:05
(Received via mailing list)
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 S. (Guest)
on 2008-11-04 22:15
(Received via mailing list)
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.
Mark T. (Guest)
on 2008-11-04 22:35
(Received via mailing list)
> 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 B. (Guest)
on 2008-11-05 16:20
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
Aldric G. (Guest)
on 2008-11-05 17:16
(Received via mailing list)
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?
Mark T. (Guest)
on 2008-11-07 00:45
(Received via mailing list)
On Nov 5, 10:14 am, Aldric G. <removed_email_address@domain.invalid> 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 F. (Guest)
on 2008-11-07 02:09
(Received via mailing list)
On Wed, Nov 5, 2008 at 1:22 AM, Rodrigo B.
<removed_email_address@domain.invalid> 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
Yaser S. (Guest)
on 2008-11-07 23:24
(Received via mailing list)
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
Jeff M. (Guest)
on 2008-11-08 18:32
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
This topic is locked and can not be replied to.