-
-
Save microspino/31244e9a0589a64be67a56f74ead013a to your computer and use it in GitHub Desktop.
Heroku style spinner
This file contains hidden or 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
# Shamelessly stolen from https://gist.github.com/ileitch/1459987 | |
module InterruptibleSleep | |
def interruptible_sleep(seconds) | |
@sleep_check, @sleep_interrupt = IO.pipe | |
IO.select([@sleep_check], nil, nil, seconds) | |
end | |
def interrupt_sleep | |
@sleep_interrupt.close if @sleep_interrupt | |
end | |
end |
This file contains hidden or 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
require 'interruptible_sleep' | |
class Spinner | |
include InterruptibleSleep | |
GLYPHS = %w(⣾ ⣷ ⣯ ⣟ ⡿ ⢿ ⣻ ⣽).reverse | |
INTERVAL_DURATION = 0.07 | |
def initialize | |
@current_glyph = 0 | |
end | |
def progress | |
loop do | |
increment | |
print "\r#{glyph} " | |
interruptible_sleep(INTERVAL_DURATION) | |
end | |
end | |
def stop | |
print "\r" | |
interrupt_sleep | |
end | |
def self.show_progress(&block) | |
process = fork(&block) | |
progress = fork do | |
progress_indicator = Spinner.new | |
progress_indicator.progress | |
end | |
Process.wait(process) | |
Process.kill("HUP", progress) | |
print "\r" | |
end | |
private | |
def increment | |
@current_glyph = @current_glyph + 1 | |
@current_glyph = 0 if @current_glyph >= GLYPHS.length | |
end | |
def glyph | |
GLYPHS[@current_glyph] | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment