Class: CliLogger::Spinner

Inherits:
Object
  • Object
show all
Defined in:
lib/cli_logger/spinner.rb

Overview

Spinner class

Instance Method Summary collapse

Constructor Details

#initializeSpinner

Returns a new instance of Spinner.



5
6
7
8
# File 'lib/cli_logger/spinner.rb', line 5

def initialize
  @chars = %w( + - - - )
  @step = 0.095
end

Instance Method Details

#clear_lineObject



37
38
39
40
41
42
43
# File 'lib/cli_logger/spinner.rb', line 37

def clear_line
  width = @chars.count

  print "\r"
  print ' ' * width
  print "\r"
end

#spinObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/cli_logger/spinner.rb', line 26

def spin
  $stdout.sync = true
  Thread.current[:done] = false
  until Thread.current[:done]
    clear_line
    print @chars.join.green
    @chars.unshift @chars.pop
    sleep @step
  end
end

#startObject



10
11
12
13
14
15
# File 'lib/cli_logger/spinner.rb', line 10

def start
  stop
  sleep @step

  @thread = Thread.new { spin }
end

#stopObject



17
18
19
20
21
22
23
24
# File 'lib/cli_logger/spinner.rb', line 17

def stop
  return unless @thread
  return if @thread[:done]

  @thread[:done] = true
  sleep @step / (@chars.count - 1)
  clear_line
end