Sha256: c8be92dada4be1cbbddf26bd9da3d9e1ed583d98ac3cbeb30e88291c88e7e239

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module VER
  class View::List::Grep < View::List
    def initialize(parent, glob = nil, &block)
      super(parent, &block)

      @glob = nil
      @glob = glob.to_s unless glob.nil?
    end

    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 = []

      if @glob
        input, query = @glob, input
      else
        input, query = input.split(/ /, 2)
        input, query = nil, input unless query
        input ||= '*'

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

      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

1 entries across 1 versions & 1 rubygems

Version Path
ver-2009.12.14 lib/ver/view/list/grep.rb