Sha256: a38752fc658fcd466f75cfacd6977567c82714d0adee5a80806e02839b3434eb

Contents?: true

Size: 971 Bytes

Versions: 2

Compression:

Stored size: 971 Bytes

Contents

module VER
  class View::List::Grep < View::List
    def update
      list.clear

      grep(entry.value).each do |choice|
        list.insert(:end, "#{choice[:match]} -- #{choice[:file]} +#{choice[:line]}")
      end
    end

    def pick_action(entry)
      index = list.curselection.first
      choice = @choices[index]
      callback.call(*choice.values_at(:file, :line))
    end

    def grep(input)
      @choices = []

      input, query = input.split(/ /, 2)
      input, query = nil, input unless query
      input ||= '*'

      return [] if !query || query.size < 3 # protect a little

      regex = /#{Regexp.escape(query)}/

      Dir.glob input do |file|
        next unless ::File.file?(file)

        File.open file do |io|
          io.each_line.with_index do |line, idx|
            next unless line =~ regex
            @choices << {file: file, line: idx, match: line.strip}
          end
        end
      end

      return @choices
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ver-2009.11.29 lib/ver/view/list/grep.rb
ver-2009.11.28 lib/ver/view/list/grep.rb