Class: Philiprehberger::Progress::Spinner
- Inherits:
-
Object
- Object
- Philiprehberger::Progress::Spinner
- Defined in:
- lib/philiprehberger/progress/spinner.rb
Constant Summary collapse
- FRAMES =
["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"].freeze
Instance Attribute Summary collapse
-
#message ⇒ Object
Returns the value of attribute message.
Instance Method Summary collapse
- #auto_spin(interval: 0.1) ⇒ Object
- #current_frame ⇒ Object
-
#initialize(message:, output: $stderr) ⇒ Spinner
constructor
A new instance of Spinner.
- #spin ⇒ Object
- #stop(final_message = 'done') ⇒ Object
- #stopped? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(message:, output: $stderr) ⇒ Spinner
Returns a new instance of Spinner.
11 12 13 14 15 16 |
# File 'lib/philiprehberger/progress/spinner.rb', line 11 def initialize(message:, output: $stderr) @message = @output = output @index = 0 @stopped = false end |
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
9 10 11 |
# File 'lib/philiprehberger/progress/spinner.rb', line 9 def @message end |
Instance Method Details
#auto_spin(interval: 0.1) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/philiprehberger/progress/spinner.rb', line 26 def auto_spin(interval: 0.1) @auto_thread = Thread.new do until @stopped spin sleep(interval) end end self end |
#current_frame ⇒ Object
48 49 50 |
# File 'lib/philiprehberger/progress/spinner.rb', line 48 def current_frame FRAMES[@index % FRAMES.length] end |
#spin ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/philiprehberger/progress/spinner.rb', line 18 def spin return self if @stopped render_to_output @index = (@index + 1) % FRAMES.length self end |
#stop(final_message = 'done') ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/philiprehberger/progress/spinner.rb', line 36 def stop( = 'done') @stopped = true @auto_thread&.join @auto_thread = nil @output.write("\r\e[2K#{}\n") if tty? self end |
#stopped? ⇒ Boolean
44 45 46 |
# File 'lib/philiprehberger/progress/spinner.rb', line 44 def stopped? @stopped end |
#to_s ⇒ Object
52 53 54 |
# File 'lib/philiprehberger/progress/spinner.rb', line 52 def to_s "#{current_frame} #{@message}" end |