Class: Utils::Grepper
- Includes:
- Term::ANSIColor, Tins::Find, Patterns
- Defined in:
- lib/utils/grepper.rb
Defined Under Namespace
Classes: Queue
Instance Attribute Summary collapse
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Grepper
constructor
A new instance of Grepper.
- #match(filename) ⇒ Object
- #match_lines(file) ⇒ Object
- #search ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Grepper
Returns a new instance of Grepper.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/utils/grepper.rb', line 32 def initialize(opts = {}) @args = opts[:args] || {} @roots = opts[:roots] || [] @config = opts[:config] || Utils::Config::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'], } @pattern = @args['R'] ? FuzzyPattern.new(pattern_opts) : RegexpPattern.new(pattern_opts) @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
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
65 66 67 |
# File 'lib/utils/grepper.rb', line 65 def paths @paths end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
67 68 69 |
# File 'lib/utils/grepper.rb', line 67 def pattern @pattern end |
Instance Method Details
#match(filename) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 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 |
# File 'lib/utils/grepper.rb', line 69 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') do |file| if file.binary? != true @args['v'] and warn "Matching #{filename.inspect}." if @args['f'] @output << filename else match_lines file end else @args['v'] and warn "Skipping binary file #{filename.inspect}." end end else @args['v'] and warn "Skipping #{filename.inspect}." end unless @output.empty? case when @args['l'], @args['e'] @output.uniq! @paths.concat @output else STDOUT.puts @output end @output.clear end self end |
#match_lines(file) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/utils/grepper.rb', line 108 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 if @args['l'] @output << @filename elsif @args['L'] @output << "#{@filename}:#{file.lineno}" elsif @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 |
#search ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/utils/grepper.rb', line 147 def search suffixes = @args['I'].ask_and_send(:split, /[\s,]+/).to_a find(*(@roots + [ { :suffix => suffixes } ])) do |filename| match(filename) end if @args['L'] or @args['e'] @paths = @paths.sort_by do |path| pair = path.split(':') pair[1] = pair[1].to_i pair end else @paths.sort! end self end |