Class: RubyRich::ProgressBar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total, width: 50, style: :default) ⇒ ProgressBar

Returns a new instance of ProgressBar.



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

def initialize(total, width: 50, style: :default)
  @total = total
  @progress = 0
  @width = width
  @style = style
end

Instance Attribute Details

#progressObject (readonly)

Returns the value of attribute progress.



4
5
6
# File 'lib/ruby_rich/progress_bar.rb', line 4

def progress
  @progress
end

Instance Method Details

#advance(amount) ⇒ Object



13
14
15
16
17
# File 'lib/ruby_rich/progress_bar.rb', line 13

def advance(amount)
  @progress += amount
  @progress = @total if @progress > @total
  render
end

#renderObject



19
20
21
22
23
24
25
26
27
# File 'lib/ruby_rich/progress_bar.rb', line 19

def render
  percentage = (@progress.to_f / @total * 100).to_i
  completed_width = (@progress.to_f / @total * @width).to_i
  incomplete_width = @width - completed_width

  bar = "[#{"=" * completed_width}#{" " * incomplete_width}]"
  print "\r#{bar} #{percentage}%"
  puts if @progress == @total
end