Skip to content

Instantly share code, notes, and snippets.

@em
Created March 26, 2011 17:37
Show Gist options
  • Save em/888465 to your computer and use it in GitHub Desktop.
Save em/888465 to your computer and use it in GitHub Desktop.
Simple utility that takes a list of keywords and recursively searches the current working directory opening the file in vim if it can infer a unique desired file.
#!/usr/bin/env ruby
require 'find'
# Take the first file if it's this much better than the alternatives:
InferenceIndex = 0.1 # 10%
results = []
if ARGV.empty?
print <<HEREDOC
Usage: e [options] [keyword]...
Recursively searches the current directory based on a set of keywords,
launching the best match if it is better than the next by at least #{(InferenceIndex * 100).to_i}%.
Otherwise it offers a choice.
Options:
-l List results, don't open
-a Show all results, unlimited
HEREDOC
exit
end
opt_list = false
ARGV.each do |a|
opt_list = true if a.match(/^-l/)
end
num_kw_chars = ARGV.join.length
Find.find('./') do |fname|
keywords_found = 0
ARGV.each do |condition|
keywords_found += condition.length * fname.scan(condition).length if fname.include? condition
end
results << [fname, keywords_found.to_f / fname.length] unless keywords_found < num_kw_chars
end
results.sort! { |a,b| b[1] <=> a[1] }
if results.empty?
puts "Didn't find shit."
exit
end
if results.count == 1 || results[0][1] - results[1][1] > InferenceIndex
exec "vim '%s'" % results[0][0]
exit
end
print "\nAmbiguous:\n\n"
results[0..9].each_with_index do |result, i|
print "#{i}. "
10.times do |i|
print (result[1]/results[0][1]*10) < i ? ' ' : '|'
end
print " #{result[0]} \n"
end
if results.length > 9
puts "\n%d more hidden." % results.length
end
print "\nTry refining the search,\nor pick one of the above (0-%d): " % [results.length, 9].min
sel = Integer(STDIN.gets) rescue nil
exec "vim '%s'" % results[sel][0] unless sel.nil?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment