Class: Whosup::Meter

Inherits:
Object
  • Object
show all
Defined in:
lib/whosup/meter.rb

Constant Summary collapse

MAX =
15_000.0

Instance Method Summary collapse

Instance Method Details

#<<(data) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/whosup/meter.rb', line 6

def <<(data)
  level = data.map(&:first).map(&:abs).reduce(:+) / data.size
  percent = level / MAX
  percent = 1 if percent > 1
  bars = (percent * columns).to_i
  print "\r"
  (0..bars).each do |i|
    color = 32 if i <= (columns * 0.5).to_i
    color = 33 if i > (columns * 0.5).to_i
    color = 31 if i >= (columns * 0.75).to_i
    print colorize("|", color)
  end
  print " " * (columns - bars)
end

#colorize(text, color_code) ⇒ Object



21
22
23
# File 'lib/whosup/meter.rb', line 21

def colorize(text, color_code)
  "\e[#{color_code}m#{text}\e[0m"
end

#columnsObject



25
26
27
# File 'lib/whosup/meter.rb', line 25

def columns
  @columns ||= Whosup::Terminal.columns
end