-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_src
executable file
·95 lines (82 loc) · 1.88 KB
/
find_src
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
#!/usr/bin/env ruby1.8
$search_pat = nil
$find_header = false
$find_source = false
$find_text = false
ARGV.each do |arg|
if arg == '-h'
$find_header = true
elsif arg == '-s'
$find_source = true
elsif arg == '-t'
$find_text = true
else
$search_pat = arg
end
end
if not $search_pat
puts "usage: #{__FILE__} search_pat [-h] [-s]"
end
if not $find_header and not $find_source and not $find_text
$find_header = true
$find_source = true
$find_text = true
end
$search_path = Dir.pwd
#initial done -----------------------------------------------------
require 'pathname'
GrepMatchedItem = Struct.new(:line, :cont)
def mygrep(fname, re)
matched = []
open(fname) do |f|
cont = f.read
lidx = 1
cont.each_line do |l|
if re =~ l
matched << GrepMatchedItem.new(lidx, l)
end
lidx += 1
end
end
if not matched.empty?
puts fname
matched.each do |s|
lead = sprintf("%10d: ", s.line)
if STDOUT.tty?
cont = s.cont.gsub(re, "\033[1;31m\\&\033[m")
else
cont = s.cont
end
puts lead + cont
end
end
end
puts "locating file list"
locate_exps = []
locate_exps += ['h', 'hpp', 'hxx', 'hh', 'idl'] if $find_header
locate_exps += ['c', 'cpp', 'cxx', 'cc', 'inl'] if $find_source
locate_exps += ['txt', 'kui', 'kuip', 'ui'] if $find_text
locate_re = "^#{$search_path}.*\\.(#{locate_exps.join '|'})$"
located_files = []
open("|locate --regex '#{locate_re}'") do |f|
f.each_line do |l|
located_files << l.strip
end
end
puts "searching patten, file count #{located_files.length}"
re = Regexp.new($search_pat)
located_files.length.times do |i|
# 显示进度
fname = located_files[i]
print "[#{i+1}/#{located_files.length}] \r"
$stdout.flush
if File.file?(fname)
begin
mygrep(fname, re)
rescue
$stderr.puts "Failed to process #{fname}"
end
end
end
puts
puts