Class: CivoCLI::Spinner
- Inherits:
-
Object
show all
- Defined in:
- lib/spinner.rb
Constant Summary
collapse
- SPINNER_SHAPES =
['|', '/', '-', '\\'].freeze
- DELAY =
0.1
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data = {}, &block) ⇒ Spinner
Returns a new instance of Spinner.
12
13
14
15
16
17
18
19
20
|
# File 'lib/spinner.rb', line 12
def initialize(data = {}, &block)
@quiet = data.delete(:quiet) || false
@data = data
@spinner_frame = 0
@counter = 20
@total = 3600 / DELAY
@block = block
spin
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
22
23
24
|
# File 'lib/spinner.rb', line 22
def method_missing(name)
@data[name] if @data.keys.include?(name)
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6
7
8
|
# File 'lib/spinner.rb', line 6
def data
@data
end
|
Class Method Details
.spin(data = {}, &block) ⇒ Object
8
9
10
|
# File 'lib/spinner.rb', line 8
def self.spin(data = {}, &block)
new(data, &block).spin
end
|
Instance Method Details
#[](key) ⇒ Object
26
27
28
|
# File 'lib/spinner.rb', line 26
def [](key)
@data[key] if @data.keys.include?(key)
end
|
#[]=(key, value) ⇒ Object
30
31
32
|
# File 'lib/spinner.rb', line 30
def []=(key, value)
@data[key] = value
end
|
#spin ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/spinner.rb', line 34
def spin
print "\033[?25l" unless @quiet
while(@total > 0) do
sleep(DELAY)
print SPINNER_SHAPES[@spinner_frame] + "\b" unless @quiet
@spinner_frame += 1
@spinner_frame = 0 if @spinner_frame == SPINNER_SHAPES.length
@counter -= 1
@total -= 1
next unless @counter == 0
@counter = 20
if result = @block.call(self)
self.data[:result] = result
print "\033[?25h" unless @quiet
return self
end
end
print "\033[?25h" unless @quiet
rescue Interrupt
print "\b\b" + "Exiting.\n"
exit 1
ensure
print "\033[?25h" unless @quiet
end
|