Class: Grepfruit::Search
- Inherits:
-
Object
- Object
- Grepfruit::Search
- Defined in:
- lib/search.rb
Constant Summary collapse
- CYAN =
"\e[36m"
- RED =
"\e[31m"
- GREEN =
"\e[32m"
- RESET =
"\e[0m"
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#excluded_lines ⇒ Object
readonly
Returns the value of attribute excluded_lines.
-
#excluded_paths ⇒ Object
readonly
Returns the value of attribute excluded_paths.
-
#regex ⇒ Object
readonly
Returns the value of attribute regex.
-
#search_hidden ⇒ Object
readonly
Returns the value of attribute search_hidden.
-
#truncate ⇒ Object
readonly
Returns the value of attribute truncate.
Instance Method Summary collapse
-
#initialize(dir:, regex:, exclude:, truncate:, search_hidden:) ⇒ Search
constructor
A new instance of Search.
- #run ⇒ Object
Constructor Details
#initialize(dir:, regex:, exclude:, truncate:, search_hidden:) ⇒ Search
Returns a new instance of Search.
13 14 15 16 17 18 19 |
# File 'lib/search.rb', line 13 def initialize(dir:, regex:, exclude:, truncate:, search_hidden:) @dir = dir @regex = regex @excluded_lines, @excluded_paths = exclude.map { |e| e.split("/") }.partition { |e| e.last.include?(":") } @truncate = truncate @search_hidden = search_hidden end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
11 12 13 |
# File 'lib/search.rb', line 11 def dir @dir end |
#excluded_lines ⇒ Object (readonly)
Returns the value of attribute excluded_lines.
11 12 13 |
# File 'lib/search.rb', line 11 def excluded_lines @excluded_lines end |
#excluded_paths ⇒ Object (readonly)
Returns the value of attribute excluded_paths.
11 12 13 |
# File 'lib/search.rb', line 11 def excluded_paths @excluded_paths end |
#regex ⇒ Object (readonly)
Returns the value of attribute regex.
11 12 13 |
# File 'lib/search.rb', line 11 def regex @regex end |
#search_hidden ⇒ Object (readonly)
Returns the value of attribute search_hidden.
11 12 13 |
# File 'lib/search.rb', line 11 def search_hidden @search_hidden end |
#truncate ⇒ Object (readonly)
Returns the value of attribute truncate.
11 12 13 |
# File 'lib/search.rb', line 11 def truncate @truncate end |
Instance Method Details
#run ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 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 |
# File 'lib/search.rb', line 21 def run lines, files = [], 0 puts "Searching for #{regex.inspect} in #{dir.inspect}...\n\n" Find.find(dir) do |path| Find.prune if excluded_path?(path) || !search_hidden && hidden?(path) next if File.directory?(path) || File.symlink?(path) files += 1 match = false File.foreach(path).with_index do |line, line_num| next unless line.valid_encoding? if line.match?(regex) next if excluded_line?(path, line_num) lines << "#{CYAN}#{relative_path_with_line_num(path, line_num)}#{RESET}: #{processed_line(line)}" match = true end end print match ? "#{RED}M#{RESET}" : "#{GREEN}.#{RESET}" end puts "\n\n" if files.positive? if lines.empty? puts "#{number_of_files(files)} checked, #{GREEN}no matches found#{RESET}" exit(0) else puts "Matches:\n\n" puts "#{lines.join("\n")}\n\n" puts "#{number_of_files(files)} checked, #{RED}#{number_of_matches(lines.size)} found#{RESET}" exit(1) end end |