lib/rubygems/commands/grep_command.rb in gem_grep-0.1.1 vs lib/rubygems/commands/grep_command.rb in gem_grep-0.1.2

- old
+ new

@@ -1,30 +1,22 @@ require 'rubygems/commands/query_command' require 'rubygems/super_search' require 'hirb' +require 'gem_grep' class Gem::Commands::GrepCommand < Gem::Commands::QueryCommand - class<<self - def valid_gemspec_columns - @valid_gemspec_columns ||= Gem::Specification.attribute_names.map {|e| e.to_s}.sort - end - end - + attr_accessor :results, :results_only def initialize super 'grep', "Enhances search command by providing extra search options and displaying results as a table" defaults.merge!(:columns=>[:name,:summary,:authors]) add_option('-c', '--columns STRING', 'Gemspec columns/attributes to display per gem') do |value, options| - options[:columns] = value.split(/\s*,\s*/).map {|e| - self.class.valid_gemspec_columns.detect {|c| c =~ /^#{e}/ } - }.compact.map {|e| e.to_sym} + options[:columns] = GemGrep.parse_input(value).map {|e| e.to_sym} end add_option('-f', '--fields STRING', 'Gemspec fields/attributes to search (only for local gems)') do |value, options| - options[:fields] = value.split(/\s*,\s*/).map {|e| - self.class.valid_gemspec_columns.detect {|c| c =~ /^#{e}/ } - }.compact + GemGrep.grep_fields = options[:fields] = GemGrep.parse_input(value) end remove_option '--name-matches' remove_option '-d' remove_option '--versions' end @@ -43,28 +35,23 @@ def description # :nodoc: 'Enhances search command by providing options to search (--fields) and display (--columns) ' + 'gemspec attributes. Results are displayed in an ascii table. Gemspec attributes can be specified '+ 'by the first unique string that it starts with i.e. "su" for "summary". To specify multiple gemspec attributes, delimit ' + - "them with commas. Gemspec attributes available to options are: #{self.class.valid_gemspec_columns.join(', ')}." + "them with commas. Gemspec attributes available to options are: #{GemGrep.valid_gemspec_columns.join(', ')}." end def execute string = get_one_optional_argument options[:name] = /#{string}/i Gem.source_index.extend Gem::SuperSearch super end def output_query_results(tuples) - tuples = cleanup_tuples(tuples) - records = tuples.map {|e| - options[:columns].inject({:name=>e[0][0]}) {|h,c| - val = e[0][-1].send(c) - h[c] = val.is_a?(Array) ? val.join(',') : val; h - } - } - say Hirb::Helpers::Table.render(records, :fields=>options[:columns]) + @results = cleanup_tuples(tuples).map {|e| e[0][-1]} + @results_only ? @results : + say(Hirb::Helpers::Table.render(@results, :fields=>options[:columns])) end # borrowed from query command def cleanup_tuples(spec_tuples) versions = Hash.new { |h,name| h[name] = [] } \ No newline at end of file