class Ccp::Utils::Colorize::Meter::Percent

Private Instance Methods

bar(max) click to toggle source
# File lib/ccp/utils/colorize.rb, line 153
def bar(max)
  buf  = ''
  sum = 0
  @rates.each_with_index do |r, i|
    v = @chars[i]  || @chars.first
    c = @colors[i] || @chars.first
    s = (max * r).ceil
    s = max - sum unless sum + s <= max # validate sum should <= max
    o = v * s   # "||||"
    sum += s
    buf << Colorize.__send__(c, o)
  end
  buf << @chars.last * (max - sum)
  return buf
end
label(*) click to toggle source
# File lib/ccp/utils/colorize.rb, line 186
def label(*)
  ""
end
rates(vals) click to toggle source
# File lib/ccp/utils/colorize.rb, line 169
def rates(vals)
  rs = vals.map{|v| v /= 100.0; [[0.0, v].max, 1.0].min }
  # validate sum should <= 1.0

  sum = 0.0
  rs.each_with_index do |r,i|
    available = 1.0 - sum
    if r > available
      rs[i] = available
      (i+1 ... rs.size).each{|_| rs[_] = 0.0}
      return rs
    end
    sum += r
  end
  return rs
end