Class: Gonzui::CommandLineSearcher

Inherits:
Object
  • Object
show all
Defined in:
lib/gonzui/cmdapp/search.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, options) ⇒ CommandLineSearcher

Returns a new instance of CommandLineSearcher.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gonzui/cmdapp/search.rb', line 14

def initialize(config, options)
  @config = config
  @dbm = DBM.open(@config, true)
  @out = (options['out'] or STDOUT)
  @nlines  = options['line-number']

  @show_method = if options['context']
                   :show_context_lines 
                 elsif options['count']
                    :show_count
                 else
                    :show_line
                 end

  @package_name = options['package']

  @ncontexts = options['context'].to_i

  @search_method = :find_all
  @search_method = :find_all_by_prefix if options['prefix']
  @search_method = :find_all_by_regexp if options['regexp']

  @use_regexp  = options['regexp']
  @use_color   = options['color']
  @no_filename = options['no-filename']

  @target_type = :all
  if options['type']
    type = options['type'].intern
    eprintf("unknown type: #{type}") unless LangScan::Type.include?(type)
    @target_type = type
  end
end

Instance Method Details

#finishObject



130
131
132
# File 'lib/gonzui/cmdapp/search.rb', line 130

def finish
  @dbm.close
end

#search(pattern) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gonzui/cmdapp/search.rb', line 101

def search(pattern)
  separator = ""
  regexp = if @use_regexp
            Regexp.new(pattern)
          else
            Regexp.new(Regexp.quote(pattern))
          end
  results = @dbm.send(@search_method, pattern)
  prev_lineno = prev_path_id = nil
  target_package_id = @dbm.get_package_id(@package_name) if @package_name
  results.sort_by {|x| [x.path_id, x.byteno] }.each {|info|
    next if prev_lineno and prev_path_id and 
      info.path_id == prev_path_id and
      info.lineno == prev_lineno
    if info.match?(@target_type)
      unless @show_method == :show_count
        if @package_name.nil? or package_match?(target_package_id, info)
          @out.print separator
          show_result(regexp, info)
          separator = "\n" if @show_method == :show_context_lines
        end
      end
    end
    prev_lineno = info.lineno
    prev_path_id = info.path_id
  }
  puts results.length if @show_method == :show_count
end