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.



219
220
221
222
223
224
225
226
227
# File 'lib/datasets/downloader.rb', line 219

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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/datasets/downloader.rb', line 229

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