ps 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ps.rb +2 -1
- data/lib/ps/process_list.rb +57 -3
- data/lib/ps/process_list_printer.rb +118 -0
- data/lib/ps/version.rb +1 -1
- data/ps.gemspec +1 -0
- metadata +19 -4
data/lib/ps.rb
CHANGED
@@ -64,13 +64,14 @@ end
|
|
64
64
|
require 'ps/command'
|
65
65
|
require 'ps/process'
|
66
66
|
require 'ps/process_list'
|
67
|
+
require 'ps/process_list_printer'
|
67
68
|
|
68
69
|
def PS *args
|
69
70
|
case args[0]
|
70
71
|
when Regexp
|
71
72
|
opts = args[1] || {}
|
72
73
|
procs = PS.all(opts)
|
73
|
-
procs = procs.command
|
74
|
+
procs = procs.select {|proc| proc.command =~ args[0]}
|
74
75
|
procs = procs.select {|proc| proc.pid != Process.pid} unless opts[:include_self]
|
75
76
|
procs
|
76
77
|
when Integer
|
data/lib/ps/process_list.rb
CHANGED
@@ -13,15 +13,69 @@ module PS
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def over val, amnt
|
16
|
-
|
16
|
+
find_all do |proc|
|
17
17
|
proc.__send__(val) >= amnt
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
def to_a
|
22
|
+
@target
|
23
|
+
end
|
24
|
+
|
25
|
+
def print headers=nil, to=nil
|
26
|
+
to ||= STDOUT
|
27
|
+
printer = Printer.new(self,headers)
|
28
|
+
to.send :puts, printer.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def choose question=nil
|
32
|
+
if empty?
|
33
|
+
puts "No processes found."
|
34
|
+
return self
|
35
|
+
end
|
36
|
+
question ||= 'Select process(s):'
|
37
|
+
self.print
|
38
|
+
puts "\n#{question}"
|
39
|
+
proc_ids = STDIN.gets.chomp
|
40
|
+
proc_ids = '*' if proc_ids.empty?
|
41
|
+
proc_ids = proc_ids.split(/[,\s]/)
|
42
|
+
|
43
|
+
if proc_ids.include?('*')
|
44
|
+
proc_ids = (0..self.size).to_a
|
45
|
+
else
|
46
|
+
proc_ids = proc_ids.collect do |proc_id|
|
47
|
+
case proc_id
|
48
|
+
when /^\d+$/
|
49
|
+
proc_id.to_i
|
50
|
+
when /^(\d+)(\.{2,3})(\d*)$/
|
51
|
+
if $3.empty?
|
52
|
+
($1.to_i..self.size).to_a
|
53
|
+
elsif $2.size === 3
|
54
|
+
($1.to_i...$3.to_i).to_a
|
55
|
+
else
|
56
|
+
($1.to_i..$3.to_i).to_a
|
57
|
+
end
|
58
|
+
when /^(\d+)\-(\d+)$/
|
59
|
+
($1.to_i..$2.to_i).to_a
|
60
|
+
end
|
61
|
+
end.flatten.uniq
|
62
|
+
end
|
63
|
+
|
64
|
+
procs = self.class.new
|
65
|
+
|
66
|
+
proc_ids.each do |proc_id|
|
67
|
+
proc = @target[proc_id.to_i]
|
68
|
+
|
69
|
+
procs << proc
|
70
|
+
end
|
71
|
+
|
72
|
+
procs
|
73
|
+
end
|
74
|
+
|
21
75
|
# Ugh shoot me for using method missing, couldn't get
|
22
76
|
# it to work any other way.
|
23
|
-
def method_missing(
|
24
|
-
new_target = @target.send(
|
77
|
+
def method_missing(*args, &blk)
|
78
|
+
new_target = @target.send(*args, &blk)
|
25
79
|
|
26
80
|
new_target.class == @target.class ? self.class.new(new_target) : new_target
|
27
81
|
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'ansi/core'
|
2
|
+
require 'ansi/terminal'
|
3
|
+
require 'ansi/string'
|
4
|
+
|
5
|
+
module PS
|
6
|
+
class ProcessList
|
7
|
+
class Printer
|
8
|
+
|
9
|
+
def initialize pl, headers=nil
|
10
|
+
@headers = headers || %w{# pid ppid user pcpu mem command}
|
11
|
+
@process_list = pl
|
12
|
+
end
|
13
|
+
|
14
|
+
def data
|
15
|
+
data = []
|
16
|
+
|
17
|
+
data << header_cells = @headers.collect do |header|
|
18
|
+
{:val => header, :header => header}
|
19
|
+
end
|
20
|
+
|
21
|
+
@process_list.to_a.each_with_index do |proc,i|
|
22
|
+
data << @headers.collect do |header|
|
23
|
+
val = header == '#' ? i : proc.send(header)
|
24
|
+
|
25
|
+
cell = {:val => val, :header => header}
|
26
|
+
|
27
|
+
case header
|
28
|
+
when 'pid'
|
29
|
+
cell[:format] = [:blue]
|
30
|
+
when '#'
|
31
|
+
cell[:format] = [:yellow]
|
32
|
+
cell[:align] = :right
|
33
|
+
when 'user','ruser'
|
34
|
+
cell[:align] = :center
|
35
|
+
when 'mem'
|
36
|
+
cell[:val] = val.round.to_s + 'mb'
|
37
|
+
cell[:align] = :right
|
38
|
+
when 'pcpu', 'pmem'
|
39
|
+
cell[:align] = :right
|
40
|
+
cell[:format] = case cell[:val]
|
41
|
+
when 0.0...25.0
|
42
|
+
[]
|
43
|
+
when 25.0..75.0
|
44
|
+
[:red]
|
45
|
+
else
|
46
|
+
[:white,:on_red]
|
47
|
+
end
|
48
|
+
|
49
|
+
cell[:val] = (cell[:val]).to_i.to_s+'%'
|
50
|
+
end
|
51
|
+
|
52
|
+
cell
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
data
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_s
|
60
|
+
printgrid(data)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def printgrid(table)
|
66
|
+
strs = []
|
67
|
+
strings = table.map do |row|
|
68
|
+
row.map { |cell| cell[:val].to_s }
|
69
|
+
end
|
70
|
+
|
71
|
+
column_widths = []
|
72
|
+
strings.each do |row|
|
73
|
+
row.each_with_index do |cell,i|
|
74
|
+
column_widths[i] = [column_widths[i] || 0, cell.length].max
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
row_separator = column_widths.map { |width| "-" * (width+2) }.join("+")[0...ANSI::Terminal.terminal_width]
|
79
|
+
|
80
|
+
# strs << row_separator
|
81
|
+
table.each_with_index do |row,ri|
|
82
|
+
diff = 0
|
83
|
+
justified_row = row.enum_for(:map).with_index do |cell,ci|
|
84
|
+
val = cell[:val].to_s
|
85
|
+
|
86
|
+
str = " "
|
87
|
+
|
88
|
+
padded_val = case cell[:align]
|
89
|
+
when :right
|
90
|
+
val.rjust(column_widths[ci])
|
91
|
+
when :center
|
92
|
+
val.center(column_widths[ci])
|
93
|
+
else
|
94
|
+
val.ljust(column_widths[ci])
|
95
|
+
end
|
96
|
+
|
97
|
+
# padded_val.sub!(val,val.ansi(*cell[:format])) if cell[:format] && !cell[:format].empty?
|
98
|
+
if cell[:format] && !cell[:format].empty?
|
99
|
+
orig_len = padded_val.length
|
100
|
+
padded_val.ansi!(*cell[:format])
|
101
|
+
diff += padded_val.length - orig_len
|
102
|
+
end
|
103
|
+
|
104
|
+
str << padded_val
|
105
|
+
|
106
|
+
str << " "
|
107
|
+
str
|
108
|
+
end
|
109
|
+
strs << justified_row.join("|")[0...ANSI::Terminal.terminal_width+diff]
|
110
|
+
strs << row_separator if ri == 0
|
111
|
+
end
|
112
|
+
|
113
|
+
strs.join("\n")
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
data/lib/ps/version.rb
CHANGED
data/ps.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tal Atlas
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-30 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|
@@ -31,6 +31,20 @@ dependencies:
|
|
31
31
|
version: "0"
|
32
32
|
type: :development
|
33
33
|
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: ansi
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
34
48
|
description: A ruby utility for interacting with the unix tool 'ps'
|
35
49
|
email:
|
36
50
|
- me@tal.by
|
@@ -50,6 +64,7 @@ files:
|
|
50
64
|
- lib/ps/command.rb
|
51
65
|
- lib/ps/process.rb
|
52
66
|
- lib/ps/process_list.rb
|
67
|
+
- lib/ps/process_list_printer.rb
|
53
68
|
- lib/ps/version.rb
|
54
69
|
- ps.gemspec
|
55
70
|
- spec/command_spec.rb
|