Class: PinterestDL::ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/pinterest_dl/progress_bar.rb

Instance Method Summary collapse

Constructor Details

#initialize(total:, width: 30, out: $stdout) ⇒ ProgressBar

Returns a new instance of ProgressBar.



6
7
8
9
10
# File 'lib/pinterest_dl/progress_bar.rb', line 6

def initialize(total:, width: 30, out: $stdout)
  @total = total.zero? ? 1 : total
  @width = width
  @out = out
end

Instance Method Details

#update(done) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/pinterest_dl/progress_bar.rb', line 12

def update(done)
  ratio = done.to_f / @total
  filled = (@width * ratio).round
  bar = ('#' * filled) + ('-' * (@width - filled))
  @out.print "\r[#{bar}] #{done}/#{@total} (#{(ratio * 100).round}%)"
  @out.print "\n" if done >= @total
  @out.flush
end