class Toys::Utils::Terminal::SpinDriver

@private

Public Class Methods

new(terminal, frames, style, frame_length) click to toggle source
# File lib/toys/utils/terminal.rb, line 436
def initialize(terminal, frames, style, frame_length)
  @mutex = ::Monitor.new
  @terminal = terminal
  @frames = frames.map do |f|
    [@terminal.apply_styles(f, *style), Terminal.remove_style_escapes(f).size]
  end
  @frame_length = frame_length
  @cur_frame = 0
  @stopping = false
  @cond = @mutex.new_cond
  @thread = @terminal.output.tty? ? start_thread : nil
end

Public Instance Methods

stop() click to toggle source
# File lib/toys/utils/terminal.rb, line 449
def stop
  @mutex.synchronize do
    @stopping = true
    @cond.broadcast
  end
  @thread&.join
  self
end

Private Instance Methods

start_thread() click to toggle source
# File lib/toys/utils/terminal.rb, line 460
def start_thread
  ::Thread.new do
    @mutex.synchronize do
      until @stopping
        @terminal.write(@frames[@cur_frame][0])
        @cond.wait(@frame_length)
        size = @frames[@cur_frame][1]
        @terminal.write("\b" * size + " " * size + "\b" * size)
        @cur_frame += 1
        @cur_frame = 0 if @cur_frame >= @frames.size
      end
    end
  end
end