-
Notifications
You must be signed in to change notification settings - Fork 1
/
rloc
executable file
·84 lines (75 loc) · 1.43 KB
/
rloc
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
#!/usr/bin/env ruby
require 'set'
def matched?(str, keys)
keys.each do |key|
if not str.index(key)
return false
end
end
return true
end
def clipUselessSubPath(str, pats)
first_useless_idx = 0
pats.each do |pat|
idx = str.rindex(pat)
first_useless_idx = [first_useless_idx, idx + pat.length].max
end
cutidx = str.index("/", first_useless_idx)
if cutidx
str = str[0, cutidx]
end
return str
end
def hlputs(str, keys)
keys.length.times do |i|
key = keys[i]
str.gsub!(key, "\033[1;#{31+i}m\\&\033[m")
end
puts str
end
args = []
show_only_directory = false
ARGV.each do |x|
if x[0,1] == '-'
str = x[1..-1]
while str && str != ""
s = str[0,1]
str = str[1..-1]
if s == 'd'
show_only_directory = true
elsif s == 'c'
args.insert 0, Dir.pwd
else
raise "unrecognized arguments #{x}"
end
end
else
args << x
end
end
if args.length == 0
puts "usage: rloc key1 key2 key3 ... [-d]"
end
arg0 = args[0]
arg_other = args[1..-1]
yet_put = Set.new
open("|locate #{arg0}") do |f|
f.each_line do |line|
line.strip!
if not matched?(line, arg_other)
next
end
line = clipUselessSubPath(line, args)
if (yet_put.include?(line))
next
end
yet_put.add(line)
if show_only_directory
if File.directory?(line)
hlputs line, args
end
else
hlputs line, args
end
end
end