1
0
Fork 0
mirror of https://github.com/nishiki/swapus.git synced 2024-11-23 04:47:52 +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 'swapus'
reverse = false
options = {
reverse: false
}
OptionParser.new do |opts|
opts.banner = 'Usage: swapus [options]'
@ -31,8 +33,12 @@ OptionParser.new do |opts|
exit 0
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
reverse = true
options[:reverse] = true
end
opts.on('-v', '--version', 'Show the version') do
@ -41,4 +47,4 @@ OptionParser.new do |opts|
end
end.parse!
SwapUsage.new(reverse).run
SwapUsage.new(options).run

View file

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