pkuse 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env ruby
  2. SHOULD_SHOW_ALL = (ARGV.include? '-a') || (ARGV.include? '--all' )
  3. SHOULD_FILTER_NONE = (ARGV.include? '-d') || (ARGV.include? '--deinstalled')
  4. N_RESULTS = 12
  5. QUERY_CMD = "dpkg-query -W --showformat='${Installed-Size} ${Package} ${Status}\n'"
  6. SORT_ASC_CMD = "sort -n"
  7. SORT_DES_CMD = "sort -nr"
  8. SORT_CMD = SHOULD_SHOW_ALL ? SORT_ASC_CMD : SORT_DES_CMD
  9. # parse indexed packages
  10. indexed_packages = (`#{QUERY_CMD} | #{SORT_CMD}`.split "\n").map do | ea |
  11. tokens = ea.split ' ' ;
  12. { :size => tokens.shift , :name => tokens.shift , :status => tokens.pop }
  13. end
  14. # filter installed packages
  15. n_packages = 0
  16. longest_installed_name = 0
  17. installed_packages = indexed_packages.select do | ea |
  18. if SHOULD_SHOW_ALL || n_packages < N_RESULTS
  19. name_length = ea[:name].size
  20. longest_installed_name = name_length if name_length > longest_installed_name
  21. (SHOULD_FILTER_NONE || ea[:status] == 'installed') && n_packages += 1
  22. else false
  23. end
  24. end
  25. # display installed packages
  26. print "#{sprintf "| %-9s | %-#{longest_installed_name}s |" , "Size (kB)" , "Package Name"}\n"
  27. print "|-----------+-" ; longest_installed_name.times { print '-' } ; print "-|\n"
  28. installed_packages.each do | ea |
  29. print "#{sprintf "| %-9d | %-#{longest_installed_name}s |" , ea[:size] , ea[:name]}\n"
  30. end