class Rush::SearchResults
An instance of this class is returned by Rush::Commands#search
. It contains both the list of entries which matched the search, as well as the raw line matches. These methods get equivalent functionality to “grep -l” and “grep -h”.
SearchResults
mixes in Rush::Commands
so that you can chain multiple searches or do file operations on the resulting entries.
Examples:
myproj['**/*.rb'].search(/class/).entries.size myproj['**/*.rb'].search(/class/).lines.size myproj['**/*.rb'].search(/class/).copy_to other_dir
Attributes
entries[R]
entries_with_lines[R]
lines[R]
pattern[R]
Public Class Methods
new(pattern)
click to toggle source
Make a blank container. Track the pattern so that we can colorize the output to show what was matched.
# File lib/rush/search_results.rb, line 21 def initialize(pattern) # Duplication of data, but this lets us return everything in the exact # order it was received. @pattern = pattern @entries = [] @entries_with_lines = {} @lines = [] end
Public Instance Methods
add(entry, lines)
click to toggle source
Add a Rush::Entry
and the array of string matches.
# File lib/rush/search_results.rb, line 31 def add(entry, lines) # this assumes that entry is unique @entries << entry @entries_with_lines[entry] = lines @lines += lines self end
Also aliased as: <<
colorize(line)
click to toggle source
# File lib/rush/search_results.rb, line 56 def colorize(line) lowlight + line.gsub(/(#{pattern.source})/, "#{hilight}\\1#{lowlight}") + normal end
each(&block)
click to toggle source
# File lib/rush/search_results.rb, line 52 def each(&block) @entries.each(&block) end
hilight()
click to toggle source
# File lib/rush/search_results.rb, line 60 def hilight "\e[34;1m" end
lowlight()
click to toggle source
# File lib/rush/search_results.rb, line 64 def lowlight "\e[37;2m" end
normal()
click to toggle source
# File lib/rush/search_results.rb, line 68 def normal "\e[0m" end
to_s()
click to toggle source
# File lib/rush/search_results.rb, line 40 def to_s widest = entries.map { |k| k.full_path.length }.max entries_with_lines.inject('') do |result, (entry, lines)| result << entry.full_path result << ' ' * (widest - entry.full_path.length + 2) result << "=> " result << colorize(lines.first.strip.head(30)) lines.each { |line| result << "\t" << line << "\n" } result << "\n" end end