gren 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/gren +1 -0
  2. data/lib/gren.rb +2 -2
  3. data/lib/gren/cli.rb +44 -22
  4. metadata +2 -2
data/bin/gren CHANGED
@@ -7,4 +7,5 @@ require 'rubygems'
7
7
  require File.expand_path(File.dirname(__FILE__) + "/../lib/gren")
8
8
  require "gren/cli"
9
9
 
10
+ Version = "0.2.0"
10
11
  Gren::CLI.execute(STDOUT, ARGV)
@@ -2,5 +2,5 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module Gren
5
- VERSION = '0.0.1'
6
- end
5
+ VERSION = '0.0.2'
6
+ end
@@ -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 !~ /\.svn/) # .svnディレクトリは無視
30
+ fpath !~ /#{@fpathPattern}/)
31
+ # 行頭の./は削除
32
+ fpath.gsub!(/^.\//, "");
33
+
26
34
  # ファイルパスを表示
27
35
  if (@fpathDisp)
28
- stdout.print "#{fpath}\n"
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
- stdout.print "#{fpath}:#{file.lineno}:#{line}\n"
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', '大文字と小文字を無視する') {|v| ignoreCase = true}
60
- opt.on('-d', '検索対象に含めたファイルを表示') {|v| fpathDisp = true}
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
- if (ARGV.length == 2)
67
- findGrep = FindGrep.new(arguments[0],
68
- arguments[1],
69
- '(\.cpp$)|(\.c$)|(\.h$)|(\.hpp$)|(\.csv$)|(makefile$)|(makefile\.[0-9A-Za-z]+$)|(\.mk$)|(\.rb$)|(\.ags$)')
70
- elsif (arguments.length == 3)
71
- findGrep = FindGrep.new(arguments[0],
72
- arguments[1],
73
- arguments[2])
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - ongaeshi