lib/lapidarius/cli.rb in lapidarius-4.0.1 vs lib/lapidarius/cli.rb in lapidarius-4.1.0

- old
+ new

@@ -5,22 +5,26 @@ class CLI HELP_FLAGS = %w[-h --help] attr_reader :name, :version, :remote - def initialize(args: [], io: STDOUT, command: Command, cutter: Cutter, tree: Tree) + def initialize(args: [], io: STDOUT, command: Command, cutter: Cutter, + tree: Tree, spinner: Spinner.new) @args = args @io = io - @tree = tree @command = command @cutter = cutter + @tree = tree + @spinner = spinner @name = @args.shift unless help? parser.parse!(@args) end def call - @io.puts output + @spinner.call do + @io.puts output + end end private def cutter @cutter.new(name: @name, cmd_klass: @command, version: @version, remote: @remote) end @@ -52,8 +56,32 @@ end end private def help? HELP_FLAGS.any? { |h| @args.first == h } + end + end + + class Spinner + CHARS = %w[| / - \\] + + def initialize(fps = 15, delay = 1.0) + @fps = fps.to_i + @delay = delay.to_f / @fps + @iter = 0 + end + + def call + spinner = Thread.new do + while @iter do + print CHARS[(@iter+=1) % CHARS.length] + sleep @delay + print "\b" + end + end + yield.tap do + @iter = false + spinner.join + end end end end