12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/tms/space.rb', line 12
def space(size, options = {})
precision = [options[:precision].to_i, 1].max || 2
length = 4 + precision + (options[:can_be_negative] ? 1 : 0)
number = size.to_i
degree = 0
while number.abs >= 1000 && degree < SIZE_SYMBOLS.length - 1
degree += 1
number /= 1024.0
end
space = "#{(degree == 0 ? number.to_s : "%.#{precision}f" % number).rjust(length)}#{number == 0 ? ' ' : SIZE_SYMBOLS[degree]}"
if options[:color]
unless ''.respond_to?(:red)
require 'toy/fast_gem'
fast_gem 'colored'
end
step = options[:color].is_a?(Hash) && options[:color][:step] || 10
start = options[:color].is_a?(Hash) && options[:color][:start] || 1
coef = 10.0 / (step * Math.log(10))
color = [[Math.log(size) * coef - start, 0].max.to_i, COLORS.length - 1].min rescue 0
Colored.colorize(space, COLORS[color])
else
space
end
end
|