gren 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/gren CHANGED
@@ -7,5 +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.0.4"
10
+ Version = "0.0.5"
11
11
  Gren::CLI.execute(STDOUT, ARGV)
data/lib/gren.rb CHANGED
@@ -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.4'
5
+ VERSION = '0.0.5'
6
6
  end
data/lib/gren/cli.rb CHANGED
@@ -6,13 +6,15 @@ module Gren
6
6
  class CLI
7
7
  def self.execute(stdout, arguments=[])
8
8
  # オプション
9
- ignoreCase = false
10
- fpathDisp = false
9
+ option = FindGrep::Option.new(false, false, ".", nil, nil)
11
10
 
12
11
  # オプション解析
13
- opt = OptionParser.new("#{File.basename($0)} [option] pattern [dir] [filename_regexp]")
14
- opt.on('-i', 'Ignore case.') {|v| ignoreCase = true}
15
- opt.on('-f', 'The searched file name is displayed.') {|v| fpathDisp = true}
12
+ opt = OptionParser.new("#{File.basename($0)} [option] pattern [dir]")
13
+ opt.on('-i', '--ignore', 'Ignore case.') {|v| option.ignoreCase = true}
14
+ opt.on('-l', '--listing', 'The searched file name is displayed.') {|v| option.fpathDisp = true}
15
+ opt.on('-f REGEXP', '--file-regexp REGEXP', 'Search file regexp. (default: ".")') {|v| option.filePattern = v}
16
+ opt.on('--if REGEXP', '--ignore-file REGEXP', 'Ignore file pattern.') {|v| option.ignoreFile = v}
17
+ opt.on('--id REGEXP', '--ignore-dir REGEXP', 'Ignore dir pattern.') {|v| option.ignoreDir = v}
16
18
  opt.parse!(arguments)
17
19
 
18
20
  # 検索オブジェクトの生成
@@ -20,19 +22,12 @@ module Gren
20
22
 
21
23
  case ARGV.length
22
24
  when 1:
23
- findGrep = FindGrep.new(arguments[0])
25
+ findGrep = FindGrep.new(arguments[0], '.', option)
24
26
  when 2:
25
- findGrep = FindGrep.new(arguments[0], arguments[1])
26
- when 3:
27
- findGrep = FindGrep.new(arguments[0], arguments[1], arguments[2])
27
+ findGrep = FindGrep.new(arguments[0], arguments[1], option)
28
28
  end
29
29
 
30
30
  if (findGrep)
31
- # オプション設定
32
- findGrep.ignoreCase = ignoreCase
33
- findGrep.fpathDisp = fpathDisp
34
-
35
- # 検索
36
31
  findGrep.searchAndPrint(stdout)
37
32
  else
38
33
  stdout.print opt.help
data/lib/gren/findgrep.rb CHANGED
@@ -4,27 +4,31 @@ require File.join(File.dirname(__FILE__), 'result')
4
4
 
5
5
  module Gren
6
6
  class FindGrep
