Class: Utils::Grepper

Inherits:
Object show all
Includes:
Term::ANSIColor, Tins::Find, Patterns
Defined in:
lib/utils/grepper.rb

Overview

A class for searching and matching text patterns within files.

This class provides functionality to search through file systems for content matching specified patterns, with support for various output formats and filtering options. It handles directory pruning, file skipping, and different types of pattern matching including regular expressions and fuzzy matching.

Examples:

grepper = Utils::Grepper.new(args: { l: true }, roots: ['.'])
grepper.search

Defined Under Namespace

Classes: Queue

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Patterns

#choose

Constructor Details

#initialize(opts = {}) ⇒ Utils::Grepper

The initialize method sets up the grepper instance with the provided options.

This method configures the grepper by processing the input options, setting up the root directories for searching, initializing the configuration, and preparing pattern matchers for filename and skip patterns. It also handles queue initialization for buffering output when specified.

provided options

Parameters:

  • opts (Hash) (defaults to: {})

    the options hash containing configuration settings

Options Hash (opts):

  • :args (Hash)

    the command-line arguments

  • :roots (Array)

    the root directories to search

  • :config (Utils::ConfigFile)

    the configuration file object

  • :pattern (Hash)

    the pattern-related options



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/utils/grepper.rb', line 80

def initialize(opts = {})
  @args  = opts[:args] || {}
  @roots = discover_roots(opts[:roots])
  @config = opts[:config] || Utils::ConfigFile.new
  if n = @args.values_at(*%w[A B C]).compact.first
    if n.to_s =~ /\A\d+\Z/ and (n = n.to_i) >= 1
      @queue = Queue.new n
    else
      raise ArgumentError, "needs to be an integer number >= 1"
    end
  end
  @paths  = []
  pattern_opts = opts.subhash(:pattern) | {
    :cset  => @args[?a],
    :icase => @args[?i] != ?n,
  }
  @pattern = choose(@args[?p], pattern_opts, default: ?r)
  @name_pattern =
    if name_pattern = @args[?N]
      RegexpPattern.new(:pattern => name_pattern)
    elsif name_pattern = @args[?n]
      FuzzyPattern.new(:pattern => name_pattern)
    end
  @skip_pattern =
    if skip_pattern = @args[?S]
      RegexpPattern.new(:pattern => skip_pattern)
    elsif skip_pattern = @args[?s]
      FuzzyPattern.new(:pattern => skip_pattern)
    end
end

Instance Attribute Details

#pathsArray (readonly)

The paths reader method provides access to the paths instance variable.

Returns:

  • (Array)

    the array of paths stored in the instance variable



114
115
116
# File 'lib/utils/grepper.rb', line 114

def paths
  @paths
end

#patternUtils::Patterns::Pattern (readonly)

The pattern reader method provides access to the pattern matcher object.

This method returns the internal pattern matcher that was initialized during object creation, allowing external code to interact with the pattern matching functionality directly.

matching operations

Returns:



124
125
126
# File 'lib/utils/grepper.rb', line 124

def pattern
  @pattern
end

Instance Method Details

#match(filename) ⇒ Utils::Grepper

The match method processes a file to find matching content based on configured patterns. It handles directory pruning, file skipping, and various output formats depending on the configuration. The method opens files for reading, applies pattern matching, and manages output through different code paths. It supports features like line-based searching, git blame integration, and multiple output modes. The method returns the instance itself to allow for method chaining.

Returns:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/utils/grepper.rb', line 137

def match(filename)
  @filename = filename
  @output = []
  bn, s = File.basename(filename), File.stat(filename)
  if !s || s.directory? && @config.search.prune?(bn)
    @args[?v] and warn "Pruning #{filename.inspect}."
    prune
  end
  if s.file? && !@config.search.skip?(bn) &&
    (!@name_pattern || @name_pattern.match(bn))
  then
    File.open(filename, 'rb', encoding: Encoding::UTF_8) do |file|
      @args[?v] and warn "Matching #{filename.inspect}."
      if @args[?f]
        @output << filename
      else
        match_lines file
      end
    end
  else
    @args[?v] and warn "Skipping #{filename.inspect}."
  end
  unless @output.empty?
    case
    when @args[?g]
      @output.uniq!
      @output.each do |l|
        blamer = LineBlamer.for_line(l)
        if blame = blamer.perform
          author = nil
          blame.sub!(/^[0-9a-f^]+/) { Term::ANSIColor.yellow($&) }
          blame.sub!(/\(([^)]+)\)/) { author = $1; "(#{Term::ANSIColor.red($1)})" }
          if !@args[?G] || author&.downcase&.match?(@args[?G].downcase)
            puts "#{blame.chomp} #{Term::ANSIColor.blue(l)}"
          end
        end
      end
    when @args[?l], @args[?e], @args[?E], @args[?r]
      @output.uniq!
      @paths.concat @output
    else
      STDOUT.puts @output
    end
    @output.clear
  end
  self
end

#match_lines(file) ⇒ Object

The match_lines method processes each line from a file using pattern matching.

This method iterates through lines in the provided file, applying pattern matching to identify relevant content. It handles various output options based on command-line arguments and manages queuing of lines for context display.

Parameters:

  • file (IO)

    the file object to be processed line by line



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/utils/grepper.rb', line 194

def match_lines(file)
  for line in file
    if m = @pattern.match(line)
      @skip_pattern and @skip_pattern =~ line and next
      line[m.begin(0)...m.end(0)] = black on_white m[0]
      @queue and @queue << line
      case
      when @args[?l]
        @output << @filename
      when @args[?L], @args[?r], @args[?g]
        @output << "#{@filename}:#{file.lineno}"
      when @args[?e], @args[?E]
        @output << "#{@filename}:#{file.lineno}"
        break
      else
        @output << red("#{@filename}:#{file.lineno}")
        if @args[?B] or @args[?C]
          @output.concat @queue.data
        else
          @output << line
        end
        if @args[?A] or @args[?C]
          where = file.tell
          lineno = file.lineno
          @queue.max_size.times do
            file.eof? and break
            line = file.readline
            @queue << line
            @output << line
          end
          file.seek where
          file.lineno = lineno
        end
      end
    else
      @queue and @queue << line
    end
  end
end

#searchUtils::Grepper

The search method performs a file search operation within specified roots, filtering results based on various criteria including file extensions, pruning directories, and skipping specific files.

It utilizes a visit lambda to determine whether each file or directory should be processed or skipped based on configuration settings and command-line arguments. The method employs the find utility to traverse the filesystem, executing match operations on qualifying files.

Returns:



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/utils/grepper.rb', line 244

def search
  suffixes = Array(@args[?I])
  visit = -> filename {
    s  = filename.lstat
    bn = filename.pathname.basename
    if !s ||
        s.directory? && @config.search.prune?(bn) ||
        (s.file? || s.symlink?) && @config.search.skip?(bn) ||
        @args[?F] && s.symlink?
    then
      @args[?v] and warn "Pruning #{filename.inspect}."
      prune
    elsif suffixes.empty?
      true
    else
      suffixes.include?(filename.suffix)
    end
  }
  find(*@roots, visit: visit) do |filename|
    match(filename)
  end
  @paths = @paths.sort_by(&:source_location)
  self
end