Sha256: 5903b79b9d23f6eca0c75f79b8ac769610b19d38cf3c29c84e05a21fa23fe776
Contents?: true
Size: 1.38 KB
Versions: 2
Compression:
Stored size: 1.38 KB
Contents
# encoding: UTF-8 require 'colored' module Tms module Space SIZE_SYMBOLS = %w[B K M G T P E Z Y].freeze COLORS = [].tap do |colors| [:white, :black, :yellow, :red].each do |color| colors << {:foreground => color} colors << {:foreground => color, :extra => :bold} end colors << {:foreground => :yellow, :extra => :reversed} colors << {:foreground => :red, :extra => :reversed} end.freeze PRECISION = 1 LENGTH = 4 + PRECISION + 1 COEF = 1 / Math.log(10) EMPTY_SPACE = ' ' * LENGTH NOT_COUNTED_SPACE = '!' * LENGTH class << self attr_writer :base10 def denominator @denominator ||= @base10 ? 1000.0 : 1024.0 end def space(size, options = {}) case size when false NOT_COUNTED_SPACE.bold.red when 0 EMPTY_SPACE else number, degree = size, 0 while number.abs >= 1000 && degree < SIZE_SYMBOLS.length - 1 number /= denominator degree += 1 end space = "#{degree == 0 ? number.to_s : "%.#{PRECISION}f" % number}#{SIZE_SYMBOLS[degree]}".rjust(LENGTH) if options[:color] color = [[Math.log(size) * COEF, 1].max.to_i, COLORS.length].min - 1 space = Colored.colorize(space, COLORS[color]) end space end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
tms-1.3.5 | lib/tms/space.rb |
tms-1.3.4 | lib/tms/space.rb |