class TheFox::Timr::ProgressBar

See ruby-progressbar Issue #131.

Public Class Methods

new(options = Hash.new) click to toggle source
# File lib/timr/progressbar.rb, line 8
def initialize(options = Hash.new)
    @total = options.fetch(:total, 100)
    @progress = options.fetch(:progress, 0)
    @length = options.fetch(:length, 10)
    @progress_mark = options.fetch(:progress_mark, ?#)
    @remainder_mark = options.fetch(:remainder_mark, ?-)
end

Public Instance Methods

render(progress = nil) click to toggle source

Render ProgressBar as String.

# File lib/timr/progressbar.rb, line 17
def render(progress = nil)
    if progress
        @progress = progress
    end
    
    progress_f = @progress.to_f / @total.to_f
    if progress_f > 1.0
        progress_f = 1.0
    end
    
    progress_f = @length.to_f * progress_f
    
    progress_s = @progress_mark * progress_f
    
    remain_l = @length - progress_s.length
    
    remain_s = @remainder_mark * remain_l
    
    '%s%s' % [progress_s, remain_s]
end