Class: Datasets::Downloader::ProgressReporter

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

Instance Method Summary collapse

Constructor Details

#initialize(base_name, size_max) ⇒ ProgressReporter

Returns a new instance of ProgressReporter.



98
99
100
101
102
103
104
105
106
# File 'lib/datasets/downloader.rb', line 98

def initialize(base_name, size_max)
  @base_name = base_name
  @size_max = size_max

  @time_previous = Time.now
  @size_previous = 0

  @need_report = ($stderr == STDERR and $stderr.tty?)
end

Instance Method Details

#report(size_current) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/datasets/downloader.rb', line 108

def report(size_current)
  return unless @need_report
  return if @size_max.nil?
  return unless foreground?

  done = (size_current == @size_max)
  time_current = Time.now
  if not done and time_current - @time_previous <= 1
    return
  end

  read_bytes = size_current - @size_previous
  throughput = read_bytes.to_f / (time_current - @time_previous)
  @time_previous = time_current
  @size_previous = size_current

  message = build_message(size_current, throughput)
  $stderr.print("\r#{message}") if message
  $stderr.puts if done
end