1
0
Fork 0
mirror of https://github.com/nishiki/swapus.git synced 2024-11-26 22:13:04 +00:00

feat: add option to print X lines

This commit is contained in:
Adrien Waksberg 2018-02-02 14:01:25 +01:00
parent cbdda61f3e
commit ac584cf9b8
2 changed files with 13 additions and 5 deletions

View file

@ -21,7 +21,9 @@ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
require 'optparse' require 'optparse'
require 'swapus' require 'swapus'
reverse = false options = {
reverse: false
}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = 'Usage: swapus [options]' opts.banner = 'Usage: swapus [options]'
@ -31,8 +33,12 @@ OptionParser.new do |opts|
exit 0 exit 0
end end
opts.on('-n', '--number N', 'Print the N first lines') do |number|
options[:number] = number.to_i
end
opts.on('-r', '--reverse', 'Reverse the result order') do opts.on('-r', '--reverse', 'Reverse the result order') do
reverse = true options[:reverse] = true
end end
opts.on('-v', '--version', 'Show the version') do opts.on('-v', '--version', 'Show the version') do
@ -41,4 +47,4 @@ OptionParser.new do |opts|
end end
end.parse! end.parse!
SwapUsage.new(reverse).run SwapUsage.new(options).run

View file

@ -17,9 +17,10 @@
# under the License. # under the License.
class SwapUsage class SwapUsage
def initialize(reverse) def initialize(options)
@processes = {} @processes = {}
@reverse = reverse @reverse = options[:reverse]
@number = options[:number]
end end
def get_processes def get_processes
@ -35,6 +36,7 @@ class SwapUsage
def show def show
@processes = @processes.sort_by { |_, v| v[:swap] } @processes = @processes.sort_by { |_, v| v[:swap] }
@processes = @processes.reverse if @reverse @processes = @processes.reverse if @reverse
@processes = @processes.first(@number) if @number
@processes.each do |pid, values| @processes.each do |pid, values|
puts "#{values[:swap]} kB [#{pid}] #{values[:cmd]}" puts "#{values[:swap]} kB [#{pid}] #{values[:cmd]}"