Class: Grepper::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/grepper/formatter.rb

Overview

Description

Grepper::Formatter provides output similar to GNU grep (and probably other greps.

Usage

f = Grepper::Formatter.initialize(grepper)
f.format
f.options = ['...'] # see below
puts f.output

Output will show filenames if there are multiple files, and will show before- and after-context. The Formatter doesn’t really do anything except format the information that the Grepper and its result set already have.

For version and license information, see grepper.rb.

Options

Formatters have an options array which you can set and add to. It’s empty by default.

Available options:

  • l – show matching filenames only

  • c – show match count only

  • n – show the line number of the match

  • H – show the name of the file of each match

  • h – don’t show the names of any files

Note that Grepper objects also have options (‘v’, ‘A1’, etc.). Both the Formatter options and the Grepper options are pulled from the canon of grep(1) options. Some make more sense for the grep operation itself (like inverting the sense of the match with ‘v’), while some are purely a formatting thing (like including the filename).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grepper) ⇒ Formatter

Returns a new instance of Formatter.



50
51
52
53
54
# File 'lib/grepper/formatter.rb', line 50

def initialize(grepper)
  @output = ""
  @grepper = grepper
  @options = []
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



48
49
50
# File 'lib/grepper/formatter.rb', line 48

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



47
48
49
# File 'lib/grepper/formatter.rb', line 47

def output
  @output
end

Instance Method Details

#formatObject



64
65
66
# File 'lib/grepper/formatter.rb', line 64

def format
  @output = process_grepper(@grepper)
end

#matches_are_contiguous?(first, second) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/grepper/formatter.rb', line 56

def matches_are_contiguous?(first,second)
  first.absolute_end == second.absolute_start - 1
end

#multi?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/grepper/formatter.rb', line 60

def multi?
  @grepper.results.size > 1 &&! @options.include?('h')
end

#process_grepper(grepper) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/grepper/formatter.rb', line 114

def process_grepper(grepper)
  str = ""
  last_file = nil
  context = grepper.results.context?

  grepper.results.each do |file,matches|

# Put in a -- boundary if this is a new file and
# there's a before- or after-context
    if context && last_file &&! (file == last_file)
      str << "--\n"
    end

    last_file = file

# 'l' -- filenames only
    if options.include?('l')
      str << "#{file}\n" unless matches.empty?
      
# 'L' -- filenames only, for non-matching files
    elsif options.include?('L')
      str << "#{file}\n" if matches.empty?

# 'c' -- match count only
    elsif options.include?('c')
      str << one_or_many("#{matches.size}\n", "#{file}:")
    else
      matches.each_with_index do |(n,before,line,after),m|

        if before
          prev = matches[m-1]
          str << boundary_string_for(prev, matches[m], :conditions => prev &&! m.zero?)
          str << before_lines(before,file,n)
        end

        str << one_or_many(format_line(n,before,line,after), "#{file}:")

        if after
          str << after_lines(after,file,n)
          succ = matches[m+1]
          str << boundary_string_for(matches[m], succ, :conditions => succ)
        end  
      end
    end
  end
  return str
end