Class: Pike13::CLI::Progress
- Inherits:
-
Object
- Object
- Pike13::CLI::Progress
- Defined in:
- lib/pike13/cli/progress.rb
Overview
Simple progress indicator for long-running operations
Defined Under Namespace
Classes: Bar
Constant Summary collapse
- SPINNER_FRAMES =
["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"].freeze
Class Method Summary collapse
-
.run(message = "Loading", enabled: true) ⇒ Object
Run a block with progress indication.
Instance Method Summary collapse
-
#initialize(message = "Loading", enabled: true) ⇒ Progress
constructor
A new instance of Progress.
-
#start ⇒ Object
Start the spinner.
-
#stop(final_message = nil) ⇒ Object
Stop the spinner and clear the line.
Constructor Details
#initialize(message = "Loading", enabled: true) ⇒ Progress
Returns a new instance of Progress.
9 10 11 12 13 14 |
# File 'lib/pike13/cli/progress.rb', line 9 def initialize( = "Loading", enabled: true) @message = @enabled = enabled && $stdout.tty? @frame_index = 0 @thread = nil end |
Class Method Details
.run(message = "Loading", enabled: true) ⇒ Object
Run a block with progress indication
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pike13/cli/progress.rb', line 39 def self.run( = "Loading", enabled: true) progress = new(, enabled: enabled) progress.start begin result = yield progress.stop result rescue StandardError => e progress.stop raise e end end |
Instance Method Details
#start ⇒ Object
Start the spinner
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pike13/cli/progress.rb', line 17 def start return unless @enabled @thread = Thread.new do loop do print "\r#{SPINNER_FRAMES[@frame_index]} #{@message}..." @frame_index = (@frame_index + 1) % SPINNER_FRAMES.length sleep 0.1 end end end |
#stop(final_message = nil) ⇒ Object
Stop the spinner and clear the line
30 31 32 33 34 35 36 |
# File 'lib/pike13/cli/progress.rb', line 30 def stop( = nil) return unless @enabled @thread&.kill print "\r#{' ' * (@message.length + 10)}\r" # Clear the line puts if end |