Class: Grepfruit::Search

Inherits:
Object
  • Object
show all
Includes:
Decorator
Defined in:
lib/grepfruit/search.rb

Constant Summary

Constants included from Decorator

Decorator::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir:, regex:, exclude:, include:, truncate:, search_hidden:, jobs:, json_output: false) ⇒ Search

Returns a new instance of Search.



12
13
14
15
16
17
18
19
20
21
# File 'lib/grepfruit/search.rb', line 12

def initialize(dir:, regex:, exclude:, include:, truncate:, search_hidden:, jobs:, json_output: false)
  @dir = File.expand_path(dir)
  @regex = regex
  @excluded_lines, @excluded_paths = exclude.map { _1.split("/") }.partition { _1.last.include?(":") }
  @included_paths = include.map { _1.split("/") }
  @truncate = truncate
  @search_hidden = search_hidden
  @jobs = jobs || Etc.nprocessors
  @json_output = json_output
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def dir
  @dir
end

#excluded_linesObject (readonly)

Returns the value of attribute excluded_lines.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def excluded_lines
  @excluded_lines
end

#excluded_pathsObject (readonly)

Returns the value of attribute excluded_paths.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def excluded_paths
  @excluded_paths
end

#included_pathsObject (readonly)

Returns the value of attribute included_paths.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def included_paths
  @included_paths
end

#jobsObject (readonly)

Returns the value of attribute jobs.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def jobs
  @jobs
end

#json_outputObject (readonly)

Returns the value of attribute json_output.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def json_output
  @json_output
end

#regexObject (readonly)

Returns the value of attribute regex.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def regex
  @regex
end

#search_hiddenObject (readonly)

Returns the value of attribute search_hidden.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def search_hidden
  @search_hidden
end

#truncateObject (readonly)

Returns the value of attribute truncate.



10
11
12
# File 'lib/grepfruit/search.rb', line 10

def truncate
  @truncate
end

Instance Method Details

#runObject



23
24
25
26
27
# File 'lib/grepfruit/search.rb', line 23

def run
  puts "Searching for #{regex.inspect} in #{dir.inspect}...\n\n" unless json_output

  display_final_results(execute_search)
end