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

#line_count, #mate, #replace_contents!, #search, #vi

Constructor Details

#initialize(pattern) ⇒ SearchResults

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



18
19
20
21
22
23
24
25
# File 'lib/rush/search_results.rb', line 18

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.



14
15
16
# File 'lib/rush/search_results.rb', line 14

def entries
  @entries
end

#entries_with_linesObject (readonly)

Returns the value of attribute entries_with_lines.



14
15
16
# File 'lib/rush/search_results.rb', line 14

def entries_with_lines
  @entries_with_lines
end

#linesObject (readonly)

Returns the value of attribute lines.



14
15
16
# File 'lib/rush/search_results.rb', line 14

def lines
  @lines
end

#patternObject (readonly)

Returns the value of attribute pattern.



14
15
16
# File 'lib/rush/search_results.rb', line 14

def pattern
  @pattern
end

Instance Method Details

#add(entry, lines) ⇒ Object

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



28
29
30
31
32
33
# File 'lib/rush/search_results.rb', line 28

def add(entry, lines)
	# this assumes that entry is unique
	@entries << entry
	@entries_with_lines[entry] = lines
	@lines += lines
end

#colorize(line) ⇒ Object



43
44
45
# File 'lib/rush/search_results.rb', line 43

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

#each(&block) ⇒ Object



37
38
39
# File 'lib/rush/search_results.rb', line 37

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

#hilightObject



47
48
49
# File 'lib/rush/search_results.rb', line 47

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

#lowlightObject



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

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

#normalObject



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

def normal
	"\e[0m"
end