class Ccp::Utils::Colorize::Bar::Progress

Public Class Methods

new(colors, format, size, vals, chars = nil) click to toggle source
# File lib/ccp/utils/colorize.rb, line 70
def initialize(colors, format, size, vals, chars = nil)
  @colors = colors.must.coerced(Array, Symbol=>lambda{|x| [x, :black]}) # [:green, :black]
  @format = format.must(String)               # "Mem[%sMB]"
  @size   = size.must(Fixnum)                 # 73
  @vals   = vals.must(Fixnum, Float, Array) # [4004,24105]
  @chars  = chars.must(Array)  { ["|"] }
  @count  = @vals.is_a?(Array) ? @vals.size : 1 # value count (fill gaps with Fixnum and Array)

  if @vals.is_a?(Array) and @vals.size < 2
    raise "vals.size expected >= %d, but got %d" % [2, @vals.size]
  end
  if @chars.size < @count
    @chars += [@chars.last] * (@vals.size - @chars.size)
  end
  if @chars.size < @count + 1
    @chars += [' ']
  end
  @colors.size >= 2 or raise "colors.size expected >= %d, but got %d" % [2, @colors.size]

  @label  = label(@vals)                    # "4004/24105"
  @rates  = rates(@vals)                    # [0.16]
end

Public Instance Methods

to_s() click to toggle source
# File lib/ccp/utils/colorize.rb, line 93
def to_s
  rest = @size - @format.size + 2 - @label.size
  return @format % @label if rest <= 0
  return @format % (bar(rest) + @label)
end

Private Instance Methods

_rate(x) click to toggle source
# File lib/ccp/utils/colorize.rb, line 123
def _rate(x)
  r = case x
      when Fixnum ; x / 100.0
      when Float  ; x
      when Array  ; (v, a) = x; _rate(v.to_f/a)
      else ; raise "error: rate got #{x.class}"
      end
  return [[0.0, r].max, 1.0].min
end
bar(max) click to toggle source
# File lib/ccp/utils/colorize.rb, line 100
def bar(max)
  o = @chars.first * ((max * @rates).ceil)  # "||||"
  x = @chars.last  * (max - o.size)         # "                  "
  Colorize.__send__(@colors.first, o) + Colorize.__send__(@colors.last, x)
end
label(width) click to toggle source
# File lib/ccp/utils/colorize.rb, line 106
def label(width)
  p = lambda{|x| x.is_a?(Float) ? ("%.1f" % x) : x.to_s }

  case width
  when Float
    return("%.1f%%" % [width*100])
  when Array
    return width.map{|_| p[_]}.join("/")
  else
    return p[width] + "%"
  end
end
rates(vals) click to toggle source
# File lib/ccp/utils/colorize.rb, line 119
def rates(vals)
  return _rate(vals)
end