Class: PS::ProcessList

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

Defined Under Namespace

Classes: Printer

Instance Method Summary collapse

Constructor Details

#initialize(target = nil) ⇒ ProcessList

Returns a new instance of ProcessList.



3
4
5
# File 'lib/ps/process_list.rb', line 3

def initialize(target=nil)
  @target = target || []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &blk) ⇒ Object

Ugh shoot me for using method missing, couldn’t get it to work any other way.



79
80
81
82
83
# File 'lib/ps/process_list.rb', line 79

def method_missing(*args, &blk)
  new_target = @target.send(*args, &blk)

  new_target.class == @target.class ? self.class.new(new_target) : new_target
end

Instance Method Details

#choose(question = nil) ⇒ Object

Print out all the processes and accept user input to return the process objects selected.



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ps/process_list.rb', line 33

def choose question=nil
  if empty?
    puts "No processes found."
    return self
  end
  question ||= 'Select process(s):'
  self.print
  puts "\n#{question}"
  proc_ids = STDIN.gets.chomp
  proc_ids = '*' if proc_ids.empty?
  proc_ids = proc_ids.split(/[,\s]/)

  if proc_ids.include?('*')
    proc_ids = (0..self.size).to_a
  else
    proc_ids = proc_ids.collect do |proc_id|
      case proc_id
      when /^\d+$/
        proc_id.to_i
      when /^(\d+)(\.{2,3})(\d*)$/
        if $3.empty?
          ($1.to_i..self.size).to_a
        elsif $2.size === 3
          ($1.to_i...$3.to_i).to_a
        else
          ($1.to_i..$3.to_i).to_a
        end
      when /^(\d+)\-(\d+)$/
        ($1.to_i..$2.to_i).to_a
      end
    end.flatten.uniq
  end

  procs = self.class.new

  proc_ids.each do |proc_id|
    proc = @target[proc_id.to_i]

    procs << proc
  end

  procs
end

#command(regex) ⇒ Object



7
8
9
# File 'lib/ps/process_list.rb', line 7

def command regex
  grep(regex) {|proc| proc.command||proc.cmd}
end

#over(val, amnt) ⇒ Object



15
16
17
18
19
# File 'lib/ps/process_list.rb', line 15

def over val, amnt
  find_all do |proc|
    proc.__send__(val) >= amnt
  end
end

#pidsObject



11
12
13
# File 'lib/ps/process_list.rb', line 11

def pids
  @target.collect {|proc| proc.pid}.compact
end


25
26
27
28
29
# File 'lib/ps/process_list.rb', line 25

def print headers=nil, to=nil
  to ||= STDOUT
  printer = Printer.new(self,headers)
  to.send :puts, printer.to_s
end

#to_aObject



21
22
23
# File 'lib/ps/process_list.rb', line 21

def to_a
  @target
end