Sha256: 6c81bb4edc5bf7ccbd3f9a51381ac128b90851bf162e3b80d4bb9f04ea9e357a

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

module Vimpack
  module Commands
    class Search < Command

      def initialize_options
        @conditions = Vimpack::Models::Script::SCRIPT_TYPES.map.inject([]) do |conditions, script_type|
          conditions << script_type if @options[script_type.gsub(' ', '_').to_sym]
          conditions
        end
      end

      def initialize_commands
        die!("search requires a single argument") unless (@commands.size == 1 || !@conditions.empty?)
        @pattern = @commands[0] if @commands.size >= 1
      end

      def run
        scripts = Vimpack::Models::Script.search(@pattern, @conditions)
        return exit_with_error!('No scripts found!', 0) if scripts.empty?
        say_justified_script_names(scripts)
      end

      private

        def say_justified_script_names(scripts)
          linesize = scripts.sort do |a,b|
            a.name.size <=> b.name.size
          end.reverse.first.name.size + 1
          scripts.each do |script|
            say("#{script.name.ljust(linesize)} #{script.type}")
          end
        end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vimpack-0.0.4 lib/vimpack/commands/search.rb
vimpack-0.0.3 lib/vimpack/commands/search.rb
vimpack-0.0.2 lib/vimpack/commands/search.rb
vimpack-0.0.1.1 lib/vimpack/commands/search.rb
vimpack-0.0.1 lib/vimpack/commands/search.rb