simple-spinner/src/spinner.cr

39 lines
606 B
Crystal
Raw Normal View History

2015-12-28 07:54:44 +00:00
require "./spinner/*"
2015-12-30 11:55:14 +00:00
class Spin
property delay, chars
CL = STDOUT.tty? ? "\u001b[0G" : "\u000d \u000d"
CLEAR = STDOUT.tty? ? "\u001b[2K" : "\u000d"
2015-12-30 14:21:42 +00:00
def initialize(@delay = 0.1, @chars = Spinner::Charset[:pipe])
2015-12-30 11:55:14 +00:00
@state = true
end
def stop
@state = false
print CLEAR
end
private def clear(count)
print CL * count
end
def start
spawn do
i = 0
while @state
chars = @chars[i % @chars.size]
print "#{chars}"
# to_s for colorize
clear(chars.to_s.size)
sleep @delay
i += 1
end
end
end
2015-12-28 07:54:44 +00:00
end