Class: Pike13::CLI::Progress::Bar

Inherits:
Object
  • Object
show all
Defined in:
lib/pike13/cli/progress.rb

Overview

Simple progress bar for known iterations

Instance Method Summary collapse

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(message = "Complete")
  return unless @enabled

  @current = @total
  bar = "" * @width
  print "\r[#{bar}] 100% - #{message}\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(message = nil)
  return unless @enabled

  @current += 1
  percentage = (@current.to_f / @total * 100).round
  filled = (@current.to_f / @total * @width).round
  bar = ("" * filled) + ("" * (@width - filled))

  suffix = message ? " - #{message}" : ""
  print "\r[#{bar}] #{percentage}%#{suffix}"
  print "\n" if @current >= @total
end