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 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 :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

路路路

--
Posted via http://www.ruby-forum.com/\.

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 :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

--
Posted via http://www.ruby-forum.com/\.

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 :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

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

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

路路路

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 :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

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

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 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-forum.com/\.

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

路路路

On Fri, Nov 7, 2008 at 3:06 AM, Michael Fellinger <m.fellinger@gmail.com>wrote:

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 :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

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

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! :wink: Still, thanks for showing me this, I've now got quite a bit
more that I can learn about :slight_smile:

--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:
>

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

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

路路路

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

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 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?