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.
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
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).
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
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).
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?
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.
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
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.