7
- # DEFAULT_FPATH_PATTERN = '(\.svn)|(\.git)|(CVS)|(\.o$)|(\.lo$)|(\.la$)|(^#.*#$)|(~$)|(^.#)|(^\.DS_Store$)|(\.bak$)|(\.BAK$)'
8
7
  IGNORE_FILE = /(\A#.*#\Z)|(~\Z)|(\A\.#)/
9
- IGNORE_DIR = /(\A\.svn\Z)|(\A\.git\Z)|(\ACVS\Z)/
8
+ IGNORE_DIR = /(\A\.svn\Z)|(\A\.git\Z)|(\ACVS\Z)/
10
9
 
11
- attr_writer :ignoreCase
12
- attr_writer :fpathDisp
10
+ Option = Struct.new(:ignoreCase, :fpathDisp, :filePattern, :ignoreFile, :ignoreDir)
13
11
 
14
- def initialize(pattern, dir = '.', filePattern = '.')
12
+ def initialize(pattern, dir, option)
15
13
  @pattern = pattern
16
14
  @dir = dir
17
- @fileRegexp = Regexp.new(filePattern)
18
- @ignoreCase = false
19
- @fpathDisp = false
15
+ @option = option
16
+ @fileRegexp = Regexp.new(option.filePattern)
20
17
  @patternRegexp = makePattenRegexp
18
+ @ignoreFile = Regexp.new(option.ignoreFile) if option.ignoreFile
19
+ @ignoreDir = Regexp.new(option.ignoreDir) if option.ignoreDir
21
20
  @result = Result.new(@dir)
22
21
  end
23
22
 
24
23
  def searchAndPrint(stdout)
25
24
  Find::find(@dir) { |fpath|
25
+ fpath_disp = fpath.gsub(/^.\//, "")
26
+
26
27
  # 除外ディレクトリ
27
- Find.prune if ignoreDir?(fpath)
28
+ if ignoreDir?(fpath)
29
+ @result.prune_dirs << fpath_disp if (@option.fpathDisp)
30
+ Find.prune
31
+ end
28
32
 
29
33
  # ファイルでなければ探索しない
30
34
  next unless FileTest.file?(fpath)
@@ -32,50 +36,73 @@ module Gren
32
36
  @result.count += 1
33
37
 
34
38
  # 読み込み不可ならば探索しない
35
- next unless FileTest.readable?(fpath)
39
+ unless FileTest.readable?(fpath)
40
+ @result.unreadable_files << fpath_disp if (@option.fpathDisp)
41
+ next
42
+ end
36
43
 
37
44
  @result.size += FileTest.size(fpath)
38
45
 
39
46
  # 除外ファイル
40
- next if ignoreFile?(fpath)
47
+ if ignoreFile?(fpath)
48
+ @result.ignore_files << fpath_disp if (@option.fpathDisp)
49
+ next
50
+ end
41
51
 
42
52
  @result.search_count += 1
43
53
  @result.search_size += FileTest.size(fpath)
44
54
 
45
- # 行頭の./は削除
46
- fpath.gsub!(/^.\//, "");
47
-
48
55
  # 検索
49
- searchMain(stdout, fpath)
56
+ searchMain(stdout, fpath, fpath_disp)
50
57
  }
51
58
 
52
59
  @result.time_stop
60
+
61
+ if (@option.fpathDisp)
62
+ stdout.puts
63
+ stdout.puts "--- search --------"
64
+ print_fpaths stdout, @result.search_files
65
+ stdout.puts "--- match --------"
66
+ print_fpaths stdout, @result.match_files
67
+ stdout.puts "--- ignore-file --------"
68
+ print_fpaths stdout, @result.ignore_files
69
+ stdout.puts "--- ignore-dir --------"
70
+ print_fpaths stdout, @result.prune_dirs
71
+ stdout.puts "--- unreadable --------"
72
+ print_fpaths stdout, @result.unreadable_files
73
+ end
74
+
53
75
  stdout.puts
54
76
  @result.print(stdout)
55
77
  end
56
78
 
79
+ def print_fpaths(stdout, data)
80
+ stdout.print data.join("\n")
81
+ stdout.puts if data.count > 0
82
+ stdout.puts "total: #{data.count}"
83
+ stdout.puts
84
+ end
85
+ private :print_fpaths
86
+
57
87
  def makePattenRegexp
58
88
  option = 0
59
- option |= Regexp::IGNORECASE if (@ignoreCase)
89
+ option |= Regexp::IGNORECASE if (@option.ignoreCase)
60
90
  Regexp.new(@pattern, option)
61
91
  end
62
92
  private :makePattenRegexp
63
93
 
64
94
  def ignoreDir?(fpath)
65
95
  FileTest.directory?(fpath) &&
66
- IGNORE_DIR.match(File.basename(fpath))
96
+ (IGNORE_DIR.match(File.basename(fpath)) ||
97
+ (@ignoreDir && @ignoreDir.match(File.basename(fpath))))
67
98
  end
68
99
  private :ignoreDir?
69
100
 
70
- def readFile?(fpath)
71
- @fileRegexp.match(fpath) &&
72
- !IGNORE_FILE.match(File.basename(fpath)) &&
73
- !binary?(fpath)
74
- end
75
- private :readFile?
76
-
77
101
  def ignoreFile?(fpath)
78
- !readFile?(fpath)
102
+ !@fileRegexp.match(fpath) ||
103
+ IGNORE_FILE.match(File.basename(fpath)) ||
104
+ (@ignoreFile && @ignoreFile.match(File.basename(fpath))) ||
105
+ binary?(fpath)
79
106
  end
80
107
  private :ignoreFile?
81
108
 
@@ -84,26 +111,19 @@ module Gren
84
111
  return s.index("\x00")
85
112
  end
86
113
 
87
- def searchMain(stdout, fpath)
88
- # ファイルパスを表示
89
- stdout.print "#{fpath}" if (@fpathDisp)
114
+ def searchMain(stdout, fpath, fpath_disp)
115
+ @result.search_files << fpath_disp if (@option.fpathDisp)
90
116
 
91
117
  open(fpath, "r") { |file|
92
118
  match_file = false
93
119
  file.each() { |line|
94
120
  line.chomp!
95
121
  if (@patternRegexp.match(line))
96
- if (!@fpathDisp)
97
- stdout.print "#{fpath}:#{file.lineno}:#{line}\n"
98
- else
99
- # 隠しコマンド
100
- # patternに"."を渡した時はFound patternを表示しない
101
- # ファイル名一覧を取得する時等に便利
102
- stdout.print " ........ Found pattern." unless (@pattern == ".")
103
- end
122
+ stdout.puts "#{fpath_disp}:#{file.lineno}:#{line}"
104
123
 
105
124
  unless match_file
106
- @result.match_file += 1
125
+ @result.match_file_count += 1
126
+ @result.match_files << fpath_disp if (@option.fpathDisp)
107
127
  match_file = true
108
128
  end
109
129
 
@@ -111,9 +131,6 @@ module Gren
111
131
  end
112
132
  }
113
133
  }
114
-
115
- # 改行
116
- stdout.puts if (@fpathDisp)
117
134
  end
118
135
  private :searchMain
119
136
 
data/lib/gren/result.rb CHANGED
@@ -5,15 +5,22 @@ module Gren
5
5
  class Result
6
6
  attr_accessor :count
7
7
  attr_accessor :search_count
8
- attr_accessor :match_file
8
+ attr_accessor :match_file_count
9
9
  attr_accessor :match_count
10
10
  attr_accessor :size
11
11
  attr_accessor :search_size
12
+
13
+ attr_accessor :search_files
14
+ attr_accessor :match_files
15
+ attr_accessor :unreadable_files
16
+ attr_accessor :prune_dirs
17
+ attr_accessor :ignore_files
12
18
 
13
19
  def initialize(start_dir)
14
20
  @start_dir = File.expand_path(start_dir)
15
- @count, @search_count, @match_file, @match_count, @size, @search_size = 0, 0, 0, 0, 0, 0
21
+ @count, @search_count, @match_file_count, @match_count, @size, @search_size = 0, 0, 0, 0, 0, 0
16
22
  @start_time = Time.now
23
+ @search_files, @match_files, @unreadable_files, @prune_dirs, @ignore_files = [], [], [], [], []
17
24
  end
18
25
 
19
26
  def time_stop
@@ -67,7 +74,7 @@ module Gren
67
74
  def print(stdout)
68
75
  stdout.puts "dir : #{@start_dir} (#{time_s})"
69
76
  stdout.puts "files : #{@search_count} in #{@count} (#{size_s(@search_size)} in #{size_s(@size)})"
70
- stdout.puts "match : #{@match_file} files, #{match_count} match"
77
+ stdout.puts "match : #{@match_file_count} files, #{match_count} hit"
71
78
  end
72
79
 
73
80
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - ongaeshi