Class: TreasureData::Command::SizeBasedDownloadProgressIndicator

Inherits:
Object
  • Object
show all
Defined in:
lib/td/command/common.rb

Instance Method Summary collapse

Constructor Details

#initialize(msg, total_size, perc_step = 1, min_periodicity = nil) ⇒ SizeBasedDownloadProgressIndicator

Returns a new instance of SizeBasedDownloadProgressIndicator.



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/td/command/common.rb', line 434

def initialize(msg, total_size, perc_step = 1, min_periodicity = nil)
  require 'ruby-progressbar'

  @total_size = total_size
  @total_byte_size = Command.humanize_bytesize(@total_size) unless unknown_progress_mode?

  @progress_bar = ProgressBar.create(
    title: msg,
    total: unknown_progress_mode? ? nil : @total_size,
    format: formated_title(0),
    output: $stdout,
  )
  @progress_bar.refresh

  # perc_step is how small a percentage increment can be shown
  @perc_step = perc_step

  # min_periodicity=X limits updates to one every X second
  @start_time = Time.now.to_i
  @min_periodicity = min_periodicity

  # track progress
  @last_perc_step = 0
  @last_time = @start_time
end

Instance Method Details

#finishObject



479
480
481
482
483
484
485
486
# File 'lib/td/command/common.rb', line 479

def finish
  if unknown_progress_mode?
    @progress_bar.format = "%t : #{Command.humanize_bytesize(@progress_bar.progress).rjust(10)} Done"
    @progress_bar.progress = 0
  else
    update_progress_bar(@progress_bar.total)
  end
end

#update(curr_size) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/td/command/common.rb', line 460

def update(curr_size)
  if unknown_progress_mode?
    update_progress_bar(curr_size)
    true
  else
    ratio = (curr_size.to_f * 100 / @total_size).round(1)
    if ratio >= (@last_perc_step + @perc_step) &&
       (!@min_periodicity || (time = Time.now.to_i) - @last_time >= @min_periodicity)
      update_progress_bar(curr_size)

      @last_perc_step = ratio
      @last_time = time
      true
    else
      false
    end
  end
end