-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.rb
executable file
·644 lines (476 loc) · 13.9 KB
/
infer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
108
109
110
111
112
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#!/usr/bin/env ruby
require 'find'
require 'yaml'
require 'optparse'
require 'optparse/time'
require 'ostruct'
require 'pp'
class String
def nibble(fixnum=1)
range_end = self.length - 1
slice(fixnum..range_end)
end
end
class Infer
attr :results, :options
class Keyword
attr :term, :case_sensitive
def initialize(term,case_sensitive=false)
@term = term
@case_sensitive = case_sensitive
end
# Generate apple query expression
def qe_modifiers
end
end
class Result
include Comparable
attr_accessor :path, :rank
def <=>(other)
r = other.rank <=> rank
if r == 0
return other.path <=> path
end
r
end
def initialize(path, rank)
@path = path
@rank = rank
end
end
def initialize(arguments)
arguments = arguments.split(' ') if arguments.is_a? String
@arguments = arguments
@results = []
@options = {
inference_index: 0.01, # 10%
max_results: 40,
unlimited_results: false,
technique: 'exhaustive',
include_dirs: false,
display_info: true,
display_ranks: true,
display_indices: true,
prompt: true,
ignore: "(^\\.|log/)",
matchers: {
graphics: "\\.(png|jpeg|jpg|gif|tiff|psd)$",
},
handlers: {
default: "vim $",
graphics: "open $",
},
}
@content_keywords = []
@path_keywords = []
load_options('~/.infrc') # load from home dir
load_options('./.infrc') # load from current dir
parse_args
end
def term_lines
`tput lines`.to_i
end
def term_cols
`tput lines`.to_i
end
def transform_keys_to_symbols(value)
return value if not value.is_a?(Hash)
hash = value.inject({}){|memo,(k,v)| memo[k.to_sym] = transform_keys_to_symbols(v); memo}
return hash
end
def load_options(path)
path = File.expand_path(path)
yml_options = YAML::load_file(path) rescue return
yml_options = transform_keys_to_symbols(yml_options)
# yml_options = yml_options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
@options.merge! yml_options
end
def invalid_arg(name)
print "Options '#{name}' requires an argument.\n"
exit
end
def search search_dir
print "Searching #{search_dir}\n\n" if @options[:verbose]
case @options[:technique]
when 'grep'
grep_search search_dir
when 'mdfind'
begin
mdfind_search search_dir
rescue Errno::ENOENT
exhaustive_search search_dir
end
else
exhaustive_search search_dir
end
end
# Grep for content search
def grep_search search_dir
args = ['-r']
end
def cleanup_path path
path.strip!
end
def pipe_lines args
IO.popen args do |out|
while path = out.readline rescue nil do
yield path
end
end
end
def process_path path
rank = rank_path(path)
result = Result.new(path,rank)
# return false if @results.include? result
@results << result unless rank < 0
true
end
def rerank_results
c_kw = @path_keywords + @filter.split(' ')
@results.each { |r|
r.rank = rank_path r.path, c_kw
}
@results.sort!
end
def mdfind_search search_dir
results = []
@base_path = File.expand_path(search_dir) + '/'
def abs_to_rel! path
path.strip!
path.slice! @base_path
end
unless @path_keywords.empty?
# First we get all files that match any of the filenames (this includes directories)
# We have to do this so we can recurse the directories, for the desired behavior
# of searching on path, instead of just filename as mdfind does
# Note: DisplayName appears to be cached better than FSName, and is the same for files
query = "(%s)" % @path_keywords.collect{|k| "kMDItemDisplayName = '*%s*'" % k}.join(' || ')
# pp query
# pp search_dir
pipe_lines ['mdfind', '-onlyin', search_dir, query] do |path|
abs_to_rel! path
# puts path
r = rank_path path
results << r unless r.nil?
if File.directory? path
exhaustive_search(path)
end
end
end
# Now we filter the result by any content matches
c_results = []
if @content_keywords.any?
query = "(%s)" % @content_keywords.collect{|k| "kMDItemTextContent = '%s'cdw" % k }.join(' && ')
# pp query
# pp search_dir
pipe_lines ['mdfind', '-onlyin', search_dir, query] do |path|
abs_to_rel! path
c_results << path
end
# pp results
# pp c_results
# We take all content results if no path criteria,
# as if the default was all inclusive
if @path_keywords.any?
results = results.keep_if do |r|
c_results.include? r[0]
end
else
results = c_results.map {|r| Result.new(r,1) }
end
end
results
end
def exhaustive_search search_dir
Find.find(search_dir) do |path|
path.slice! './'
if @options[:ignore] && File.basename(path).match(@options[:ignore])
Find.prune if File.directory?(path) # Don't recurse this dir
next # Don't save result
end
path += '/' if File.directory?(path)
# rank = rank_path(path)
process_path path
end
end
def exec_result(result)
command = nil
if @options[:command]
command = @options[:command]
command += ' $' unless command.match /\$/
else
@options[:matchers].each do |type, pattern|
command = @options[:handlers][type] if result.path.match(pattern) && @options[:handlers][type]
end
command ||= @options[:handlers][:default]
end
command = command.gsub /\$/, '"%s"' % result.path.gsub('"','\"')
puts command
exec command
end
def rank_path(path, p_kw=@path_keywords)
unless @case_sensitive
path = path.downcase
p_kw = p_kw.map {|kw| kw.downcase }
end
if p_kw.empty? && !path.empty?
return 1
end
chars_matched = 0
content_matched = 0
if !@options[:include_dirs] && File.directory?(path)
return -1
end
p_kw.each do |condition|
return -1 unless path.include? condition
chars_matched += condition.length * path.scan(condition).length
end
c_kw = @content_keywords
if c_kw.any? && File.exists?(path) && !File.directory?(path)
File.open(path, "r") do |infile|
while (line = infile.gets)
c_kw.each do |kw|
if line.include? kw
content_matched += kw.length * line.scan(kw).length
end
end
end
end
end
# pp content_matched
return -1 if chars_matched == 0 && content_matched == 0
# puts
# puts chars_matched.to_f / path.length
chars_matched.to_f / path.length
end
def parse_args
opts = OptionParser.new do |opts|
opts.banner = "Usage: i [options] keywords..."
opts.separator ""
opts.separator "Options:"
opts.on("-m", "--max [num]", Integer, "Limit number of results") do |v|
@options[:max_results] = v
end
opts.on("-t", "--technique [mdfind|grep]", "Search technique to use") do |v|
@options[:technique] = v ? 'mdfind' : ''
end
opts.on("-s", "--[no-]showonly", "Show results and never open the inference") do |v|
@options[:show_only] = v
end
opts.on("-a", "--all", "Do not truncate the results") do |v|
@options[:max_results] = nil
end
opts.on("--[no-]prompt", "Prompt for result selection") do |v|
@options[:prompt] = v
end
opts.on("-p", "--plain", "Plain filename output; no indices, ranks, prompting, or unnecessary info.") do |v|
@options[:display_info] = false
@options[:display_ranks] = false
@options[:display_indices] = false
@options[:prompt] = false
end
opts.on("-z", "--null", "Separate results with a null character instead of newline.") do |v|
@options[:terminator] = "\x00"
end
opts.on("-g", "--global", "Global filesystem search") do |v|
@options[:global_search] = v
end
opts.on("-c [COMMAND]", "--command", "Execute command on inference") do |v|
@options[:command] = v
end
opts.on("-v", "--[no-]verbose", "Verbose output") do |v|
@options[:verbose] = v
end
opts.on("-[0-9]", "--index", Integer, "Force open result n") do |v|
@override_index = v
end
# no argument, shows at tail. this will print an options summary.
# try it and see!
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
@options[:command] = @arguments.shift if File.basename($0) == 'I'
opts.parse!(@arguments)
# inside search special notation
# parse search keywords
@arguments.each_with_index do |a, i|
case a
when /^\/.+/
@content_keywords << a.nibble
else
@path_keywords << a
end
# @arguments.delete_at(i)
end
end
def read_char
system "stty raw -echo"
STDIN.getc
ensure
system "stty -raw echo"
end
BACKSPACE = "\u007F"
def filtering_loop
@selection ||= 0
@filter = ''
begin
c = read_char
# pp c
# return
case c
when "\u0003" # ^C
puts 'quit'
return
when BACKSPACE
next if @filter.length == 0
@filter.chop!
print "\33[1D\33[K"
rerank_results
when "\33"
# pp STDIN.getc
#
# exit unless STDIN.stat.size > 0
if STDIN.getc == '['
d = STDIN.getc
if d == 'A'
@selection = [0, @selection-1].max
print "\33[1D\33[K"
end
if d == 'B' && @selection+1 < @display_count
@selection += 1
print "\33[1D\33[K"
end
end
when "\r"
exec_result @results[@selection]
when /[0-9]/
@selection = Integer(c)
when "\n"
exit
else
@filter += c
print c
rerank_results
# print "\\33[1C"
end
print_results(@filter)
end until c == "\n" || c == "\r"
exit
end
def print_results(filter=nil)
width = `tput cols`.to_i
height = `tput lines`.to_i
flen = filter ? filter.length : 0
results = @results
if filter
print "\33[1B\33[%dD" % (19 + flen)
c_kw = @path_keywords + filter.split(' ')
else
print "Filter: \n"
# print "\\33[7mFilter 100 files: \\n\\n"
end
# Erase to end of screen
print "\33[J\n"
if @options[:max_results]
@display_count = [@options[:max_results], results.length, height-5].min
else
@display_count = results.length
end
selection = @selection || 0
outputted = 0
first_result = nil
results.each_with_index do |result, i|
# next unless !filter || result.path.include?(filter)
next if result.rank < 0
break if outputted >= @display_count
selected = (selection == outputted)
first_result ||= result
outputted += 1
if !selected
# print "\\33[37m"
end
if @options[:display_indices]
print selected ? "\u25B6" : ' '
print " #{i} ".rjust((@display_count-1).to_s.length+2)
end
if @options[:display_ranks]
rank_ratio = result.rank / first_result.rank * 5
rank_remainder = rank_ratio - Integer(rank_ratio)
partial_blocks = ["\u258F","\u258E","\u258D","\u258C","\u258B","\u258A","\u2589","\u2588"]
remainder_block = partial_blocks[rank_remainder * partial_blocks.length]
print ("\u2588"*(rank_ratio) + remainder_block).ljust(6)
end
line = "#{result.path}"
# This is the best way to do a normal slice(-n) apparently
# Ruby is fucking stupid.
print line.split(//).last(width-12).join('')
if selected
# print " <- launch with <enter>"
else
print "\33[0m"
end
print "\n"
end
# print "\\33[J"
# count = results.reduce(0) {|r,c| c += 1; break if r.rank.nil? }
count = results.find_index {|r| r.rank.nil?} || results.length
if count > @display_count
puts "\n%d more hidden.\n" % (count - @display_count)
outputted += 2
end
# Move cursor up to filter
print "\33[%dA\33[%dC" % [outputted+2,8 + flen]
if @options[:prompt]
# print "\\nPick one of the results to launch (0-%d): " % (display_count-1)
# puts "\\33[%dA" % 5
# puts "\\33[J"
#
# filtering_loop
# sel = Integer(read_char) rescue nil
# exec_result @results[sel] unless sel.nil?
end
end
def run
# use first option as search directory if it is a dir and outside of the cwd
# search_dir = (@arguments[0] if @arguments[0] && @arguments[0].match(/^[\\~\\.\\/]/) && File.directory?(@arguments[0])) || './'
search_dir = './'
search_dir = '/' if @options[:global_search]
search(search_dir)
@results.sort!
if @results.empty?
print "Didn't find anything.\n"
exit
end
unless @options[:show_only] || !@options[:display_info]
if @results.count == 1 || @results[0].rank - @results[1].rank > @options[:inference_index]
exec_result @results[0]
exit
end
# print "\\Too vague. Try refining the search.\\n"
end
print "\n" if @options[:display_info]
print_results
filtering_loop
end
#
# return a structure describing the options.
#
def self.parse(args)
# the options specified on the command line will be collected in *options*.
# we set default values here.
end # parse()
end # class Infer
# options = Infer.parse(@arguments)
# pp options
if __FILE__ == $0
trap("SIGINT") { puts " ya"; exit!; }
begin
i = Infer.new(ARGV)
i.run
rescue OptionParser::InvalidOption => e
puts e.message
end
end
[0,1,2].map {|i| i + 1 }