Class: GeektoolKit::CpuRecord

Inherits:
Object
  • Object
show all
Includes:
LineFormatter
Defined in:
lib/geektool_kit/cpu_record.rb

Constant Summary collapse

NORMAL_RANGE =
0..9
ELEVATED_RANGE =
10..19
WARNING_RANGE =
20..29
CRITICAL_RANGE =
30..200

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LineFormatter

#create_line

Constructor Details

#initialize(line) ⇒ CpuRecord

Returns a new instance of CpuRecord.



15
16
17
18
19
# File 'lib/geektool_kit/cpu_record.rb', line 15

def initialize line

  self.name = line[:name].strip
  self.percent = line[:percent].to_f
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/geektool_kit/cpu_record.rb', line 12

def name
  @name
end

#percentObject

Returns the value of attribute percent.



13
14
15
# File 'lib/geektool_kit/cpu_record.rb', line 13

def percent
  @percent
end

Class Method Details

.get_dataObject



59
60
61
# File 'lib/geektool_kit/cpu_record.rb', line 59

def self.get_data
  %x{ps -arcwwwxo "command %cpu"}
end

.get_recordsObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/geektool_kit/cpu_record.rb', line 63

def self.get_records

  data = self.get_data.encode("UTF-8", "binary", :invalid => :replace, :undef => :replace, :replace => "#").split("\n")

  records = []
  data.each do |d|
    matches = /(?<name>.*)\s+(?<percent>\d{1,}\.\d+)/.match(d)
    records << CpuRecord.new(matches) unless matches.nil?
  end

  return records
end

Instance Method Details

#<=>(other) ⇒ Object



43
44
45
# File 'lib/geektool_kit/cpu_record.rb', line 43

def <=> other
  return other.percent <=> self.percent
end

#colorObject



29
30
31
32
33
34
35
36
37
# File 'lib/geektool_kit/cpu_record.rb', line 29

def color
  case percent.to_i
     when NORMAL_RANGE then Colors.DEFAULT
     when ELEVATED_RANGE then Colors.CYAN
     when WARNING_RANGE then Colors.YELLOW
     when CRITICAL_RANGE then Colors.RED
     else Colors.RED
  end
end

#colorize_line(line) ⇒ Object



55
56
57
# File 'lib/geektool_kit/cpu_record.rb', line 55

def colorize_line line
  "#{color}#{line}#{Colors.RESET}"
end

#create_display_text(max_width = 30) ⇒ Object



51
52
53
# File 'lib/geektool_kit/cpu_record.rb', line 51

def create_display_text max_width = 30
  create_line(name, create_percent_display_text, max_width)
end

#create_percent_display_text(precision = 1) ⇒ Object



47
48
49
# File 'lib/geektool_kit/cpu_record.rb', line 47

def create_percent_display_text precision = 1
  "#{"%.#{precision}f" % percent}%"
end

#is_consequential?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/geektool_kit/cpu_record.rb', line 25

def is_consequential?
  !percent.nil? && percent.to_i > 1
end

#is_valid?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/geektool_kit/cpu_record.rb', line 21

def is_valid?
  !self.name.nil? && !self.percent.nil?
end


39
40
41
# File 'lib/geektool_kit/cpu_record.rb', line 39

def print
  puts colorize_line(create_display_text)
end