Class: Utils::Finder
- Includes:
- Term::ANSIColor, Tins::Find, Patterns
- Defined in:
- lib/utils/finder.rb
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
- #build_paths ⇒ Object
- #create_paths ⇒ Object
- #index_path ⇒ Object
-
#initialize(opts = {}) ⇒ Finder
constructor
A new instance of Finder.
- #reset_index ⇒ Object
- #search ⇒ Object
- #search_directly ⇒ Object
- #search_index ⇒ Object
- #search_paths(paths) ⇒ Object
Methods included from Patterns
Constructor Details
#initialize(opts = {}) ⇒ Finder
Returns a new instance of Finder.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/utils/finder.rb', line 17 def initialize(opts = {}) @args = opts[:args] || {} @roots = discover_roots(opts[:roots]) @config = opts[:config] || Utils::ConfigFile.new if @args[?l] || @args[?L] @pattern = nil else pattern_opts = opts.subhash(:pattern) | { :cset => @args[?a], :icase => @args[?i] != ?n, } @pattern = choose(@args[?p], pattern_opts) end @paths = [] reset_index end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
36 37 38 |
# File 'lib/utils/finder.rb', line 36 def output @output end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
34 35 36 |
# File 'lib/utils/finder.rb', line 34 def paths @paths end |
Instance Method Details
#build_paths ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/utils/finder.rb', line 45 def build_paths paths = [] visit = -> filename { s = filename.stat bn = filename.pathname.basename if !s || s.directory? && @config.discover.prune?(bn) || s.file? && @config.discover.skip?(bn) then @args[?v] and warn "Pruning #{filename.inspect}." prune end true } find(*@roots, visit: visit) do |filename| filename.stat.directory? and filename << ?/ paths << filename end paths.uniq! paths end |
#create_paths ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/utils/finder.rb', line 76 def create_paths paths = build_paths File.secure_write(index_path) do |output| output.puts paths end paths end |
#index_path ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/utils/finder.rb', line 67 def index_path roots = @roots.map { |r| File.(r) }.uniq.sort filename = "finder-paths-" + Digest::MD5.new.update(roots.inspect).hexdigest dirname = File.join(Dir.tmpdir, File.basename($0)) FileUtils.mkdir_p dirname File.join(dirname, filename) end |
#reset_index ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/utils/finder.rb', line 95 def reset_index path = index_path if @args[?r] || index_expired?(path) @args[?v] and warn "Resetting index #{path.inspect}." FileUtils.rm_f path mize_cache_clear end self end |
#search ⇒ Object
43 44 45 46 |
# File 'lib/utils/finder.rb', line 43 def search_index paths = load_paths search_paths(paths) end |
#search_directly ⇒ Object
109 110 111 |
# File 'lib/utils/finder.rb', line 109 def search_directly search_paths build_paths end |
#search_index ⇒ Object
38 39 40 41 |
# File 'lib/utils/finder.rb', line 38 def search_index paths = load_paths search_paths(paths) end |
#search_paths(paths) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/utils/finder.rb', line 113 def search_paths(paths) suffixes = Array(@args[?I]) suffixes.full? do |s| paths.select! { |path| s.include?(File.extname(path)[1..-1]) } end paths = paths.map! do |path| if @pattern.nil? [ [ path.count(?/), path ], path, path ] elsif match = @pattern.match(path) if FuzzyPattern === @pattern current = 0 marked_path = '' score, e = path.size, nil for i in 1...match.size match[i] or next b = match.begin(i) e ||= b marked_path << path[current...b] marked_path << red(path[b, 1]) score += (b - e) * (path.size - b) e = match.end(i) current = b + 1 end marked_path << match.post_match [ score, path, marked_path ] else marked_path = path[0...match.begin(0)] << red(path[match.begin(0)...match.end(0)]) << path[match.end(0)..-1] [ 0, path, marked_path ] end end end paths.compact! @paths, @output = paths.sort.transpose.values_at(-2, -1) if n = @args[?n]&.to_i @paths = @paths&.first(n) || [] @output = @output&.first(n) || [] end self end |