Skip to content

Instantly share code, notes, and snippets.

@jpweber
Last active October 27, 2015 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpweber/fee0243704e03d3a5fba to your computer and use it in GitHub Desktop.
Save jpweber/fee0243704e03d3a5fba to your computer and use it in GitHub Desktop.
spinner
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