Class: Package::Audit::Util::Spinner

Inherits:
Object
  • Object
show all
Defined in:
lib/package/audit/util/spinner.rb

Constant Summary collapse

ANIMATION_SPEED =
0.1
STATES =
%w[| / - \\]

Instance Method Summary collapse

Constructor Details

#initialize(message = 'Loading...') ⇒ Spinner

Returns a new instance of Spinner.



8
9
10
11
12
# File 'lib/package/audit/util/spinner.rb', line 8

def initialize(message = 'Loading...')
  @message = message
  @running = false
  @thread = nil
end

Instance Method Details

#startObject

rubocop:disable Metrics/MethodLength



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/package/audit/util/spinner.rb', line 14

def start # rubocop:disable Metrics/MethodLength
  return if ENV['RACK_ENV'] == 'test'
  raise 'Loading indicator already started.' if @running

  @running = true
  @thread = Thread.new do
    step = 0
    while @running
      if @running && (ENV['RUBY_ENV'] != 'test' && ENV['RACK_ENV'] != 'test')
        print "\r#{@message} #{STATES[step % STATES.length]}"
      end
      sleep ANIMATION_SPEED
      step += 1
    end
  end
end

#stopObject



31
32
33
34
35
36
37
38
# File 'lib/package/audit/util/spinner.rb', line 31

def stop
  return if ENV['RACK_ENV'] == 'test'
  return unless @running

  @running = false
  @thread&.join
  clear_console_line
end