simple-spinner/src/spinner.cr

41 lines
655 B
Crystal
Raw Permalink Normal View History

2015-12-28 07:54:44 +00:00
require "./spinner/*"
2015-12-30 11:55:14 +00:00
class Spin
2017-06-21 05:21:34 +00:00
property delay
property chars : Array(String)
2015-12-30 11:55:14 +00:00
CL = STDOUT.tty? ? "\u001b[0G" : "\u000d \u000d"
CLEAR = STDOUT.tty? ? "\u001b[2K" : "\u000d"
2017-06-21 05:21:34 +00:00
def initialize(@delay = 0.1, chars = Spinner::Charset[:pipe])
2015-12-30 11:55:14 +00:00
@state = true
2017-06-21 05:21:34 +00:00
@chars = chars.to_a
2015-12-30 11:55:14 +00:00
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