Class: PS::ProcessList::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/ps/process_list_printer.rb

Instance Method Summary collapse

Constructor Details

#initialize(pl, headers = nil) ⇒ Printer

Returns a new instance of Printer.



9
10
11
12
# File 'lib/ps/process_list_printer.rb', line 9

def initialize pl, headers=nil
  @headers = headers || %w{# pid ppid user pcpu mem command}
  @process_list = pl
end

Instance Method Details

#dataObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ps/process_list_printer.rb', line 14

def data
  data = []

  data << header_cells = @headers.collect do |header|
    {:val => header, :header => header}
  end

  @process_list.to_a.each_with_index do |proc,i|
    data << @headers.collect do |header|
      val = header == '#' ? i : proc.send(header)
      
      cell = {:val => val, :header => header}

      case header
      when 'pid'
        cell[:format] = [:blue]
      when '#'
        cell[:format] = [:yellow]
        cell[:align] = :right
      when 'user','ruser'
        cell[:align] = :center
      when 'mem'
        cell[:val] = val.round.to_s + 'mb'
        cell[:align] = :right
      when 'pcpu', 'pmem'
        cell[:align] = :right
        cell[:format] = case cell[:val]
        when 0.0...25.0
          []
        when 25.0..75.0
          [:red]
        else
          [:white,:on_red]
        end
        
        cell[:val] = (cell[:val]).to_i.to_s+'%'
      end

      cell
    end
  end

  data
end

#to_sObject



59
60
61
# File 'lib/ps/process_list_printer.rb', line 59

def to_s
  printgrid(data)
end