class ProgressBar

Attributes

percentage[R]

Public Class Methods

new(title: "Progress", count: 100) click to toggle source
# File lib/progress_bar.rb, line 7
def initialize(title: "Progress", count: 100)
  @start_time = Time.now
  @percentage = 0
  @initial_string = "\u2591" * 50
  @title = title
  @count = count
end

Public Instance Methods

increment(value = 1) click to toggle source
# File lib/progress_bar.rb, line 20
def increment(value = 1)
  unless value.zero?
    @percentage += value * 100.0 / @count
    error_check(@percentage)
  end
  rounded_percentage = @percentage.round(5)
  normalized_percentage = rounded_percentage.to_i / 2
  progress = "\u2588" * normalized_percentage
  remaining = @initial_string[normalized_percentage..49]
  print "\r#{@title}: #{progress.green}#{remaining} #{rounded_percentage.to_i}%"
  puts "\nCompleted in #{time_taken}" if rounded_percentage == 100
end
percentage=(value) click to toggle source
# File lib/progress_bar.rb, line 15
def percentage=(value)
  @percentage = value
  increment(0)
end

Private Instance Methods

error_check(value) click to toggle source
# File lib/progress_bar.rb, line 46
def error_check(value)
  return if value.is_a?(Numeric) && value.between?(0, 100)
  raise ArgumentError, 'Value must be a number between 0 and 100'
end
time_taken() click to toggle source
# File lib/progress_bar.rb, line 35
def time_taken
  seconds = (Time.now - @start_time).round
  if seconds < 60
    "#{seconds}s"
  elsif seconds < 3600
    "#{seconds / 60}m #{seconds % 60}s"
  else
    "#{seconds / 3600}h #{(seconds / 60) % 60}m #{seconds % 60}s"
  end
end