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.



178
179
180
181
182
183
184
185
186
# File 'lib/datasets/downloader.rb', line 178

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



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/datasets/downloader.rb', line 188

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