Class: ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/progress_bar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title: "Progress", count: 100) ⇒ ProgressBar

Returns a new instance of ProgressBar.



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#percentageObject

Returns the value of attribute percentage.



5
6
7
# File 'lib/progress_bar.rb', line 5

def percentage
  @percentage
end

Instance Method Details

#increment(value = 1) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# 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