Class: BarOfProgress

Inherits:
Object
  • Object
show all
Defined in:
lib/bar-of-progress.rb,
lib/bar_of_progress/version.rb

Constant Summary collapse

DEFAULTS =
{
  :total => 100,
  :length => 10,
  :braces => %w{[ ]},
  :complete_indicator => "●",
  :partial_indicator => "◍",
  :incomplete_indicator => "◌",
  :precision => 20
}
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BarOfProgress

Returns a new instance of BarOfProgress.



17
18
19
20
21
22
23
# File 'lib/bar-of-progress.rb', line 17

def initialize(options = {})
  @options = DEFAULTS.merge(options)

  #massage data because eww.
  @options[:total] = @options[:total].to_d
  @options[:length] = @options[:length].to_i
end

Instance Method Details

#progress(amount = 0) ⇒ Object



25
26
27
28
29
30
# File 'lib/bar-of-progress.rb', line 25

def progress(amount = 0)
  bubbles = clamp(((amount.to_d / @options[:total]) * @options[:length]), 0, @options[:length]).to_d
  full_bubbles = bubbles.floor
  partial_bubbles = bubbles.truncate(@options[:precision]) % 1 == 0 ? 0 : 1
  "#{@options[:braces][0]}#{chars(@options[:complete_indicator], full_bubbles)}#{chars(@options[:partial_indicator], partial_bubbles)}#{chars(@options[:incomplete_indicator], (@options[:length] - full_bubbles - partial_bubbles))}#{@options[:braces][1]}"
end