Last active
October 27, 2015 03:24
-
-
Save jpweber/fee0243704e03d3a5fba to your computer and use it in GitHub Desktop.
spinner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def show_wait_spinner(fps=10) | |
chars = %w[| / - \\] | |
delay = 1.0/fps | |
iter = 0 | |
spinner = Thread.new do | |
while iter do # Keep spinning until told otherwise | |
print chars[(iter+=1) % chars.length] | |
sleep delay | |
print "\b" | |
end | |
end | |
yield.tap{ # After yielding to the block, save the return value | |
iter = false # Tell the thread to exit, cleaning up after itself… | |
spinner.join # …and wait for it to do so. | |
} # Use the block's return value as the method's | |
end | |
print "Doing something tricky..." | |
show_wait_spinner{ | |
sleep rand(4)+2 # Simulate a task taking an unknown amount of time | |
} | |
puts "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment