File size: 864 Bytes
874ae95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class CommandLineProgress
  def initialize(max)
    @max = max
    @current = 0
    @percentage = 0

    puts "Starting processing of #{@max} entries..." if @max.positive? # rubocop:disable Rails/Output
  end

  def current_string_length
    @current_string_length ||= @max.to_s.length
  end

  def tick
    @current += 1
    changed = compute_percentage
    inform_progress if changed
  end

  def compute_percentage
    new_percentage = (@current / @max.to_f) * 100.0

    if new_percentage - @percentage >= 10.0
      @percentage += 10
      true
    else
      false
    end
  end

  def inform_progress
    if @percentage.to_i == 100
      puts 'Processing complete!' # rubocop:disable Rails/Output
    else
      puts "Progress: #{@percentage}% [ #{@current.to_s.rjust(current_string_length, ' ')}/#{@max} ]" # rubocop:disable Rails/Output
    end
  end
end