Class: Gitingest::ProgressIndicator

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

Constant Summary collapse

BAR_WIDTH =
30

Instance Method Summary collapse

Constructor Details

#initialize(total, logger) ⇒ ProgressIndicator

Returns a new instance of ProgressIndicator.



7
8
9
10
11
12
13
14
# File 'lib/gitingest/progress_indicator.rb', line 7

def initialize(total, logger)
  @total = total
  @logger = logger
  @last_percent = 0
  @start_time = Time.now
  @last_update_time = Time.now
  @update_interval = 0.5
end

Instance Method Details

#update(current) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitingest/progress_indicator.rb', line 16

def update(current)
  now = Time.now
  return if now - @last_update_time < @update_interval && current != @total

  @last_update_time = now
  percent = (current.to_f / @total * 100).round
  return unless percent > @last_percent || current == @total

  elapsed = now - @start_time
  progress_chars = (BAR_WIDTH * (current.to_f / @total)).round
  bar = "[#{"|" * progress_chars}#{" " * (BAR_WIDTH - progress_chars)}]"

  rate = if elapsed.positive?
           (current / elapsed).round(1)
         else
           0 # Avoid division by zero if elapsed time is zero
         end
  eta_string = current.positive? && percent < 100 && rate.positive? ? " ETA: #{format_time((@total - current) / rate)}" : ""

  print "\r\e[K#{bar} #{percent}% | #{current}/#{@total} files (#{rate} files/sec)#{eta_string}"
  print "\n" if current == @total
  if (percent % 10).zero? && percent != @last_percent || current == @total
    @logger.info "Processing: #{percent}% complete (#{current}/#{@total} files)#{eta_string}"
  end
  @last_percent = percent
end