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.
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (Guest)
on 2008-11-04 17: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
63529e6fbb35dde336d471b569b84d87?d=identicon&s=25 Rodrigo Bermejo (rbermejo)
on 2008-11-04 17:23
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
B09f4659460545e38ece34ddd0d96b46?d=identicon&s=25 Yaser Sulaiman (Guest)
on 2008-11-04 19: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 Bermejo
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (Guest)
on 2008-11-04 19: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
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (Guest)
on 2008-11-04 20: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
B09f4659460545e38ece34ddd0d96b46?d=identicon&s=25 Yaser Sulaiman (Guest)
on 2008-11-04 21: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 Sulaiman
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2008-11-04 21: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
63529e6fbb35dde336d471b569b84d87?d=identicon&s=25 Rodrigo Bermejo (rbermejo)
on 2008-11-05 15:20
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
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (Guest)
on 2008-11-05 16: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?
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2008-11-06 23:45
(Received via mailing list)
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.
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2008-11-07 01:09
(Received via mailing list)
On Wed, Nov 5, 2008 at 1:22 AM, Rodrigo Bermejo
<rodrigo.bermejo@ps.ge.com> 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
B09f4659460545e38ece34ddd0d96b46?d=identicon&s=25 Yaser Sulaiman (Guest)
on 2008-11-07 22: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
C1b6b5557723c9db912b075e954166d3?d=identicon&s=25 Jeff Moore (djief)
on 2008-11-08 17: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.