Class: ForemanMaintain::Reporter::CLIReporter::Spinner

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_maintain/reporter/cli_reporter.rb

Overview

Simple spinner able to keep updating current line

Instance Method Summary collapse

Constructor Details

#initialize(reporter, interval = 0.1) ⇒ Spinner

Returns a new instance of Spinner.



10
11
12
13
14
15
16
17
18
19
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 10

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

Instance Method Details

#activateObject



32
33
34
35
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 32

def activate
  @mutex.synchronize { @active = true } unless @reporter.plaintext?
  spin
end

#active?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 28

def active?
  @mutex.synchronize { @active }
end

#deactivateObject



37
38
39
40
41
42
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 37

def deactivate
  return unless active?
  @mutex.synchronize do
    @active = false
  end
end

#start_spinnerObject



44
45
46
47
48
49
50
51
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 44

def start_spinner
  @thread = Thread.new do
    loop do
      spin
      sleep @interval
    end
  end
end

#update(line) ⇒ Object



21
22
23
24
25
26
# File 'lib/foreman_maintain/reporter/cli_reporter.rb', line 21

def update(line)
  @mutex.synchronize do
    @current_line = line
    print_current_line
  end
end