Class: FileFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/file_finder.rb

Overview

ruby -e “$:.unshift File.dirname(__FILE__); require ‘stridx’; idx = CppStringIndex.new(2); idx.add(‘foobar00’,3); idx.add(‘fo0br’,5); pp idx.find(‘foo’);”

Constant Summary collapse

@@idx_updating =
true
@@dir_list =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileFinder

Returns a new instance of FileFinder.



40
41
42
43
44
45
# File 'lib/vimamsa/file_finder.rb', line 40

def initialize()
  vma.hook.register(:shutdown, self.method("save"))
  @@dir_list = vma.marshal_load("file_index")
  @@dir_list ||= []
  update_search_idx
end

Class Method Details

.recursively_find_filesObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/vimamsa/file_finder.rb', line 131

def self.recursively_find_files()
  debug("START find files")
  dlist = []

  for d in cnf.search_dirs!
    debug("FIND FILEs IN #{d}")
    dlist = dlist + Dir.glob("#{d}/**/*").select { |e| File.file?(e) and cnf.find_extensions!.include?(File.extname(e)) }
    debug("FIND FILEs IN #{d} END")
  end
  @@dir_list = dlist
  update_search_idx
  debug("END find files")
  return @@dir_list
end

.update_indexObject



33
34
35
36
37
38
# File 'lib/vimamsa/file_finder.rb', line 33

def self.update_index()
  message("Start updating file index")
  Thread.new {
    recursively_find_files()
  }
end

.update_search_idxObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vimamsa/file_finder.rb', line 47

def self.update_search_idx
  Thread.new {
    sleep 0.1
    @@idx = StringIndex.new
    @@idx_updating = true

    aa = []; @@dir_list.each_with_index { |x, i| aa << [x, i] }

    # Parallel.map(aa, in_threads: 8) do |(x, i)|
    # @@idx.add(x, i)
    # end

    i = 0
    for x in @@dir_list
      i += 1
      # str_idx_addToIndex(x, i)
      @@idx.add(x, i)
    end
    @@idx_updating = false
    message("Finish updating file index")
  }
end

Instance Method Details

#filter_files(search_str) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/vimamsa/file_finder.rb', line 146

def filter_files(search_str)
  puts "search list: #{@@dir_list.size}"
  dir_hash = {}

  res = @@idx.find(search_str)
  resultarr = []
  for (idx, score) in res
    fn = @@dir_list[idx - 1]
    puts "#{idx} #{score} #{fn}"
    resultarr << [fn, score]
  end
  return resultarr

  # Ripl.start :binding => binding

  scores = Parallel.map(@@dir_list, in_threads: 8) do |file|
    [file, srn_dst(search_str, file)]
  end
  for s in scores
    dir_hash[s[0]] = s[1] if s[1] > 0
  end
  # debug scores
  dir_hash = dir_hash.sort_by { |k, v| -v }
  dir_hash = dir_hash[0..20]
  dir_hash.map do |file, d|
    debug "D:#{d} #{file}"
  end
  return dir_hash
end

#gui_file_finder_select_callback(search_str, idx) ⇒ Object



124
125
126
127
128
129
# File 'lib/vimamsa/file_finder.rb', line 124

def gui_file_finder_select_callback(search_str, idx)
  selected_file = @file_search_list[idx][0]
  debug "FILE FINDER SELECT CALLBACK: s=#{search_str},i=#{idx}: #{selected_file}"
  gui_select_window_close(0)
  open_new_file(selected_file)
end

#gui_file_finder_update_callback(search_str = "") ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vimamsa/file_finder.rb', line 109

def gui_file_finder_update_callback(search_str = "")
  debug "FILE FINDER UPDATE CALLBACK: #{search_str}"
  if (search_str.size >= 3)
    files = filter_files(search_str)
    @file_search_list = files
    if files.size > 1
      files = files.collect{|x|[tilde_path(x[0])]}
    end

    return files
    #return files.values
  end
  return []
end

#saveObject



78
79
80
81
# File 'lib/vimamsa/file_finder.rb', line 78

def save()
  debug "SAVE FILE INDEX", 2
  vma.marshal_save("file_index", @@dir_list)
end

#start_guiObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vimamsa/file_finder.rb', line 83

def start_gui()
  if cnf.search_dirs!.empty?
    message("FileFinder: No cnf.search_dirs defined")
    return
  end
  l = []
  $select_keys = ["h", "l", "f", "d", "s", "a", "g", "z"]
  if @@dir_list == nil
    Thread.new { FileFinder.recursively_find_files() }
  end

  # select_callback = proc { |search_str, idx| gui_file_finder_select_callback(search_str, idx) }
  select_callback = self.method("gui_file_finder_select_callback")
  update_callback = self.method("gui_file_finder_update_callback")


  opt = { :title => "Fuzzy filename search",
          :desc => "Search for files in folders defined in cnf.search_dirs" ,
          :columns => [{:title=>'Filename',:id=>0}]
          }
                           
  gui_select_update_window(l, $select_keys.collect { |x| x.upcase },
                           select_callback,
                           update_callback, opt)
end

#update_search_idxObject



74
75
76
# File 'lib/vimamsa/file_finder.rb', line 74

def update_search_idx
  FileFinder.update_search_idx
end

#updating?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/vimamsa/file_finder.rb', line 70

def updating?
  @@idx_updating
end