Class: Pike13::CLI::Progress::Bar
- Inherits:
-
Object
- Object
- Pike13::CLI::Progress::Bar
- Defined in:
- lib/pike13/cli/progress.rb
Overview
Simple progress bar for known iterations
Instance Method Summary collapse
- #finish(message = "Complete") ⇒ Object
- #increment(message = nil) ⇒ Object
-
#initialize(total, width: 40, enabled: true) ⇒ Bar
constructor
A new instance of Bar.
Constructor Details
#initialize(total, width: 40, enabled: true) ⇒ Bar
Returns a new instance of Bar.
55 56 57 58 59 60 |
# File 'lib/pike13/cli/progress.rb', line 55 def initialize(total, width: 40, enabled: true) @total = total @current = 0 @width = width @enabled = enabled && $stdout.tty? end |
Instance Method Details
#finish(message = "Complete") ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/pike13/cli/progress.rb', line 75 def finish( = "Complete") return unless @enabled @current = @total = "█" * @width print "\r[#{}] 100% - #{}\n" end |
#increment(message = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pike13/cli/progress.rb', line 62 def increment( = nil) return unless @enabled @current += 1 percentage = (@current.to_f / @total * 100).round filled = (@current.to_f / @total * @width).round = ("█" * filled) + ("░" * (@width - filled)) suffix = ? " - #{}" : "" print "\r[#{}] #{percentage}%#{suffix}" print "\n" if @current >= @total end |