gren 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/gren +1 -0
- data/lib/gren.rb +2 -2
- data/lib/gren/cli.rb +44 -22
- metadata +2 -2
data/bin/gren
CHANGED
data/lib/gren.rb
CHANGED
data/lib/gren/cli.rb
CHANGED
@@ -4,13 +4,18 @@ require 'optparse'
|
|
4
4
|
|
5
5
|
module Gren
|
6
6
|
class FindGrep
|
7
|
+
DEFAULT_DIR = '.'
|
8
|
+
DEFAULT_FILE_PATTERN = '(\.cpp$)|(\.c$)|(\.h$)|(\.hpp$)|(\.csv$)|(makefile$)|(makefile\.[0-9A-Za-z]+$)|(\.mk$)|(\.rb$)|(\.ags$)'
|
9
|
+
DEFAULT_FPATH_PATTERN = '(\.svn)|(\.git)|(CVS)'
|
10
|
+
|
7
11
|
attr_writer :ignoreCase
|
8
12
|
attr_writer :fpathDisp
|
9
13
|
|
10
|
-
def initialize(pattern, dir, filePattern)
|
14
|
+
def initialize(pattern, dir = DEFAULT_DIR, filePattern = DEFAULT_FILE_PATTERN, fpathPattern = DEFAULT_FPATH_PATTERN)
|
11
15
|
@pattern = pattern
|
12
16
|
@dir = dir
|
13
17
|
@filePattern = filePattern
|
18
|
+
@fpathPattern = fpathPattern
|
14
19
|
@ignoreCase = false
|
15
20
|
@fpathDisp = false
|
16
21
|
end
|
@@ -22,10 +27,13 @@ module Gren
|
|
22
27
|
Find::find(@dir) { |fpath|
|
23
28
|
if (File.file?(fpath) &&
|
24
29
|
fileRegexp.match(fpath) &&
|
25
|
-
fpath !~
|
30
|
+
fpath !~ /#{@fpathPattern}/)
|
31
|
+
# 行頭の./は削除
|
32
|
+
fpath.gsub!(/^.\//, "");
|
33
|
+
|
26
34
|
# ファイルパスを表示
|
27
35
|
if (@fpathDisp)
|
28
|
-
stdout.print "#{fpath}
|
36
|
+
stdout.print "#{fpath}"
|
29
37
|
end
|
30
38
|
|
31
39
|
# 検索
|
@@ -33,10 +41,22 @@ module Gren
|
|
33
41
|
file.each() { |line|
|
34
42
|
line.chomp!
|
35
43
|
if (patternRegexp.match(line))
|
36
|
-
|
44
|
+
if (!@fpathDisp)
|
45
|
+
stdout.print "#{fpath}:#{file.lineno}:#{line}\n"
|
46
|
+
else
|
47
|
+
# 隠しコマンド
|
48
|
+
# patternに"."を渡した時はFound patternを表示しない
|
49
|
+
# ファイル名一覧を取得する時等に便利
|
50
|
+
stdout.print " ........ Found pattern." unless (@pattern == ".")
|
51
|
+
break
|
52
|
+
end
|
37
53
|
end
|
38
54
|
}
|
39
55
|
}
|
56
|
+
|
57
|
+
# 改行
|
58
|
+
stdout.puts if (@fpathDisp)
|
59
|
+
|
40
60
|
end
|
41
61
|
}
|
42
62
|
end
|
@@ -51,37 +71,39 @@ module Gren
|
|
51
71
|
|
52
72
|
class CLI
|
53
73
|
def self.execute(stdout, arguments=[])
|
74
|
+
# オプション
|
54
75
|
ignoreCase = false
|
55
76
|
fpathDisp = false
|
56
77
|
|
57
78
|
# オプション解析
|
58
|
-
opt = OptionParser.new("#{File.basename($0)} [option] pattern dir [file_pattern] [fpath]")
|
59
|
-
opt.on('-i', '
|
60
|
-
opt.on('-
|
79
|
+
opt = OptionParser.new("#{File.basename($0)} [option] pattern [dir] [file_pattern] [fpath]")
|
80
|
+
opt.on('-i', 'Ignore case.') {|v| ignoreCase = true}
|
81
|
+
opt.on('-f', 'The searched file name is displayed.') {|v| fpathDisp = true}
|
61
82
|
opt.parse!(arguments)
|
62
83
|
|
63
84
|
# 検索オブジェクトの生成
|
64
85
|
findGrep = nil
|
65
86
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
87
|
+
case ARGV.length
|
88
|
+
when 1:
|
89
|
+
findGrep = FindGrep.new(arguments[0])
|
90
|
+
when 2:
|
91
|
+
findGrep = FindGrep.new(arguments[0], arguments[1])
|
92
|
+
when 3:
|
93
|
+
findGrep = FindGrep.new(arguments[0], arguments[1], arguments[2])
|
94
|
+
end
|
95
|
+
|
96
|
+
if (findGrep)
|
97
|
+
# オプション設定
|
98
|
+
findGrep.ignoreCase = ignoreCase
|
99
|
+
findGrep.fpathDisp = fpathDisp
|
100
|
+
|
101
|
+
# 検索
|
102
|
+
findGrep.searchAndPrint(stdout)
|
74
103
|
else
|
75
104
|
stdout.print opt.help
|
76
|
-
return
|
77
105
|
end
|
78
106
|
|
79
|
-
# オプション設定
|
80
|
-
findGrep.ignoreCase = ignoreCase
|
81
|
-
findGrep.fpathDisp = fpathDisp
|
82
|
-
|
83
|
-
# 検索
|
84
|
-
findGrep.searchAndPrint(stdout)
|
85
107
|
end
|
86
108
|
end
|
87
109
|
end
|