class ForemanMaintain::Reporter::CLIReporter::Spinner

Simple spinner able to keep updating current line

Public Class Methods

new(reporter, interval = 0.1) click to toggle source
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 9
def initialize(reporter, interval = 0.1)
  @reporter = reporter
  @mutex = Mutex.new
  @active = false
  @interval = interval
  @spinner_index = 0
  @spinner_chars = %w[| / - \\]
  @current_line = ''
  @puts_needed = false
end

Public Instance Methods

activate() click to toggle source
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 31
def activate
  @mutex.synchronize { @active = true } unless @reporter.plaintext?
  spin
end
active?() click to toggle source
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 27
def active?
  @mutex.synchronize { @active }
end
deactivate() click to toggle source
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 36
def deactivate
  return unless active?
  @mutex.synchronize do
    @active = false
  end
end
start_spinner() click to toggle source
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 43
def start_spinner
  @thread = Thread.new do
    loop do
      spin
      sleep @interval
    end
  end
end
update(line) click to toggle source
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 20
def update(line)
  @mutex.synchronize do
    @current_line = line
    print_current_line
  end
end

Private Instance Methods

print_current_line() click to toggle source
spin() click to toggle source
# File lib/foreman_maintain/reporter/cli_reporter.rb, line 54
def spin
  @mutex.synchronize do
    return unless @active
    print_current_line
    @spinner_index = (@spinner_index + 1) % @spinner_chars.size
  end
end