Class: VER::View::List::Grep

Inherits:
VER::View::List show all
Defined in:
lib/ver/view/list/grep.rb

Instance Attribute Summary

Attributes inherited from VER::View::List

#callback, #entry, #frame, #list, #parent, #tag

Instance Method Summary collapse

Methods inherited from VER::View::List

#cancel, #completion, #destroy, #initialize, #line_down, #line_up, #message, #on_update, #pick, #pick_first, #pick_selection, #quit, #select_index, #setup_events, #setup_keymap, #setup_widgets, #sublist

Constructor Details

This class inherits a constructor from VER::View::List

Instance Method Details

#grep(input) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ver/view/list/grep.rb', line 17

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

#pick_action(entry) ⇒ Object



11
12
13
14
15
# File 'lib/ver/view/list/grep.rb', line 11

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

#updateObject



3
4
5
6
7
8
9
# File 'lib/ver/view/list/grep.rb', line 3

def update
  list.clear

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