Class: Gitingest::ProgressIndicator

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

Constant Summary collapse

BAR_WIDTH =
30

Instance Method Summary collapse

Constructor Details

#initialize(total, logger) ⇒ ProgressIndicator

Returns a new instance of ProgressIndicator.



361
362
363
364
365
366
367
368
# File 'lib/gitingest/generator.rb', line 361

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



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/gitingest/generator.rb', line 370

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