Class: Rush::SearchResults

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Commands
Defined in:
lib/rush/search_results.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#edit, #line_count, #open, #open_command, #open_with, #opt_to_s, #output_of, #replace_contents!, #search

Constructor Details

#initialize(pattern) ⇒ SearchResults

Make a blank container. Track the pattern so that we can colorize the output to show what was matched.



21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



17
18
19
# File 'lib/rush/search_results.rb', line 17

def entries
  @entries
end

#entries_with_linesObject (readonly)

Returns the value of attribute entries_with_lines.



17
18
19
# File 'lib/rush/search_results.rb', line 17

def entries_with_lines
  @entries_with_lines
end

#linesObject (readonly)

Returns the value of attribute lines.



17
18
19
# File 'lib/rush/search_results.rb', line 17

def lines
  @lines
end

#patternObject (readonly)

Returns the value of attribute pattern.



17
18
19
# File 'lib/rush/search_results.rb', line 17

def pattern
  @pattern
end

Instance Method Details

#add(entry, lines) ⇒ Object Also known as: <<

Add a Rush::Entry and the array of string matches.



31
32
33
34
35
36
37
# 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

#colorize(line) ⇒ Object



56
57
58
# File 'lib/rush/search_results.rb', line 56

def colorize(line)
  lowlight + line.gsub(/(#{pattern.source})/, "#{hilight}\\1#{lowlight}") + normal
end

#each(&block) ⇒ Object



52
53
54
# File 'lib/rush/search_results.rb', line 52

def each(&block)
  @entries.each(&block)
end

#hilightObject



60
61
62
# File 'lib/rush/search_results.rb', line 60

def hilight
  "\e[34;1m"
end

#lowlightObject



64
65
66
# File 'lib/rush/search_results.rb', line 64

def lowlight
  "\e[37;2m"
end

#normalObject



68
69
70
# File 'lib/rush/search_results.rb', line 68

def normal
  "\e[0m"
end

#to_sObject



40
41
42
43
44
45
46
47
48
49
50
# 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